诸暨麻将添加redis
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

221 linhas
8.1 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. #ifndef GOOGLE_PROTOBUF_UTIL_CONVERTER_DATAPIECE_H__
  31. #define GOOGLE_PROTOBUF_UTIL_CONVERTER_DATAPIECE_H__
  32. #include <string>
  33. #include <google/protobuf/stubs/common.h>
  34. #include <google/protobuf/stubs/logging.h>
  35. #include <google/protobuf/type.pb.h>
  36. #include <google/protobuf/stubs/strutil.h>
  37. #include <google/protobuf/stubs/statusor.h>
  38. #include <google/protobuf/port_def.inc>
  39. namespace google {
  40. namespace protobuf {
  41. namespace util {
  42. namespace converter {
  43. class ProtoWriter;
  44. // Container for a single piece of data together with its data type.
  45. //
  46. // For primitive types (int32, int64, uint32, uint64, double, float, bool),
  47. // the data is stored by value.
  48. //
  49. // For string, a StringPiece is stored. For Cord, a pointer to Cord is stored.
  50. // Just like StringPiece, the DataPiece class does not own the storage for
  51. // the actual string or Cord, so it is the user's responsiblity to guarantee
  52. // that the underlying storage is still valid when the DataPiece is accessed.
  53. class PROTOBUF_EXPORT DataPiece {
  54. public:
  55. // Identifies data type of the value.
  56. // These are the types supported by DataPiece.
  57. enum Type {
  58. TYPE_INT32 = 1,
  59. TYPE_INT64 = 2,
  60. TYPE_UINT32 = 3,
  61. TYPE_UINT64 = 4,
  62. TYPE_DOUBLE = 5,
  63. TYPE_FLOAT = 6,
  64. TYPE_BOOL = 7,
  65. TYPE_ENUM = 8,
  66. TYPE_STRING = 9,
  67. TYPE_BYTES = 10,
  68. TYPE_NULL = 11, // explicit NULL type
  69. };
  70. // Constructors and Destructor
  71. explicit DataPiece(const int32 value)
  72. : type_(TYPE_INT32), i32_(value), use_strict_base64_decoding_(false) {}
  73. explicit DataPiece(const int64 value)
  74. : type_(TYPE_INT64), i64_(value), use_strict_base64_decoding_(false) {}
  75. explicit DataPiece(const uint32 value)
  76. : type_(TYPE_UINT32), u32_(value), use_strict_base64_decoding_(false) {}
  77. explicit DataPiece(const uint64 value)
  78. : type_(TYPE_UINT64), u64_(value), use_strict_base64_decoding_(false) {}
  79. explicit DataPiece(const double value)
  80. : type_(TYPE_DOUBLE),
  81. double_(value),
  82. use_strict_base64_decoding_(false) {}
  83. explicit DataPiece(const float value)
  84. : type_(TYPE_FLOAT), float_(value), use_strict_base64_decoding_(false) {}
  85. explicit DataPiece(const bool value)
  86. : type_(TYPE_BOOL), bool_(value), use_strict_base64_decoding_(false) {}
  87. DataPiece(StringPiece value, bool use_strict_base64_decoding)
  88. : type_(TYPE_STRING),
  89. str_(StringPiecePod::CreateFromStringPiece(value)),
  90. use_strict_base64_decoding_(use_strict_base64_decoding) {}
  91. // Constructor for bytes. The second parameter is not used.
  92. DataPiece(StringPiece value, bool dummy, bool use_strict_base64_decoding)
  93. : type_(TYPE_BYTES),
  94. str_(StringPiecePod::CreateFromStringPiece(value)),
  95. use_strict_base64_decoding_(use_strict_base64_decoding) {}
  96. DataPiece(const DataPiece& r) : type_(r.type_) { InternalCopy(r); }
  97. DataPiece& operator=(const DataPiece& x) {
  98. InternalCopy(x);
  99. return *this;
  100. }
  101. static DataPiece NullData() { return DataPiece(TYPE_NULL, 0); }
  102. virtual ~DataPiece() {
  103. }
  104. // Accessors
  105. Type type() const { return type_; }
  106. bool use_strict_base64_decoding() { return use_strict_base64_decoding_; }
  107. StringPiece str() const {
  108. GOOGLE_LOG_IF(DFATAL, type_ != TYPE_STRING) << "Not a string type.";
  109. return str_;
  110. }
  111. // Parses, casts or converts the value stored in the DataPiece into an int32.
  112. util::StatusOr<int32> ToInt32() const;
  113. // Parses, casts or converts the value stored in the DataPiece into a uint32.
  114. util::StatusOr<uint32> ToUint32() const;
  115. // Parses, casts or converts the value stored in the DataPiece into an int64.
  116. util::StatusOr<int64> ToInt64() const;
  117. // Parses, casts or converts the value stored in the DataPiece into a uint64.
  118. util::StatusOr<uint64> ToUint64() const;
  119. // Parses, casts or converts the value stored in the DataPiece into a double.
  120. util::StatusOr<double> ToDouble() const;
  121. // Parses, casts or converts the value stored in the DataPiece into a float.
  122. util::StatusOr<float> ToFloat() const;
  123. // Parses, casts or converts the value stored in the DataPiece into a bool.
  124. util::StatusOr<bool> ToBool() const;
  125. // Parses, casts or converts the value stored in the DataPiece into a string.
  126. util::StatusOr<std::string> ToString() const;
  127. // Tries to convert the value contained in this datapiece to string. If the
  128. // conversion fails, it returns the default_string.
  129. std::string ValueAsStringOrDefault(StringPiece default_string) const;
  130. util::StatusOr<std::string> ToBytes() const;
  131. private:
  132. friend class ProtoWriter;
  133. // Disallow implicit constructor.
  134. DataPiece();
  135. // Helper to create NULL or ENUM types.
  136. DataPiece(Type type, int32 val)
  137. : type_(type), i32_(val), use_strict_base64_decoding_(false) {}
  138. // Same as the ToEnum() method above but with additional flag to ignore
  139. // unknown enum values.
  140. util::StatusOr<int> ToEnum(const google::protobuf::Enum* enum_type,
  141. bool use_lower_camel_for_enums,
  142. bool case_insensitive_enum_parsing,
  143. bool ignore_unknown_enum_values,
  144. bool* is_unknown_enum_value) const;
  145. // For numeric conversion between
  146. // int32, int64, uint32, uint64, double, float and bool
  147. template <typename To>
  148. util::StatusOr<To> GenericConvert() const;
  149. // For conversion from string to
  150. // int32, int64, uint32, uint64, double, float and bool
  151. template <typename To>
  152. util::StatusOr<To> StringToNumber(bool (*func)(StringPiece,
  153. To*)) const;
  154. // Decodes a base64 string. Returns true on success.
  155. bool DecodeBase64(StringPiece src, std::string* dest) const;
  156. // Helper function to initialize this DataPiece with 'other'.
  157. void InternalCopy(const DataPiece& other);
  158. // Data type for this piece of data.
  159. Type type_;
  160. typedef ::google::protobuf::internal::StringPiecePod StringPiecePod;
  161. // Stored piece of data.
  162. union {
  163. int32 i32_;
  164. int64 i64_;
  165. uint32 u32_;
  166. uint64 u64_;
  167. double double_;
  168. float float_;
  169. bool bool_;
  170. StringPiecePod str_;
  171. };
  172. // Uses a stricter version of base64 decoding for byte fields.
  173. bool use_strict_base64_decoding_;
  174. };
  175. } // namespace converter
  176. } // namespace util
  177. } // namespace protobuf
  178. } // namespace google
  179. #include <google/protobuf/port_undef.inc>
  180. #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_DATAPIECE_H__