| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 TYPE_DOUBLE = 5, | 69 TYPE_DOUBLE = 5, |
| 70 TYPE_FLOAT = 6, | 70 TYPE_FLOAT = 6, |
| 71 TYPE_BOOL = 7, | 71 TYPE_BOOL = 7, |
| 72 TYPE_ENUM = 8, | 72 TYPE_ENUM = 8, |
| 73 TYPE_STRING = 9, | 73 TYPE_STRING = 9, |
| 74 TYPE_BYTES = 10, | 74 TYPE_BYTES = 10, |
| 75 TYPE_NULL = 11, // explicit NULL type | 75 TYPE_NULL = 11, // explicit NULL type |
| 76 }; | 76 }; |
| 77 | 77 |
| 78 // Constructors and Destructor | 78 // Constructors and Destructor |
| 79 explicit DataPiece(const int32 value) : type_(TYPE_INT32), i32_(value) {} | 79 explicit DataPiece(const int32 value) |
| 80 explicit DataPiece(const int64 value) : type_(TYPE_INT64), i64_(value) {} | 80 : type_(TYPE_INT32), i32_(value), use_strict_base64_decoding_(false) {} |
| 81 explicit DataPiece(const uint32 value) : type_(TYPE_UINT32), u32_(value) {} | 81 explicit DataPiece(const int64 value) |
| 82 explicit DataPiece(const uint64 value) : type_(TYPE_UINT64), u64_(value) {} | 82 : type_(TYPE_INT64), i64_(value), use_strict_base64_decoding_(false) {} |
| 83 explicit DataPiece(const double value) : type_(TYPE_DOUBLE), double_(value) {} | 83 explicit DataPiece(const uint32 value) |
| 84 explicit DataPiece(const float value) : type_(TYPE_FLOAT), float_(value) {} | 84 : type_(TYPE_UINT32), u32_(value), use_strict_base64_decoding_(false) {} |
| 85 explicit DataPiece(const bool value) : type_(TYPE_BOOL), bool_(value) {} | 85 explicit DataPiece(const uint64 value) |
| 86 : type_(TYPE_UINT64), u64_(value), use_strict_base64_decoding_(false) {} |
| 87 explicit DataPiece(const double value) |
| 88 : type_(TYPE_DOUBLE), |
| 89 double_(value), |
| 90 use_strict_base64_decoding_(false) {} |
| 91 explicit DataPiece(const float value) |
| 92 : type_(TYPE_FLOAT), float_(value), use_strict_base64_decoding_(false) {} |
| 93 explicit DataPiece(const bool value) |
| 94 : type_(TYPE_BOOL), bool_(value), use_strict_base64_decoding_(false) {} |
| 86 DataPiece(StringPiece value, bool use_strict_base64_decoding) | 95 DataPiece(StringPiece value, bool use_strict_base64_decoding) |
| 87 : type_(TYPE_STRING), | 96 : type_(TYPE_STRING), |
| 88 str_(StringPiecePod::CreateFromStringPiece(value)), | 97 str_(StringPiecePod::CreateFromStringPiece(value)), |
| 89 use_strict_base64_decoding_(use_strict_base64_decoding) {} | 98 use_strict_base64_decoding_(use_strict_base64_decoding) {} |
| 90 // Constructor for bytes. The second parameter is not used. | 99 // Constructor for bytes. The second parameter is not used. |
| 91 DataPiece(StringPiece value, bool dummy, bool use_strict_base64_decoding) | 100 DataPiece(StringPiece value, bool dummy, bool use_strict_base64_decoding) |
| 92 : type_(TYPE_BYTES), | 101 : type_(TYPE_BYTES), |
| 93 str_(StringPiecePod::CreateFromStringPiece(value)), | 102 str_(StringPiecePod::CreateFromStringPiece(value)), |
| 94 use_strict_base64_decoding_(use_strict_base64_decoding) {} | 103 use_strict_base64_decoding_(use_strict_base64_decoding) {} |
| 95 DataPiece(const DataPiece& r) : type_(r.type_), str_(r.str_) {} | 104 |
| 105 DataPiece(const DataPiece& r) : type_(r.type_) { InternalCopy(r); } |
| 106 |
| 96 DataPiece& operator=(const DataPiece& x) { | 107 DataPiece& operator=(const DataPiece& x) { |
| 97 type_ = x.type_; | 108 InternalCopy(x); |
| 98 str_ = x.str_; | |
| 99 return *this; | 109 return *this; |
| 100 } | 110 } |
| 101 | 111 |
| 102 static DataPiece NullData() { return DataPiece(TYPE_NULL, 0); } | 112 static DataPiece NullData() { return DataPiece(TYPE_NULL, 0); } |
| 103 | 113 |
| 104 virtual ~DataPiece() { | 114 virtual ~DataPiece() { |
| 105 } | 115 } |
| 106 | 116 |
| 107 // Accessors | 117 // Accessors |
| 108 Type type() const { return type_; } | 118 Type type() const { return type_; } |
| 109 | 119 |
| 120 bool use_strict_base64_decoding() { return use_strict_base64_decoding_; } |
| 121 |
| 110 StringPiece str() const { | 122 StringPiece str() const { |
| 111 GOOGLE_LOG_IF(DFATAL, type_ != TYPE_STRING) << "Not a string type."; | 123 GOOGLE_LOG_IF(DFATAL, type_ != TYPE_STRING) << "Not a string type."; |
| 112 return str_; | 124 return str_; |
| 113 } | 125 } |
| 114 | 126 |
| 115 | 127 |
| 116 // Parses, casts or converts the value stored in the DataPiece into an int32. | 128 // Parses, casts or converts the value stored in the DataPiece into an int32. |
| 117 util::StatusOr<int32> ToInt32() const; | 129 util::StatusOr<int32> ToInt32() const; |
| 118 | 130 |
| 119 // Parses, casts or converts the value stored in the DataPiece into a uint32. | 131 // Parses, casts or converts the value stored in the DataPiece into a uint32. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 140 // Tries to convert the value contained in this datapiece to string. If the | 152 // Tries to convert the value contained in this datapiece to string. If the |
| 141 // conversion fails, it returns the default_string. | 153 // conversion fails, it returns the default_string. |
| 142 string ValueAsStringOrDefault(StringPiece default_string) const; | 154 string ValueAsStringOrDefault(StringPiece default_string) const; |
| 143 | 155 |
| 144 util::StatusOr<string> ToBytes() const; | 156 util::StatusOr<string> ToBytes() const; |
| 145 | 157 |
| 146 // Converts a value into protocol buffer enum number. If the value is a | 158 // Converts a value into protocol buffer enum number. If the value is a |
| 147 // string, first attempts conversion by name, trying names as follows: | 159 // string, first attempts conversion by name, trying names as follows: |
| 148 // 1) the directly provided string value. | 160 // 1) the directly provided string value. |
| 149 // 2) the value upper-cased and replacing '-' by '_' | 161 // 2) the value upper-cased and replacing '-' by '_' |
| 162 // 3) if use_lower_camel_for_enums is true it also attempts by comparing |
| 163 // enum name without underscore with the value upper cased above. |
| 150 // If the value is not a string, attempts to convert to a 32-bit integer. | 164 // If the value is not a string, attempts to convert to a 32-bit integer. |
| 151 // If none of these succeeds, returns a conversion error status. | 165 // If none of these succeeds, returns a conversion error status. |
| 152 util::StatusOr<int> ToEnum(const google::protobuf::Enum* enum_type) const; | 166 util::StatusOr<int> ToEnum(const google::protobuf::Enum* enum_type, |
| 167 bool use_lower_camel_for_enums) const; |
| 153 | 168 |
| 154 private: | 169 private: |
| 155 // Disallow implicit constructor. | 170 // Disallow implicit constructor. |
| 156 DataPiece(); | 171 DataPiece(); |
| 157 | 172 |
| 158 // Helper to create NULL or ENUM types. | 173 // Helper to create NULL or ENUM types. |
| 159 DataPiece(Type type, int32 val) : type_(type), i32_(val) {} | 174 DataPiece(Type type, int32 val) |
| 175 : type_(type), i32_(val), use_strict_base64_decoding_(false) {} |
| 160 | 176 |
| 161 // For numeric conversion between | 177 // For numeric conversion between |
| 162 // int32, int64, uint32, uint64, double, float and bool | 178 // int32, int64, uint32, uint64, double, float and bool |
| 163 template <typename To> | 179 template <typename To> |
| 164 util::StatusOr<To> GenericConvert() const; | 180 util::StatusOr<To> GenericConvert() const; |
| 165 | 181 |
| 166 // For conversion from string to | 182 // For conversion from string to |
| 167 // int32, int64, uint32, uint64, double, float and bool | 183 // int32, int64, uint32, uint64, double, float and bool |
| 168 template <typename To> | 184 template <typename To> |
| 169 util::StatusOr<To> StringToNumber(bool (*func)(StringPiece, To*)) const; | 185 util::StatusOr<To> StringToNumber(bool (*func)(StringPiece, To*)) const; |
| 170 | 186 |
| 171 // Decodes a base64 string. Returns true on success. | 187 // Decodes a base64 string. Returns true on success. |
| 172 bool DecodeBase64(StringPiece src, string* dest) const; | 188 bool DecodeBase64(StringPiece src, string* dest) const; |
| 173 | 189 |
| 190 // Helper function to initialize this DataPiece with 'other'. |
| 191 void InternalCopy(const DataPiece& other); |
| 192 |
| 174 // Data type for this piece of data. | 193 // Data type for this piece of data. |
| 175 Type type_; | 194 Type type_; |
| 176 | 195 |
| 177 typedef ::google::protobuf::internal::StringPiecePod StringPiecePod; | 196 typedef ::google::protobuf::internal::StringPiecePod StringPiecePod; |
| 178 | 197 |
| 179 // Stored piece of data. | 198 // Stored piece of data. |
| 180 union { | 199 union { |
| 181 int32 i32_; | 200 int32 i32_; |
| 182 int64 i64_; | 201 int64 i64_; |
| 183 uint32 u32_; | 202 uint32 u32_; |
| 184 uint64 u64_; | 203 uint64 u64_; |
| 185 double double_; | 204 double double_; |
| 186 float float_; | 205 float float_; |
| 187 bool bool_; | 206 bool bool_; |
| 188 StringPiecePod str_; | 207 StringPiecePod str_; |
| 189 }; | 208 }; |
| 190 | 209 |
| 191 // Uses a stricter version of base64 decoding for byte fields. | 210 // Uses a stricter version of base64 decoding for byte fields. |
| 192 bool use_strict_base64_decoding_; | 211 bool use_strict_base64_decoding_; |
| 193 }; | 212 }; |
| 194 | 213 |
| 195 } // namespace converter | 214 } // namespace converter |
| 196 } // namespace util | 215 } // namespace util |
| 197 } // namespace protobuf | 216 } // namespace protobuf |
| 198 | 217 |
| 199 } // namespace google | 218 } // namespace google |
| 200 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_DATAPIECE_H__ | 219 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_DATAPIECE_H__ |
| OLD | NEW |