statusor.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright 2020 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef RLWE_STATUSOR_H_
  17. #define RLWE_STATUSOR_H_
  18. #include <cassert>
  19. #include "absl/base/attributes.h"
  20. #include "absl/status/status.h"
  21. #include "absl/types/optional.h"
  22. namespace rlwe {
  23. template <typename T>
  24. class StatusOr {
  25. public:
  26. // Construct a new StatusOr with Status::UNKNOWN status
  27. StatusOr();
  28. // Construct a new StatusOr with the given non-ok status. After calling
  29. // this constructor, calls to value() will CHECK-fail.
  30. //
  31. // NOTE: Not explicit - we want to use StatusOr<T> as a return
  32. // value, so it is convenient and sensible to be able to do 'return
  33. // Status()' when the return type is StatusOr<T>.
  34. //
  35. // REQUIRES: status != Status::OK. This requirement is DCHECKed.
  36. // In optimized builds, passing Status::OK here will have the effect
  37. // of passing PosixErrorSpace::EINVAL as a fallback.
  38. StatusOr(const absl::Status& status);
  39. // Construct a new StatusOr with the given value. If T is a plain pointer,
  40. // value must not be NULL. After calling this constructor, calls to
  41. // value() will succeed, and calls to status() will return OK.
  42. //
  43. // NOTE: Not explicit - we want to use StatusOr<T> as a return type
  44. // so it is convenient and sensible to be able to do 'return T()'
  45. // when the return type is StatusOr<T>.
  46. //
  47. // REQUIRES: if T is a plain pointer, value != NULL. This requirement is
  48. // DCHECKed. In optimized builds, passing a NULL pointer here will have
  49. // the effect of passing absl::StatusCode::kInternal as a fallback.
  50. StatusOr(const T& value);
  51. // Copy constructor.
  52. StatusOr(const StatusOr& other);
  53. // Assignment operator.
  54. StatusOr& operator=(const StatusOr& other);
  55. // Move constructor and move-assignment operator.
  56. StatusOr(StatusOr&& other) = default;
  57. StatusOr& operator=(StatusOr&& other) = default;
  58. // Rvalue-reference overloads of the other constructors and assignment
  59. // operators, to support move-only types and avoid unnecessary copying.
  60. StatusOr(T&& value);
  61. // Returns a reference to our status. If this contains a T, then
  62. // returns Status::OK.
  63. const absl::Status& status() const;
  64. // Returns this->status().ok()
  65. bool ok() const;
  66. // Returns a reference to our current value, or CHECK-fails if !this->ok().
  67. const T& ValueOrDie() const&;
  68. T& ValueOrDie() &;
  69. const T&& ValueOrDie() const&&;
  70. T&& ValueOrDie() &&;
  71. // Returns a reference to our current value, or CHECK-fails if !this->ok().
  72. const T& value() const&;
  73. T& value() &;
  74. const T&& value() const&&;
  75. T&& value() &&;
  76. // Ignores any errors. This method does nothing except potentially suppress
  77. // complaints from any tools that are checking that errors are not dropped on
  78. // the floor.
  79. void IgnoreError() const {}
  80. operator absl::Status() const { return status(); }
  81. template <template <typename> class OtherStatusOrType>
  82. operator OtherStatusOrType<T>() {
  83. if (value_) {
  84. return OtherStatusOrType<T>(std::move(value_.value()));
  85. } else {
  86. return OtherStatusOrType<T>(status());
  87. }
  88. }
  89. private:
  90. absl::Status status_;
  91. absl::optional<T> value_;
  92. };
  93. namespace internal {
  94. class StatusOrHelper {
  95. public:
  96. // Move type-agnostic error handling to the .cc.
  97. static absl::Status HandleInvalidStatusCtorArg();
  98. static absl::Status HandleNullObjectCtorArg();
  99. static void Crash(const absl::Status& status);
  100. // Customized behavior for StatusOr<T> vs. StatusOr<T*>
  101. template <typename T>
  102. struct Specialize;
  103. };
  104. template <typename T>
  105. struct StatusOrHelper::Specialize {
  106. // For non-pointer T, a reference can never be NULL.
  107. static inline bool IsValueNull(const T& t) { return false; }
  108. };
  109. template <typename T>
  110. struct StatusOrHelper::Specialize<T*> {
  111. static inline bool IsValueNull(const T* t) { return t == nullptr; }
  112. };
  113. } // namespace internal
  114. template <typename T>
  115. inline StatusOr<T>::StatusOr()
  116. : status_(absl::UnknownError("")), value_(absl::nullopt) {}
  117. template <typename T>
  118. inline StatusOr<T>::StatusOr(const absl::Status& status)
  119. : status_(status), value_(absl::nullopt) {
  120. if (status.ok()) {
  121. status_ = internal::StatusOrHelper::HandleInvalidStatusCtorArg();
  122. }
  123. }
  124. template <typename T>
  125. inline StatusOr<T>::StatusOr(const T& value)
  126. : status_(absl::OkStatus()), value_(value) {
  127. if (internal::StatusOrHelper::Specialize<T>::IsValueNull(value)) {
  128. status_ = internal::StatusOrHelper::HandleNullObjectCtorArg();
  129. }
  130. }
  131. template <typename T>
  132. inline StatusOr<T>::StatusOr(const StatusOr& other)
  133. : status_(other.status_), value_(other.value_) {}
  134. template <typename T>
  135. inline StatusOr<T>& StatusOr<T>::operator=(const StatusOr<T>& other) {
  136. status_ = other.status_;
  137. value_.reset(other.value_);
  138. return *this;
  139. }
  140. template <typename T>
  141. inline StatusOr<T>::StatusOr(T&& value)
  142. : status_(absl::OkStatus()), value_(std::forward<T>(value)) {
  143. if (internal::StatusOrHelper::Specialize<T>::IsValueNull(value_.value())) {
  144. status_ = internal::StatusOrHelper::HandleNullObjectCtorArg();
  145. }
  146. }
  147. template <typename T>
  148. inline const absl::Status& StatusOr<T>::status() const {
  149. return status_;
  150. }
  151. template <typename T>
  152. inline bool StatusOr<T>::ok() const {
  153. return status_.ok();
  154. }
  155. template <typename T>
  156. inline const T& StatusOr<T>::ValueOrDie() const& {
  157. if (!value_) {
  158. internal::StatusOrHelper::Crash(status());
  159. }
  160. return value_.value();
  161. }
  162. template <typename T>
  163. inline T& StatusOr<T>::ValueOrDie() & {
  164. if (!value_) {
  165. internal::StatusOrHelper::Crash(status());
  166. }
  167. return value_.value();
  168. }
  169. template <typename T>
  170. inline const T&& StatusOr<T>::ValueOrDie() const&& {
  171. if (!value_) {
  172. internal::StatusOrHelper::Crash(status());
  173. }
  174. return std::move(value_.value());
  175. }
  176. template <typename T>
  177. inline T&& StatusOr<T>::ValueOrDie() && {
  178. if (!value_) {
  179. internal::StatusOrHelper::Crash(status());
  180. }
  181. return std::move(value_.value());
  182. }
  183. template <typename T>
  184. inline const T& StatusOr<T>::value() const& {
  185. if (!value_) {
  186. internal::StatusOrHelper::Crash(status());
  187. }
  188. return value_.value();
  189. }
  190. template <typename T>
  191. inline T& StatusOr<T>::value() & {
  192. if (!value_) {
  193. internal::StatusOrHelper::Crash(status());
  194. }
  195. return value_.value();
  196. }
  197. template <typename T>
  198. inline const T&& StatusOr<T>::value() const&& {
  199. if (!value_) {
  200. internal::StatusOrHelper::Crash(status());
  201. }
  202. return std::move(value_.value());
  203. }
  204. template <typename T>
  205. inline T&& StatusOr<T>::value() && {
  206. if (!value_) {
  207. internal::StatusOrHelper::Crash(status());
  208. }
  209. return std::move(value_.value());
  210. }
  211. } // namespace rlwe
  212. #endif // RLWE_STATUSOR_H_