unknown_field_set.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 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 //
35 // Contains classes used to keep track of unrecognized fields seen while
36 // parsing a protocol message.
37 
38 #ifndef GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__
39 #define GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__
40 
41 #include <assert.h>
42 #include <string>
43 #include <vector>
49 #include <google/protobuf/port.h>
50 
51 #include <google/protobuf/port_def.inc>
52 
53 #ifdef SWIG
54 #error "You cannot SWIG proto headers"
55 #endif
56 
57 namespace google {
58 namespace protobuf {
59 namespace io {
60 class CodedInputStream; // coded_stream.h
61 class CodedOutputStream; // coded_stream.h
62 class ZeroCopyInputStream; // zero_copy_stream.h
63 } // namespace io
64 namespace internal {
65 class InternalMetadataWithArena; // metadata.h
66 class WireFormat; // wire_format.h
67 class MessageSetFieldSkipperUsingCord;
68 // extension_set_heavy.cc
69 } // namespace internal
70 
71 class Message; // message.h
72 class UnknownField; // below
73 
74 // An UnknownFieldSet contains fields that were encountered while parsing a
75 // message but were not defined by its type. Keeping track of these can be
76 // useful, especially in that they may be written if the message is serialized
77 // again without being cleared in between. This means that software which
78 // simply receives messages and forwards them to other servers does not need
79 // to be updated every time a new field is added to the message definition.
80 //
81 // To get the UnknownFieldSet attached to any message, call
82 // Reflection::GetUnknownFields().
83 //
84 // This class is necessarily tied to the protocol buffer wire format, unlike
85 // the Reflection interface which is independent of any serialization scheme.
86 class PROTOBUF_EXPORT UnknownFieldSet {
87  public:
89  ~UnknownFieldSet();
90 
91  // Remove all fields.
92  inline void Clear();
93 
94  // Remove all fields and deallocate internal data objects
95  void ClearAndFreeMemory();
96 
97  // Is this set empty?
98  inline bool empty() const;
99 
100  // Merge the contents of some other UnknownFieldSet with this one.
101  void MergeFrom(const UnknownFieldSet& other);
102 
103  // Similar to above, but this function will destroy the contents of other.
104  void MergeFromAndDestroy(UnknownFieldSet* other);
105 
106  // Merge the contents an UnknownFieldSet with the UnknownFieldSet in
107  // *metadata, if there is one. If *metadata doesn't have an UnknownFieldSet
108  // then add one to it and make it be a copy of the first arg.
109  static void MergeToInternalMetdata(
110  const UnknownFieldSet& other,
112 
113  // Swaps the contents of some other UnknownFieldSet with this one.
114  inline void Swap(UnknownFieldSet* x);
115 
116  // Computes (an estimate of) the total number of bytes currently used for
117  // storing the unknown fields in memory. Does NOT include
118  // sizeof(*this) in the calculation.
119  size_t SpaceUsedExcludingSelfLong() const;
120 
122  return internal::ToIntSize(SpaceUsedExcludingSelfLong());
123  }
124 
125  // Version of SpaceUsed() including sizeof(*this).
126  size_t SpaceUsedLong() const;
127 
128  int SpaceUsed() const { return internal::ToIntSize(SpaceUsedLong()); }
129 
130  // Returns the number of fields present in the UnknownFieldSet.
131  inline int field_count() const;
132  // Get a field in the set, where 0 <= index < field_count(). The fields
133  // appear in the order in which they were added.
134  inline const UnknownField& field(int index) const;
135  // Get a mutable pointer to a field in the set, where
136  // 0 <= index < field_count(). The fields appear in the order in which
137  // they were added.
138  inline UnknownField* mutable_field(int index);
139 
140  // Adding fields ---------------------------------------------------
141 
142  void AddVarint(int number, uint64 value);
143  void AddFixed32(int number, uint32 value);
144  void AddFixed64(int number, uint64 value);
145  void AddLengthDelimited(int number, const std::string& value);
146  std::string* AddLengthDelimited(int number);
147  UnknownFieldSet* AddGroup(int number);
148 
149  // Adds an unknown field from another set.
150  void AddField(const UnknownField& field);
151 
152  // Delete fields with indices in the range [start .. start+num-1].
153  // Caution: implementation moves all fields with indices [start+num .. ].
154  void DeleteSubrange(int start, int num);
155 
156  // Delete all fields with a specific field number. The order of left fields
157  // is preserved.
158  // Caution: implementation moves all fields after the first deleted field.
159  void DeleteByNumber(int number);
160 
161  // Parsing helpers -------------------------------------------------
162  // These work exactly like the similarly-named methods of Message.
163 
164  bool MergeFromCodedStream(io::CodedInputStream* input);
165  bool ParseFromCodedStream(io::CodedInputStream* input);
166  bool ParseFromZeroCopyStream(io::ZeroCopyInputStream* input);
167  bool ParseFromArray(const void* data, int size);
168  inline bool ParseFromString(const std::string& data) {
169  return ParseFromArray(data.data(), static_cast<int>(data.size()));
170  }
171 
172  static const UnknownFieldSet* default_instance();
173 
174  private:
175  // For InternalMergeFrom
176  friend class UnknownField;
177  // Merges from other UnknownFieldSet. This method assumes, that this object
178  // is newly created and has no fields.
179  void InternalMergeFrom(const UnknownFieldSet& other);
180  void ClearFallback();
181 
182  std::vector<UnknownField> fields_;
184 };
185 
186 #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
187 namespace internal {
188 
189 inline void WriteVarint(uint32 num, uint64 val, UnknownFieldSet* unknown) {
190  unknown->AddVarint(num, val);
191 }
192 inline void WriteLengthDelimited(uint32 num, StringPiece val,
193  UnknownFieldSet* unknown) {
194  unknown->AddLengthDelimited(num)->assign(val.data(), val.size());
195 }
196 
197 PROTOBUF_EXPORT
198 const char* PackedEnumParser(void* object, const char* ptr, ParseContext* ctx,
199  bool (*is_valid)(int),
200  InternalMetadataWithArena* unknown, int field_num);
201 PROTOBUF_EXPORT
202 const char* PackedEnumParserArg(void* object, const char* ptr,
203  ParseContext* ctx,
204  bool (*is_valid)(const void*, int),
205  const void* data,
206  InternalMetadataWithArena* unknown,
207  int field_num);
208 
209 PROTOBUF_EXPORT
210 const char* UnknownGroupParse(UnknownFieldSet* unknown, const char* ptr,
211  ParseContext* ctx);
212 PROTOBUF_EXPORT
213 const char* UnknownFieldParse(uint64 tag, UnknownFieldSet* unknown,
214  const char* ptr, ParseContext* ctx);
215 PROTOBUF_EXPORT
216 const char* UnknownFieldParse(uint32 tag, InternalMetadataWithArena* metadata,
217  const char* ptr, ParseContext* ctx);
218 
219 } // namespace internal
220 #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
221 
222 // Represents one field in an UnknownFieldSet.
223 class PROTOBUF_EXPORT UnknownField {
224  public:
225  enum Type {
230  TYPE_GROUP
231  };
232 
233  // The field's field number, as seen on the wire.
234  inline int number() const;
235 
236  // The field type.
237  inline Type type() const;
238 
239  // Accessors -------------------------------------------------------
240  // Each method works only for UnknownFields of the corresponding type.
241 
242  inline uint64 varint() const;
243  inline uint32 fixed32() const;
244  inline uint64 fixed64() const;
245  inline const std::string& length_delimited() const;
246  inline const UnknownFieldSet& group() const;
247 
248  inline void set_varint(uint64 value);
249  inline void set_fixed32(uint32 value);
250  inline void set_fixed64(uint64 value);
251  inline void set_length_delimited(const std::string& value);
252  inline std::string* mutable_length_delimited();
253  inline UnknownFieldSet* mutable_group();
254 
255  // Serialization API.
256  // These methods can take advantage of the underlying implementation and may
257  // archieve a better performance than using getters to retrieve the data and
258  // do the serialization yourself.
259  void SerializeLengthDelimitedNoTag(io::CodedOutputStream* output) const;
260  uint8* SerializeLengthDelimitedNoTagToArray(uint8* target) const;
261 
262  inline size_t GetLengthDelimitedSize() const;
263 
264 
265  // If this UnknownField contains a pointer, delete it.
266  void Delete();
267 
268  // Make a deep copy of any pointers in this UnknownField.
269  void DeepCopy(const UnknownField& other);
270 
271  // Set the wire type of this UnknownField. Should only be used when this
272  // UnknownField is being created.
273  inline void SetType(Type type);
274 
277  };
278 
281  union {
285  mutable union LengthDelimited length_delimited_;
287  } data_;
288 };
289 
290 // ===================================================================
291 // inline implementations
292 
294 
296 
298 
299 inline void UnknownFieldSet::Clear() {
300  if (!fields_.empty()) {
301  ClearFallback();
302  }
303 }
304 
305 inline bool UnknownFieldSet::empty() const { return fields_.empty(); }
306 
308  fields_.swap(x->fields_);
309 }
310 
311 inline int UnknownFieldSet::field_count() const {
312  return static_cast<int>(fields_.size());
313 }
314 inline const UnknownField& UnknownFieldSet::field(int index) const {
315  return (fields_)[static_cast<size_t>(index)];
316 }
318  return &(fields_)[static_cast<size_t>(index)];
319 }
320 
322  const std::string& value) {
323  AddLengthDelimited(number)->assign(value);
324 }
325 
326 
327 
328 
329 inline int UnknownField::number() const { return static_cast<int>(number_); }
331  return static_cast<Type>(type_);
332 }
333 
334 inline uint64 UnknownField::varint() const {
335  assert(type() == TYPE_VARINT);
336  return data_.varint_;
337 }
339  assert(type() == TYPE_FIXED32);
340  return data_.fixed32_;
341 }
343  assert(type() == TYPE_FIXED64);
344  return data_.fixed64_;
345 }
347  assert(type() == TYPE_LENGTH_DELIMITED);
348  return *data_.length_delimited_.string_value;
349 }
350 inline const UnknownFieldSet& UnknownField::group() const {
351  assert(type() == TYPE_GROUP);
352  return *data_.group_;
353 }
354 
356  assert(type() == TYPE_VARINT);
357  data_.varint_ = value;
358 }
360  assert(type() == TYPE_FIXED32);
361  data_.fixed32_ = value;
362 }
364  assert(type() == TYPE_FIXED64);
365  data_.fixed64_ = value;
366 }
368  assert(type() == TYPE_LENGTH_DELIMITED);
369  data_.length_delimited_.string_value->assign(value);
370 }
372  assert(type() == TYPE_LENGTH_DELIMITED);
373  return data_.length_delimited_.string_value;
374 }
376  assert(type() == TYPE_GROUP);
377  return data_.group_;
378 }
379 
382  return data_.length_delimited_.string_value->size();
383 }
384 
386  type_ = type;
387 }
388 
389 
390 } // namespace protobuf
391 } // namespace google
392 
393 #include <google/protobuf/port_undef.inc>
394 #endif // GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__
google::protobuf::UnknownField::group
const UnknownFieldSet & group() const
Definition: unknown_field_set.h:350
google::protobuf::UnknownField::mutable_group
UnknownFieldSet * mutable_group()
Definition: unknown_field_set.h:375
google::protobuf::UnknownField::SetType
void SetType(Type type)
Definition: unknown_field_set.h:385
google::protobuf::UnknownField::TYPE_FIXED64
@ TYPE_FIXED64
Definition: unknown_field_set.h:228
data_
StringPiece data_
Definition: bytestream_unittest.cc:60
google::protobuf::value
const Descriptor::ReservedRange value
Definition: src/google/protobuf/descriptor.h:1954
google::protobuf::UnknownField::set_length_delimited
void set_length_delimited(const std::string &value)
Definition: unknown_field_set.h:367
google::protobuf::UnknownFieldSet::SpaceUsedExcludingSelf
int SpaceUsedExcludingSelf() const
Definition: unknown_field_set.h:121
google::protobuf::UnknownField::fixed32
uint32 fixed32() const
Definition: unknown_field_set.h:338
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
Definition: macros.h:40
google::protobuf.internal::InternalMetadataWithArena
Definition: metadata.h:52
google::protobuf::UnknownField::fixed32_
uint32 fixed32_
Definition: unknown_field_set.h:283
google::protobuf::UnknownFieldSet::ClearFallback
void ClearFallback()
Definition: unknown_field_set.cc:57
google::protobuf.internal.python_message.MergeFrom
MergeFrom
Definition: python_message.py:1340
google::protobuf::UnknownField::TYPE_FIXED32
@ TYPE_FIXED32
Definition: unknown_field_set.h:227
google::protobuf::UnknownField::length_delimited
const std::string & length_delimited() const
Definition: unknown_field_set.h:346
input
std::string input
Definition: tokenizer_unittest.cc:197
google::protobuf::uint8
uint8_t uint8
Definition: protobuf/src/google/protobuf/stubs/port.h:153
google::protobuf::UnknownField::TYPE_VARINT
@ TYPE_VARINT
Definition: unknown_field_set.h:226
google::protobuf::UnknownField::varint
uint64 varint() const
Definition: unknown_field_set.h:334
benchmarks.util.result_uploader.metadata
def metadata
Definition: result_uploader.py:97
google::protobuf::UnknownFieldSet::SpaceUsed
int SpaceUsed() const
Definition: unknown_field_set.h:128
google::protobuf::uint32
uint32_t uint32
Definition: protobuf/src/google/protobuf/stubs/port.h:155
google::protobuf::UnknownField
Definition: unknown_field_set.h:223
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
target
GLenum target
Definition: glcorearb.h:3739
x
GLint GLenum GLint x
Definition: glcorearb.h:2834
port.h
google::protobuf::UnknownFieldSet::Clear
void Clear()
Definition: unknown_field_set.h:299
google::protobuf::python::cmessage::UnknownFieldSet
static PyObject * UnknownFieldSet(CMessage *self)
Definition: python/google/protobuf/pyext/message.cc:2501
google::protobuf::UnknownField::type
Type type() const
Definition: unknown_field_set.h:330
google::uint32
uint32_t uint32
Definition: sdk/include/aditof/log.h:59
google::protobuf::UnknownField::data_
union google::protobuf::UnknownField::@164 data_
google::protobuf::UnknownFieldSet::ParseFromString
bool ParseFromString(const std::string &data)
Definition: unknown_field_set.h:168
google::protobuf::UnknownFieldSet::field_count
int field_count() const
Definition: unknown_field_set.h:311
google::protobuf::UnknownFieldSet::~UnknownFieldSet
~UnknownFieldSet()
Definition: unknown_field_set.h:295
google::protobuf::UnknownField::set_fixed32
void set_fixed32(uint32 value)
Definition: unknown_field_set.h:359
google::protobuf::UnknownField::LengthDelimited
Definition: unknown_field_set.h:275
parse_context.h
google::protobuf::UnknownFieldSet::UnknownFieldSet
UnknownFieldSet()
Definition: unknown_field_set.h:293
Type
Definition: type.pb.h:182
google::protobuf.internal::ToIntSize
int ToIntSize(size_t size)
Definition: message_lite.h:104
coded_stream.h
google::protobuf.internal::PackedEnumParserArg
const char * PackedEnumParserArg(void *object, const char *ptr, ParseContext *ctx, bool(*is_valid)(const void *, int), const void *data, InternalMetadataWithArenaLite *metadata, int field_num)
Definition: parse_context.cc:437
start
GLuint start
Definition: glcorearb.h:2858
google::protobuf.internal::WriteLengthDelimited
void WriteLengthDelimited(uint32 num, StringPiece val, std::string *s)
Definition: parse_context.cc:313
google::protobuf::UnknownFieldSet::AddVarint
void AddVarint(int number, uint64 value)
Definition: unknown_field_set.cc:134
google::protobuf.internal::UnknownFieldParse
const char * UnknownFieldParse(uint32 tag, std::string *unknown, const char *ptr, ParseContext *ctx)
Definition: parse_context.cc:545
google::protobuf::UnknownField::fixed64
uint64 fixed64() const
Definition: unknown_field_set.h:342
google::protobuf::uint64
uint64_t uint64
Definition: protobuf/src/google/protobuf/stubs/port.h:156
google::protobuf::UnknownField::set_fixed64
void set_fixed64(uint64 value)
Definition: unknown_field_set.h:363
google::protobuf::UnknownField::set_varint
void set_varint(uint64 value)
Definition: unknown_field_set.h:355
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
google::protobuf::io::CodedOutputStream
Definition: coded_stream.h:693
google::protobuf::UnknownFieldSet::AddLengthDelimited
void AddLengthDelimited(int number, const std::string &value)
Definition: unknown_field_set.h:321
google::protobuf::io::ZeroCopyInputStream
Definition: zero_copy_stream.h:126
google::protobuf::UnknownField::LengthDelimited::string_value
std::string * string_value
Definition: unknown_field_set.h:276
type
GLenum type
Definition: glcorearb.h:2695
google::protobuf::UnknownField::TYPE_GROUP
@ TYPE_GROUP
Definition: unknown_field_set.h:230
google::protobuf::UnknownField::group_
UnknownFieldSet * group_
Definition: unknown_field_set.h:286
google::protobuf::UnknownFieldSet::field
const UnknownField & field(int index) const
Definition: unknown_field_set.h:314
google::protobuf::UnknownField::varint_
uint64 varint_
Definition: unknown_field_set.h:282
google::protobuf::UnknownField::TYPE_LENGTH_DELIMITED
@ TYPE_LENGTH_DELIMITED
Definition: unknown_field_set.h:229
google::protobuf.internal.python_message.Clear
Clear
Definition: python_message.py:1431
google::protobuf.internal::PackedEnumParser
const char * PackedEnumParser(void *object, const char *ptr, ParseContext *ctx)
Definition: parse_context.cc:419
common.h
google::protobuf::python::cmessage::DeepCopy
PyObject * DeepCopy(CMessage *self, PyObject *arg)
Definition: python/google/protobuf/pyext/message.cc:2377
google::protobuf::UnknownFieldSet::empty
bool empty() const
Definition: unknown_field_set.h:305
size
GLsizeiptr size
Definition: glcorearb.h:2943
google::protobuf::UnknownFieldSet::mutable_field
UnknownField * mutable_field(int index)
Definition: unknown_field_set.h:317
google::protobuf::UnknownFieldSet::ClearAndFreeMemory
void ClearAndFreeMemory()
Definition: unknown_field_set.h:297
google::protobuf::UnknownFieldSet
Definition: unknown_field_set.h:86
google::protobuf::UnknownField::type_
uint32 type_
Definition: unknown_field_set.h:280
message_lite.h
google::protobuf::io::CodedInputStream
Definition: coded_stream.h:173
google::protobuf::descriptor_unittest::AddField
FieldDescriptorProto * AddField(DescriptorProto *parent, const std::string &name, int number, FieldDescriptorProto::Label label, FieldDescriptorProto::Type type)
Definition: descriptor_unittest.cc:109
logging.h
google::protobuf::UnknownField::mutable_length_delimited
std::string * mutable_length_delimited()
Definition: unknown_field_set.h:371
google::protobuf::UnknownField::fixed64_
uint64 fixed64_
Definition: unknown_field_set.h:284
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
internal
Definition: any.pb.h:40
val
GLuint GLfloat * val
Definition: glcorearb.h:3604
google::protobuf.internal::WriteVarint
void WriteVarint(uint64 val, std::string *s)
Definition: parse_context.cc:299
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
google::protobuf::UnknownField::number_
uint32 number_
Definition: unknown_field_set.h:279
assert.h
google::protobuf::UnknownField::number
int number() const
Definition: unknown_field_set.h:329
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
index
GLuint index
Definition: glcorearb.h:3055
google::protobuf::UnknownFieldSet::Swap
void Swap(UnknownFieldSet *x)
Definition: unknown_field_set.h:307
group
static uint32_t * group(tarjan *t, upb_refcounted *r)
Definition: ruby/ext/google/protobuf_c/upb.c:5943
google::protobuf::UnknownField::GetLengthDelimitedSize
size_t GetLengthDelimitedSize() const
Definition: unknown_field_set.h:380
number
double number
Definition: cJSON.h:326
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::UnknownFieldSet::fields_
std::vector< UnknownField > fields_
Definition: unknown_field_set.h:182
google::protobuf::UnknownField::Type
Type
Definition: unknown_field_set.h:225
GOOGLE_DCHECK_EQ
#define GOOGLE_DCHECK_EQ
Definition: logging.h:196


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:00