protobuf/src/google/protobuf/util/internal/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 <cstdint>
35 #include <string>
36 
37 #include <google/protobuf/stubs/common.h>
38 #include <google/protobuf/stubs/logging.h>
39 #include <google/protobuf/type.pb.h>
40 #include <google/protobuf/stubs/statusor.h>
41 #include <google/protobuf/stubs/strutil.h>
42 
43 #include <google/protobuf/port_def.inc>
44 
45 namespace google {
46 namespace protobuf {
47 namespace util {
48 namespace converter {
49 class ProtoWriter;
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 responsibility to guarantee
59 // that the underlying storage is still valid when the DataPiece is accessed.
60 class PROTOBUF_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_t value)
80  : type_(TYPE_INT32), i32_(value), use_strict_base64_decoding_(false) {}
81  explicit DataPiece(const int64_t value)
82  : type_(TYPE_INT64), i64_(value), use_strict_base64_decoding_(false) {}
83  explicit DataPiece(const uint32_t value)
84  : type_(TYPE_UINT32), u32_(value), use_strict_base64_decoding_(false) {}
85  explicit DataPiece(const uint64_t 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)
96  : type_(TYPE_STRING),
97  str_(value),
98  use_strict_base64_decoding_(use_strict_base64_decoding) {}
99  // Constructor for bytes. The second parameter is not used.
100  DataPiece(StringPiece value, bool /*dummy*/, bool use_strict_base64_decoding)
101  : type_(TYPE_BYTES),
102  str_(value),
103  use_strict_base64_decoding_(use_strict_base64_decoding) {}
104 
105  DataPiece(const DataPiece& r) : type_(r.type_) { InternalCopy(r); }
106 
108  InternalCopy(x);
109  return *this;
110  }
111 
112  static DataPiece NullData() { return DataPiece(TYPE_NULL, 0); }
113 
114  virtual ~DataPiece() {
115  }
116 
117  // Accessors
118  Type type() const { return type_; }
119 
120  bool use_strict_base64_decoding() { return use_strict_base64_decoding_; }
121 
122  StringPiece str() const {
123  GOOGLE_LOG_IF(DFATAL, type_ != TYPE_STRING) << "Not a string type.";
124  return str_;
125  }
126 
127 
128  // Parses, casts or converts the value stored in the DataPiece into an int32.
129  util::StatusOr<int32_t> ToInt32() const;
130 
131  // Parses, casts or converts the value stored in the DataPiece into a uint32.
132  util::StatusOr<uint32_t> ToUint32() const;
133 
134  // Parses, casts or converts the value stored in the DataPiece into an int64.
136 
137  // Parses, casts or converts the value stored in the DataPiece into a uint64.
138  util::StatusOr<uint64_t> ToUint64() const;
139 
140  // Parses, casts or converts the value stored in the DataPiece into a double.
141  util::StatusOr<double> ToDouble() const;
142 
143  // Parses, casts or converts the value stored in the DataPiece into a float.
144  util::StatusOr<float> ToFloat() const;
145 
146  // Parses, casts or converts the value stored in the DataPiece into a bool.
147  util::StatusOr<bool> ToBool() const;
148 
149  // Parses, casts or converts the value stored in the DataPiece into a string.
151 
152  // Tries to convert the value contained in this datapiece to string. If the
153  // conversion fails, it returns the default_string.
154  std::string ValueAsStringOrDefault(StringPiece default_string) const;
155 
156  util::StatusOr<std::string> ToBytes() const;
157 
158  private:
159  friend class ProtoWriter;
160 
161  // Disallow implicit constructor.
162  DataPiece();
163 
164  // Helper to create NULL or ENUM types.
166  : type_(type), i32_(val), use_strict_base64_decoding_(false) {}
167 
168  // Same as the ToEnum() method above but with additional flag to ignore
169  // unknown enum values.
171  bool use_lower_camel_for_enums,
172  bool case_insensitive_enum_parsing,
173  bool ignore_unknown_enum_values,
174  bool* is_unknown_enum_value) const;
175 
176  // For numeric conversion between
177  // int32, int64, uint32, uint64, double, float and bool
178  template <typename To>
179  util::StatusOr<To> GenericConvert() const;
180 
181  // For conversion from string to
182  // int32, int64, uint32, uint64, double, float and bool
183  template <typename To>
184  util::StatusOr<To> StringToNumber(bool (*func)(StringPiece, To*)) const;
185 
186  // Decodes a base64 string. Returns true on success.
187  bool DecodeBase64(StringPiece src, std::string* dest) const;
188 
189  // Helper function to initialize this DataPiece with 'other'.
190  void InternalCopy(const DataPiece& other);
191 
192  // Data type for this piece of data.
193  Type type_;
194 
195  // Stored piece of data.
196  union {
201  double double_;
202  float float_;
203  bool bool_;
205  };
206 
207  // Uses a stricter version of base64 decoding for byte fields.
208  bool use_strict_base64_decoding_;
209 };
210 
211 } // namespace converter
212 } // namespace util
213 } // namespace protobuf
214 } // namespace google
215 
216 #include <google/protobuf/port_undef.inc>
217 
218 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_DATAPIECE_H__
google::protobuf::util::converter::DataPiece::str
StringPiece str() const
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:122
google::protobuf::util::converter::DataPiece::operator=
DataPiece & operator=(const DataPiece &x)
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:107
google::protobuf::util::converter::DataPiece::DataPiece
DataPiece(const int32_t value)
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:79
google::protobuf::value
const Descriptor::ReservedRange value
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1954
google::protobuf::Enum
false
#define false
Definition: setup_once.h:323
google::protobuf::util::converter::DataPiece::i32_
int32_t i32_
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:197
google::protobuf::util::converter::DataPiece::DataPiece
DataPiece(const double value)
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:87
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
type_
std::string type_
Definition: client_channel_stress_test.cc:212
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
make_cmakelists.converter
converter
Definition: make_cmakelists.py:317
google::protobuf::util::converter::DataPiece::DataPiece
DataPiece(const float value)
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:91
default_string
static upb_StringView default_string(upb_ToProto_Context *ctx, const upb_FieldDef *f)
Definition: def_to_proto.c:124
google::protobuf::util::converter::DataPiece::u64_
uint64_t u64_
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:200
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
google::protobuf::util::converter::DataPiece::NullData
static DataPiece NullData()
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:112
google::protobuf::util::converter::DataPiece::DataPiece
DataPiece(Type type, int32_t val)
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:165
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
ToString
std::string ToString(const grpc::string_ref &r)
Definition: string_ref_helper.cc:24
Type
Definition: bloaty/third_party/protobuf/src/google/protobuf/type.pb.h:182
google::protobuf::StringPiece
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/stringpiece.h:180
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
google::protobuf::util::converter::DataPiece::u32_
uint32_t u32_
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:199
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
google::protobuf::util::converter::DataPiece::DataPiece
DataPiece(const bool value)
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:93
google::protobuf::util::converter::DataPiece::DataPiece
DataPiece(StringPiece value, bool, bool use_strict_base64_decoding)
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:100
tests.qps.qps_worker.dest
dest
Definition: qps_worker.py:45
google::protobuf::util::converter::ProtoWriter
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/proto_writer.h:67
google::protobuf::util::StatusOr
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/statusor.h:99
google::protobuf::util::converter::DataPiece::~DataPiece
virtual ~DataPiece()
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:114
absl::time_internal::ToInt64
int64_t ToInt64(Duration d, Ratio)
Definition: third_party/abseil-cpp/absl/time/time.h:1492
google::protobuf::util::converter::DataPiece::DataPiece
DataPiece(const DataPiece &r)
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:105
google::protobuf::util::converter::DataPiece::DataPiece
DataPiece(StringPiece value, bool use_strict_base64_decoding)
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:95
BSSL_NAMESPACE_BEGIN::DecodeBase64
static bool DecodeBase64(std::vector< uint8_t > *out, const char *in)
Definition: ssl_test.cc:795
func
const EVP_CIPHER *(* func)(void)
Definition: cipher_extra.c:73
google::protobuf::util::converter::DataPiece::str_
StringPiece str_
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:204
google::protobuf::util::converter::DataPiece::type
Type type() const
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:118
fix_build_deps.r
r
Definition: fix_build_deps.py:491
google::protobuf::util::converter::DataPiece::DataPiece
DataPiece(const int64_t value)
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:81
GOOGLE_LOG_IF
#define GOOGLE_LOG_IF(LEVEL, CONDITION)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:150
google::protobuf::util::converter::DataPiece::DataPiece
DataPiece(const uint64_t value)
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:85
google::protobuf::util::converter::DataPiece::i64_
int64_t i64_
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:198
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
str_
std::string str_
Definition: client_lb_end2end_test.cc:2368
google::protobuf::util::converter::DataPiece
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/datapiece.h:59
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
phone_pb2.enum_type
enum_type
Definition: phone_pb2.py:198
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
google::protobuf::util::converter::DataPiece::DataPiece
DataPiece(const uint32_t value)
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:83
google::protobuf::util::converter::DataPiece::use_strict_base64_decoding
bool use_strict_base64_decoding()
Definition: protobuf/src/google/protobuf/util/internal/datapiece.h:120


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:08