webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
datapiece.h
Go to the documentation of this file.
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 
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.
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  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_), str_(r.str_) {}
97  type_ = x.type_;
98  str_ = x.str_;
99  return *this;
100  }
101 
102  static DataPiece NullData() { return DataPiece(TYPE_NULL, 0); }
103 
104  virtual ~DataPiece() {
105  }
106 
107  // Accessors
108  Type type() const { return type_; }
109 
110  StringPiece str() const {
111  GOOGLE_LOG_IF(DFATAL, type_ != TYPE_STRING) << "Not a string type.";
112  return str_;
113  }
114 
115 
116  // Parses, casts or converts the value stored in the DataPiece into an int32.
117  util::StatusOr<int32> ToInt32() const;
118 
119  // Parses, casts or converts the value stored in the DataPiece into a uint32.
120  util::StatusOr<uint32> ToUint32() const;
121 
122  // Parses, casts or converts the value stored in the DataPiece into an int64.
123  util::StatusOr<int64> ToInt64() const;
124 
125  // Parses, casts or converts the value stored in the DataPiece into a uint64.
126  util::StatusOr<uint64> ToUint64() const;
127 
128  // Parses, casts or converts the value stored in the DataPiece into a double.
129  util::StatusOr<double> ToDouble() const;
130 
131  // Parses, casts or converts the value stored in the DataPiece into a float.
132  util::StatusOr<float> ToFloat() const;
133 
134  // Parses, casts or converts the value stored in the DataPiece into a bool.
135  util::StatusOr<bool> ToBool() const;
136 
137  // Parses, casts or converts the value stored in the DataPiece into a string.
139 
140  // Tries to convert the value contained in this datapiece to string. If the
141  // conversion fails, it returns the default_string.
142  string ValueAsStringOrDefault(StringPiece default_string) const;
143 
144  util::StatusOr<string> ToBytes() const;
145 
146  // Converts a value into protocol buffer enum number. If the value is a
147  // string, first attempts conversion by name, trying names as follows:
148  // 1) the directly provided string value.
149  // 2) the value upper-cased and replacing '-' by '_'
150  // 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.
153 
154  private:
155  // Disallow implicit constructor.
156  DataPiece();
157 
158  // Helper to create NULL or ENUM types.
159  DataPiece(Type type, int32 val) : type_(type), i32_(val) {}
160 
161  // For numeric conversion between
162  // int32, int64, uint32, uint64, double, float and bool
163  template <typename To>
164  util::StatusOr<To> GenericConvert() const;
165 
166  // For conversion from string to
167  // int32, int64, uint32, uint64, double, float and bool
168  template <typename To>
169  util::StatusOr<To> StringToNumber(bool (*func)(StringPiece, To*)) const;
170 
171  // Decodes a base64 string. Returns true on success.
172  bool DecodeBase64(StringPiece src, string* dest) const;
173 
174  // Data type for this piece of data.
175  Type type_;
176 
177  typedef ::google::protobuf::internal::StringPiecePod StringPiecePod;
178 
179  // Stored piece of data.
180  union {
185  double double_;
186  float float_;
187  bool bool_;
188  StringPiecePod str_;
189  };
190 
191  // Uses a stricter version of base64 decoding for byte fields.
192  bool use_strict_base64_decoding_;
193 };
194 
195 } // namespace converter
196 } // namespace util
197 } // namespace protobuf
198 
199 } // namespace google
200 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_DATAPIECE_H__
virtual ~DataPiece()
Definition: datapiece.h:104
Definition: util.py:1
std::integral_constant< bool, B > bool_
Definition: Brigand.h:836
DataPiece(const bool value)
Definition: datapiece.h:85
Definition: stringpiece.h:441
Definition: type.pb.h:533
dest
Definition: upload.py:394
DataPiece(const uint64 value)
Definition: datapiece.h:82
uint64 u64_
Definition: datapiece.h:184
int dummy
Definition: voe_standard_test.cc:35
StringPiecePod str_
Definition: datapiece.h:188
DataPiece(const DataPiece &r)
Definition: datapiece.h:95
float float_
Definition: datapiece.h:186
int32_t int32
Definition: port.h:130
Definition: stringpiece.h:178
static DataPiece NullData()
Definition: datapiece.h:102
EGLSurface EGLint x
Definition: eglext.h:950
EGLAttrib * value
Definition: eglext.h:120
GLenum func
Definition: gl2.h:481
enum_type
Definition: descriptor_pb2.py:1573
Type type() const
Definition: datapiece.h:108
uint32_t uint32
Definition: port.h:135
#define GOOGLE_LOG_IF(LEVEL, CONDITION)
Definition: logging.h:148
Definition: __init__.py:1
uint64_t uint64
Definition: port.h:136
bool bool_
Definition: datapiece.h:187
std::string ToString(const T &value)
Definition: angleutils.h:163
DataPiece & operator=(const DataPiece &x)
Definition: datapiece.h:96
The field type used for enum fields.
int64 i64_
Definition: datapiece.h:182
DataPiece(const uint32 value)
Definition: datapiece.h:81
uint32 u32_
Definition: datapiece.h:183
EGLenum type
Definition: eglext.h:63
GLenum src
Definition: gl2ext.h:304
DataPiece(const int64 value)
Definition: datapiece.h:80
DataPiece(StringPiece value, bool use_strict_base64_decoding)
Definition: datapiece.h:86
double double_
Definition: datapiece.h:185
DataPiece(StringPiece value, bool dummy, bool use_strict_base64_decoding)
Definition: datapiece.h:91
DataPiece(const double value)
Definition: datapiece.h:83
int64_t int64
Definition: port.h:131
DataPiece(const float value)
Definition: datapiece.h:84
DataPiece(const int32 value)
Definition: datapiece.h:79
Definition: statusor.h:97
#define LIBPROTOBUF_EXPORT
Definition: port.h:97
Definition: gflags_completions.h:115
int32 i32_
Definition: datapiece.h:181
StringPiece str() const
Definition: datapiece.h:110
GLuint GLsizei GLsizei GLfloat * val
Definition: gl2ext.h:3301
GLboolean r
Definition: gl2ext.h:306