诸暨麻将添加redis
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

264 lignes
8.4 KiB

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // StatusOr<T> is the union of a Status object and a T
  31. // object. StatusOr models the concept of an object that is either a
  32. // usable value, or an error Status explaining why such a value is
  33. // not present. To this end, StatusOr<T> does not allow its Status
  34. // value to be Status::OK. Further, StatusOr<T*> does not allow the
  35. // contained pointer to be nullptr.
  36. //
  37. // The primary use-case for StatusOr<T> is as the return value of a
  38. // function which may fail.
  39. //
  40. // Example client usage for a StatusOr<T>, where T is not a pointer:
  41. //
  42. // StatusOr<float> result = DoBigCalculationThatCouldFail();
  43. // if (result.ok()) {
  44. // float answer = result.ValueOrDie();
  45. // printf("Big calculation yielded: %f", answer);
  46. // } else {
  47. // LOG(ERROR) << result.status();
  48. // }
  49. //
  50. // Example client usage for a StatusOr<T*>:
  51. //
  52. // StatusOr<Foo*> result = FooFactory::MakeNewFoo(arg);
  53. // if (result.ok()) {
  54. // std::unique_ptr<Foo> foo(result.ValueOrDie());
  55. // foo->DoSomethingCool();
  56. // } else {
  57. // LOG(ERROR) << result.status();
  58. // }
  59. //
  60. // Example client usage for a StatusOr<std::unique_ptr<T>>:
  61. //
  62. // StatusOr<std::unique_ptr<Foo>> result = FooFactory::MakeNewFoo(arg);
  63. // if (result.ok()) {
  64. // std::unique_ptr<Foo> foo = result.ConsumeValueOrDie();
  65. // foo->DoSomethingCool();
  66. // } else {
  67. // LOG(ERROR) << result.status();
  68. // }
  69. //
  70. // Example factory implementation returning StatusOr<T*>:
  71. //
  72. // StatusOr<Foo*> FooFactory::MakeNewFoo(int arg) {
  73. // if (arg <= 0) {
  74. // return ::util::Status(::util::error::INVALID_ARGUMENT,
  75. // "Arg must be positive");
  76. // } else {
  77. // return new Foo(arg);
  78. // }
  79. // }
  80. //
  81. #ifndef GOOGLE_PROTOBUF_STUBS_STATUSOR_H_
  82. #define GOOGLE_PROTOBUF_STUBS_STATUSOR_H_
  83. #include <new>
  84. #include <string>
  85. #include <utility>
  86. #include <google/protobuf/stubs/status.h>
  87. #include <google/protobuf/port_def.inc>
  88. namespace google {
  89. namespace protobuf {
  90. namespace util {
  91. template<typename T>
  92. class StatusOr {
  93. template<typename U> friend class StatusOr;
  94. public:
  95. // Construct a new StatusOr with Status::UNKNOWN status
  96. StatusOr();
  97. // Construct a new StatusOr with the given non-ok status. After calling
  98. // this constructor, calls to ValueOrDie() will CHECK-fail.
  99. //
  100. // NOTE: Not explicit - we want to use StatusOr<T> as a return
  101. // value, so it is convenient and sensible to be able to do 'return
  102. // Status()' when the return type is StatusOr<T>.
  103. //
  104. // REQUIRES: status != Status::OK. This requirement is DCHECKed.
  105. // In optimized builds, passing Status::OK here will have the effect
  106. // of passing PosixErrorSpace::EINVAL as a fallback.
  107. StatusOr(const Status& status); // NOLINT
  108. // Construct a new StatusOr with the given value. If T is a plain pointer,
  109. // value must not be nullptr. After calling this constructor, calls to
  110. // ValueOrDie() will succeed, and calls to status() will return OK.
  111. //
  112. // NOTE: Not explicit - we want to use StatusOr<T> as a return type
  113. // so it is convenient and sensible to be able to do 'return T()'
  114. // when when the return type is StatusOr<T>.
  115. //
  116. // REQUIRES: if T is a plain pointer, value != nullptr. This requirement is
  117. // DCHECKed. In optimized builds, passing a null pointer here will have
  118. // the effect of passing PosixErrorSpace::EINVAL as a fallback.
  119. StatusOr(const T& value); // NOLINT
  120. // Copy constructor.
  121. StatusOr(const StatusOr& other);
  122. // Conversion copy constructor, T must be copy constructible from U
  123. template<typename U>
  124. StatusOr(const StatusOr<U>& other);
  125. // Assignment operator.
  126. StatusOr& operator=(const StatusOr& other);
  127. // Conversion assignment operator, T must be assignable from U
  128. template<typename U>
  129. StatusOr& operator=(const StatusOr<U>& other);
  130. // Returns a reference to our status. If this contains a T, then
  131. // returns Status::OK.
  132. const Status& status() const;
  133. // Returns this->status().ok()
  134. bool ok() const;
  135. // Returns a reference to our current value, or CHECK-fails if !this->ok().
  136. // If you need to initialize a T object from the stored value,
  137. // ConsumeValueOrDie() may be more efficient.
  138. const T& ValueOrDie() const;
  139. private:
  140. Status status_;
  141. T value_;
  142. };
  143. ////////////////////////////////////////////////////////////////////////////////
  144. // Implementation details for StatusOr<T>
  145. namespace internal {
  146. class PROTOBUF_EXPORT StatusOrHelper {
  147. public:
  148. // Move type-agnostic error handling to the .cc.
  149. static void Crash(const util::Status& status);
  150. // Customized behavior for StatusOr<T> vs. StatusOr<T*>
  151. template<typename T>
  152. struct Specialize;
  153. };
  154. template<typename T>
  155. struct StatusOrHelper::Specialize {
  156. // For non-pointer T, a reference can never be nullptr.
  157. static inline bool IsValueNull(const T& t) { return false; }
  158. };
  159. template<typename T>
  160. struct StatusOrHelper::Specialize<T*> {
  161. static inline bool IsValueNull(const T* t) { return t == nullptr; }
  162. };
  163. } // namespace internal
  164. template<typename T>
  165. inline StatusOr<T>::StatusOr()
  166. : status_(util::Status::UNKNOWN) {
  167. }
  168. template<typename T>
  169. inline StatusOr<T>::StatusOr(const Status& status) {
  170. if (status.ok()) {
  171. status_ = Status(error::INTERNAL, "Status::OK is not a valid argument.");
  172. } else {
  173. status_ = status;
  174. }
  175. }
  176. template<typename T>
  177. inline StatusOr<T>::StatusOr(const T& value) {
  178. if (internal::StatusOrHelper::Specialize<T>::IsValueNull(value)) {
  179. status_ = Status(error::INTERNAL, "nullptr is not a vaild argument.");
  180. } else {
  181. status_ = Status::OK;
  182. value_ = value;
  183. }
  184. }
  185. template<typename T>
  186. inline StatusOr<T>::StatusOr(const StatusOr<T>& other)
  187. : status_(other.status_), value_(other.value_) {
  188. }
  189. template<typename T>
  190. inline StatusOr<T>& StatusOr<T>::operator=(const StatusOr<T>& other) {
  191. status_ = other.status_;
  192. value_ = other.value_;
  193. return *this;
  194. }
  195. template<typename T>
  196. template<typename U>
  197. inline StatusOr<T>::StatusOr(const StatusOr<U>& other)
  198. : status_(other.status_), value_(other.status_.ok() ? other.value_ : T()) {
  199. }
  200. template<typename T>
  201. template<typename U>
  202. inline StatusOr<T>& StatusOr<T>::operator=(const StatusOr<U>& other) {
  203. status_ = other.status_;
  204. if (status_.ok()) value_ = other.value_;
  205. return *this;
  206. }
  207. template<typename T>
  208. inline const Status& StatusOr<T>::status() const {
  209. return status_;
  210. }
  211. template<typename T>
  212. inline bool StatusOr<T>::ok() const {
  213. return status().ok();
  214. }
  215. template<typename T>
  216. inline const T& StatusOr<T>::ValueOrDie() const {
  217. if (!status_.ok()) {
  218. internal::StatusOrHelper::Crash(status_);
  219. }
  220. return value_;
  221. }
  222. } // namespace util
  223. } // namespace protobuf
  224. } // namespace google
  225. #include <google/protobuf/port_undef.inc>
  226. #endif // GOOGLE_PROTOBUF_STUBS_STATUSOR_H_