| OLD | NEW |
| (Empty) | |
| 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 |
| 79 explicit DataPiece(const int32 value) : type_(TYPE_INT32), i32_(value) {} |
| 80 explicit DataPiece(const int64 value) : type_(TYPE_INT64), i64_(value) {} |
| 81 explicit DataPiece(const uint32 value) : type_(TYPE_UINT32), u32_(value) {} |
| 82 explicit DataPiece(const uint64 value) : type_(TYPE_UINT64), u64_(value) {} |
| 83 explicit DataPiece(const double value) : type_(TYPE_DOUBLE), double_(value) {} |
| 84 explicit DataPiece(const float value) : type_(TYPE_FLOAT), float_(value) {} |
| 85 explicit DataPiece(const bool value) : type_(TYPE_BOOL), bool_(value) {} |
| 86 explicit DataPiece(StringPiece value) |
| 87 : type_(TYPE_STRING), |
| 88 str_(StringPiecePod::CreateFromStringPiece(value)) {} |
| 89 // Constructor for bytes. The second parameter is not used. |
| 90 explicit DataPiece(StringPiece value, bool dummy) |
| 91 : type_(TYPE_BYTES), str_(StringPiecePod::CreateFromStringPiece(value)) {} |
| 92 DataPiece(const DataPiece& r) : type_(r.type_), str_(r.str_) {} |
| 93 DataPiece& operator=(const DataPiece& x) { |
| 94 type_ = x.type_; |
| 95 str_ = x.str_; |
| 96 return *this; |
| 97 } |
| 98 |
| 99 static DataPiece NullData() { return DataPiece(TYPE_NULL, 0); } |
| 100 |
| 101 virtual ~DataPiece() { |
| 102 } |
| 103 |
| 104 // Accessors |
| 105 Type type() const { return type_; } |
| 106 |
| 107 StringPiece str() const { |
| 108 GOOGLE_LOG_IF(DFATAL, type_ != TYPE_STRING) << "Not a string type."; |
| 109 return str_; |
| 110 } |
| 111 |
| 112 |
| 113 // Parses, casts or converts the value stored in the DataPiece into an int32. |
| 114 util::StatusOr<int32> ToInt32() const; |
| 115 |
| 116 // Parses, casts or converts the value stored in the DataPiece into a uint32. |
| 117 util::StatusOr<uint32> ToUint32() const; |
| 118 |
| 119 // Parses, casts or converts the value stored in the DataPiece into an int64. |
| 120 util::StatusOr<int64> ToInt64() const; |
| 121 |
| 122 // Parses, casts or converts the value stored in the DataPiece into a uint64. |
| 123 util::StatusOr<uint64> ToUint64() const; |
| 124 |
| 125 // Parses, casts or converts the value stored in the DataPiece into a double. |
| 126 util::StatusOr<double> ToDouble() const; |
| 127 |
| 128 // Parses, casts or converts the value stored in the DataPiece into a float. |
| 129 util::StatusOr<float> ToFloat() const; |
| 130 |
| 131 // Parses, casts or converts the value stored in the DataPiece into a bool. |
| 132 util::StatusOr<bool> ToBool() const; |
| 133 |
| 134 // Parses, casts or converts the value stored in the DataPiece into a string. |
| 135 util::StatusOr<string> ToString() const; |
| 136 |
| 137 // Tries to convert the value contained in this datapiece to string. If the |
| 138 // conversion fails, it returns the default_string. |
| 139 string ValueAsStringOrDefault(StringPiece default_string) const; |
| 140 |
| 141 util::StatusOr<string> ToBytes() const; |
| 142 |
| 143 // Converts a value into protocol buffer enum number. If the value is a |
| 144 // string, first attempts conversion by name, trying names as follows: |
| 145 // 1) the directly provided string value. |
| 146 // 2) the value upper-cased and replacing '-' by '_' |
| 147 // If the value is not a string, attempts to convert to a 32-bit integer. |
| 148 // If none of these succeeds, returns a conversion error status. |
| 149 util::StatusOr<int> ToEnum(const google::protobuf::Enum* enum_type) const; |
| 150 |
| 151 private: |
| 152 // Disallow implicit constructor. |
| 153 DataPiece(); |
| 154 |
| 155 // Helper to create NULL or ENUM types. |
| 156 DataPiece(Type type, int32 val) : type_(type), i32_(val) {} |
| 157 |
| 158 // For numeric conversion between |
| 159 // int32, int64, uint32, uint64, double, float and bool |
| 160 template <typename To> |
| 161 util::StatusOr<To> GenericConvert() const; |
| 162 |
| 163 // For conversion from string to |
| 164 // int32, int64, uint32, uint64, double, float and bool |
| 165 template <typename To> |
| 166 util::StatusOr<To> StringToNumber(bool (*func)(StringPiece, To*)) const; |
| 167 |
| 168 // Data type for this piece of data. |
| 169 Type type_; |
| 170 |
| 171 // StringPiece is not a POD and can not be used in an union (pre C++11). We |
| 172 // need a POD version of it. |
| 173 struct StringPiecePod { |
| 174 const char* data; |
| 175 int size; |
| 176 |
| 177 // Create from a StringPiece. |
| 178 static StringPiecePod CreateFromStringPiece(StringPiece str) { |
| 179 StringPiecePod pod; |
| 180 pod.data = str.data(); |
| 181 pod.size = str.size(); |
| 182 return pod; |
| 183 } |
| 184 |
| 185 // Cast to StringPiece. |
| 186 operator StringPiece() const { return StringPiece(data, size); } |
| 187 |
| 188 bool operator==(const char* value) const { |
| 189 return StringPiece(data, size) == StringPiece(value); |
| 190 } |
| 191 |
| 192 string ToString() const { return string(data, size); } |
| 193 }; |
| 194 |
| 195 // Stored piece of data. |
| 196 union { |
| 197 int32 i32_; |
| 198 int64 i64_; |
| 199 uint32 u32_; |
| 200 uint64 u64_; |
| 201 double double_; |
| 202 float float_; |
| 203 bool bool_; |
| 204 StringPiecePod str_; |
| 205 }; |
| 206 }; |
| 207 |
| 208 } // namespace converter |
| 209 } // namespace util |
| 210 } // namespace protobuf |
| 211 |
| 212 } // namespace google |
| 213 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_DATAPIECE_H__ |
| OLD | NEW |