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