datapiece.h 8.0 KB

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