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