• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) {}
DataPiece(const DataPiece & r)95   DataPiece(const DataPiece& r) : type_(r.type_), str_(r.str_) {}
96   DataPiece& operator=(const DataPiece& x) {
97     type_ = x.type_;
98     str_ = x.str_;
99     return *this;
100   }
101 
NullData()102   static DataPiece NullData() { return DataPiece(TYPE_NULL, 0); }
103 
~DataPiece()104   virtual ~DataPiece() {
105   }
106 
107   // Accessors
type()108   Type type() const { return type_; }
109 
str()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.
138   util::StatusOr<string> ToString() const;
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.
152   util::StatusOr<int> ToEnum(const google::protobuf::Enum* enum_type) const;
153 
154  private:
155   // Disallow implicit constructor.
156   DataPiece();
157 
158   // Helper to create NULL or ENUM types.
DataPiece(Type type,int32 val)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 {
181     int32 i32_;
182     int64 i64_;
183     uint32 u32_;
184     uint64 u64_;
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__
201