unknown_field_set.cc
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 
36 
46 
47 #include <google/protobuf/port_def.inc>
48 
49 namespace google {
50 namespace protobuf {
51 
53  static auto instance = internal::OnShutdownDelete(new UnknownFieldSet());
54  return instance;
55 }
56 
58  GOOGLE_DCHECK(!fields_.empty());
59  int n = fields_.size();
60  do {
61  (fields_)[--n].Delete();
62  } while (n > 0);
63  fields_.clear();
64 }
65 
67  int other_field_count = other.field_count();
68  if (other_field_count > 0) {
69  fields_.reserve(fields_.size() + other_field_count);
70  for (int i = 0; i < other_field_count; i++) {
71  fields_.push_back((other.fields_)[i]);
72  fields_.back().DeepCopy((other.fields_)[i]);
73  }
74  }
75 }
76 
78  int other_field_count = other.field_count();
79  if (other_field_count > 0) {
80  fields_.reserve(fields_.size() + other_field_count);
81  for (int i = 0; i < other_field_count; i++) {
82  fields_.push_back((other.fields_)[i]);
83  fields_.back().DeepCopy((other.fields_)[i]);
84  }
85  }
86 }
87 
88 // A specialized MergeFrom for performance when we are merging from an UFS that
89 // is temporary and can be destroyed in the process.
91  if (fields_.empty()) {
92  fields_ = std::move(other->fields_);
93  } else {
94  fields_.insert(fields_.end(),
95  std::make_move_iterator(other->fields_.begin()),
96  std::make_move_iterator(other->fields_.end()));
97  }
98  other->fields_.clear();
99 }
100 
102  const UnknownFieldSet& other,
104  metadata->mutable_unknown_fields()->MergeFrom(other);
105 }
106 
108  if (fields_.empty()) return 0;
109 
110  size_t total_size = sizeof(fields_) + sizeof(UnknownField) * fields_.size();
111 
112  for (int i = 0; i < fields_.size(); i++) {
113  const UnknownField& field = (fields_)[i];
114  switch (field.type()) {
116  total_size += sizeof(*field.data_.length_delimited_.string_value) +
119  break;
121  total_size += field.data_.group_->SpaceUsedLong();
122  break;
123  default:
124  break;
125  }
126  }
127  return total_size;
128 }
129 
131  return sizeof(*this) + SpaceUsedExcludingSelf();
132 }
133 
136  field.number_ = number;
139  fields_.push_back(field);
140 }
141 
144  field.number_ = number;
147  fields_.push_back(field);
148 }
149 
152  field.number_ = number;
155  fields_.push_back(field);
156 }
157 
160  field.number_ = number;
163  fields_.push_back(field);
165 }
166 
167 
170  field.number_ = number;
173  fields_.push_back(field);
174  return field.data_.group_;
175 }
176 
178  fields_.push_back(field);
179  fields_.back().DeepCopy(field);
180 }
181 
183  // Delete the specified fields.
184  for (int i = 0; i < num; ++i) {
185  (fields_)[i + start].Delete();
186  }
187  // Slide down the remaining fields.
188  for (int i = start + num; i < fields_.size(); ++i) {
189  (fields_)[i - num] = (fields_)[i];
190  }
191  // Pop off the # of deleted fields.
192  for (int i = 0; i < num; ++i) {
193  fields_.pop_back();
194  }
195 }
196 
198  int left = 0; // The number of fields left after deletion.
199  for (int i = 0; i < fields_.size(); ++i) {
200  UnknownField* field = &(fields_)[i];
201  if (field->number() == number) {
202  field->Delete();
203  } else {
204  if (i != left) {
205  (fields_)[left] = (fields_)[i];
206  }
207  ++left;
208  }
209  }
210  fields_.resize(left);
211 }
212 
214  UnknownFieldSet other;
216  input->ConsumedEntireMessage()) {
217  MergeFromAndDestroy(&other);
218  return true;
219  } else {
220  return false;
221  }
222 }
223 
225  Clear();
226  return MergeFromCodedStream(input);
227 }
228 
230  io::CodedInputStream coded_input(input);
231  return (ParseFromCodedStream(&coded_input) &&
232  coded_input.ConsumedEntireMessage());
233 }
234 
235 bool UnknownFieldSet::ParseFromArray(const void* data, int size) {
238 }
239 
241  switch (type()) {
243  delete data_.length_delimited_.string_value;
244  break;
246  delete data_.group_;
247  break;
248  default:
249  break;
250  }
251 }
252 
254  switch (type()) {
256  data_.length_delimited_.string_value =
257  new std::string(*data_.length_delimited_.string_value);
258  break;
261  group->InternalMergeFrom(*data_.group_);
262  data_.group_ = group;
263  break;
264  }
265  default:
266  break;
267  }
268 }
269 
270 
272  io::CodedOutputStream* output) const {
274  const std::string& data = *data_.length_delimited_.string_value;
275  output->WriteVarint32(data.size());
276  output->WriteRawMaybeAliased(data.data(), data.size());
277 }
278 
281  const std::string& data = *data_.length_delimited_.string_value;
284  return target;
285 }
286 
287 #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
288 namespace internal {
289 const char* PackedEnumParser(void* object, const char* ptr, ParseContext* ctx,
290  bool (*is_valid)(int),
292  int field_num) {
293  return ctx->ReadPackedVarint(
294  ptr, [object, is_valid, metadata, field_num](uint64 val) {
295  if (is_valid(val)) {
296  static_cast<RepeatedField<int>*>(object)->Add(val);
297  } else {
298  WriteVarint(field_num, val, metadata->mutable_unknown_fields());
299  }
300  });
301 }
302 const char* PackedEnumParserArg(void* object, const char* ptr,
303  ParseContext* ctx,
304  bool (*is_valid)(const void*, int),
305  const void* data,
306  InternalMetadataWithArena* metadata,
307  int field_num) {
308  return ctx->ReadPackedVarint(
309  ptr, [object, is_valid, data, metadata, field_num](uint64 val) {
310  if (is_valid(data, val)) {
311  static_cast<RepeatedField<int>*>(object)->Add(val);
312  } else {
313  WriteVarint(field_num, val, metadata->mutable_unknown_fields());
314  }
315  });
316 }
317 
318 class UnknownFieldParserHelper {
319  public:
320  explicit UnknownFieldParserHelper(UnknownFieldSet* unknown)
321  : unknown_(unknown) {}
322 
323  void AddVarint(uint32 num, uint64 value) { unknown_->AddVarint(num, value); }
324  void AddFixed64(uint32 num, uint64 value) {
325  unknown_->AddFixed64(num, value);
326  }
327  const char* ParseLengthDelimited(uint32 num, const char* ptr,
328  ParseContext* ctx) {
329  std::string* s = unknown_->AddLengthDelimited(num);
330  int size = ReadSize(&ptr);
332  return ctx->ReadString(ptr, size, s);
333  }
334  const char* ParseGroup(uint32 num, const char* ptr, ParseContext* ctx) {
335  UnknownFieldParserHelper child(unknown_->AddGroup(num));
336  return ctx->ParseGroup(&child, ptr, num * 8 + 3);
337  }
338  void AddFixed32(uint32 num, uint32 value) {
339  unknown_->AddFixed32(num, value);
340  }
341 
342  const char* _InternalParse(const char* ptr, ParseContext* ctx) {
343  return WireFormatParser(*this, ptr, ctx);
344  }
345 
346  private:
347  UnknownFieldSet* unknown_;
348 };
349 
350 const char* UnknownGroupParse(UnknownFieldSet* unknown, const char* ptr,
351  ParseContext* ctx) {
352  UnknownFieldParserHelper field_parser(unknown);
353  return WireFormatParser(field_parser, ptr, ctx);
354 }
355 
356 const char* UnknownFieldParse(uint64 tag, UnknownFieldSet* unknown,
357  const char* ptr, ParseContext* ctx) {
358  UnknownFieldParserHelper field_parser(unknown);
359  return FieldParser(tag, field_parser, ptr, ctx);
360 }
361 
362 const char* UnknownFieldParse(uint32 tag, InternalMetadataWithArena* metadata,
363  const char* ptr, ParseContext* ctx) {
364  return UnknownFieldParse(tag, metadata->mutable_unknown_fields(), ptr, ctx);
365 }
366 
367 } // namespace internal
368 #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
369 
370 } // namespace protobuf
371 } // namespace google
google::protobuf::UnknownField::group
const UnknownFieldSet & group() const
Definition: unknown_field_set.h:350
zero_copy_stream.h
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
google::protobuf::value
const Descriptor::ReservedRange value
Definition: src/google/protobuf/descriptor.h:1954
google::protobuf::UnknownFieldSet::SpaceUsedExcludingSelf
int SpaceUsedExcludingSelf() const
Definition: unknown_field_set.h:121
zero_copy_stream_impl.h
google::protobuf.internal::InternalMetadataWithArena
Definition: metadata.h:52
google::protobuf::UnknownField::SerializeLengthDelimitedNoTag
void SerializeLengthDelimitedNoTag(io::CodedOutputStream *output) const
Definition: unknown_field_set.cc:271
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::UnknownField::TYPE_FIXED32
@ TYPE_FIXED32
Definition: unknown_field_set.h:227
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::UnknownFieldSet::MergeToInternalMetdata
static void MergeToInternalMetdata(const UnknownFieldSet &other, internal::InternalMetadataWithArena *metadata)
Definition: unknown_field_set.cc:101
GOOGLE_DCHECK
#define GOOGLE_DCHECK
Definition: logging.h:194
google::protobuf::UnknownField::TYPE_VARINT
@ TYPE_VARINT
Definition: unknown_field_set.h:226
s
XmlRpcServer s
google::protobuf::UnknownFieldSet::ParseFromZeroCopyStream
bool ParseFromZeroCopyStream(io::ZeroCopyInputStream *input)
Definition: unknown_field_set.cc:229
benchmarks.util.result_uploader.metadata
def metadata
Definition: result_uploader.py:97
google::protobuf::uint32
uint32_t uint32
Definition: protobuf/src/google/protobuf/stubs/port.h:155
google::protobuf.internal::ReadSize
uint32 ReadSize(const char **pp)
Definition: parse_context.h:518
google::protobuf::UnknownField
Definition: unknown_field_set.h:223
google::protobuf::UnknownFieldSet::AddField
void AddField(const UnknownField &field)
Definition: unknown_field_set.cc:177
left
GLint left
Definition: glcorearb.h:4150
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
google::protobuf::RepeatedField< int >
google::protobuf::UnknownField::length_delimited_
union LengthDelimited length_delimited_
Definition: unknown_field_set.h:285
google::protobuf::python::cdescriptor_pool::Add
static PyObject * Add(PyObject *self, PyObject *file_descriptor_proto)
Definition: descriptor_pool.cc:621
google::protobuf::UnknownFieldSet::default_instance
static const UnknownFieldSet * default_instance()
Definition: unknown_field_set.cc:52
target
GLenum target
Definition: glcorearb.h:3739
google::protobuf.internal.python_message._InternalParse
_InternalParse
Definition: python_message.py:1197
google::protobuf::python::cmessage::UnknownFieldSet
static PyObject * UnknownFieldSet(CMessage *self)
Definition: python/google/protobuf/pyext/message.cc:2501
google::protobuf::UnknownFieldSet::Clear
void Clear()
Definition: unknown_field_set.h:299
google::protobuf::io::CodedOutputStream::WriteVarint32ToArray
static uint8 * WriteVarint32ToArray(uint32 value, uint8 *target)
Definition: coded_stream.h:1149
google::protobuf::UnknownField::type
Type type() const
Definition: unknown_field_set.h:330
google::protobuf::UnknownField::data_
union google::protobuf::UnknownField::@164 data_
google::protobuf::UnknownFieldSet::field_count
int field_count() const
Definition: unknown_field_set.h:311
google::protobuf::UnknownFieldSet::MergeFromAndDestroy
void MergeFromAndDestroy(UnknownFieldSet *other)
Definition: unknown_field_set.cc:90
google::protobuf::UnknownFieldSet::InternalMergeFrom
void InternalMergeFrom(const UnknownFieldSet &other)
Definition: unknown_field_set.cc:66
parse_context.h
google::protobuf.internal::OnShutdownDelete
T * OnShutdownDelete(T *p)
Definition: common.h:185
google::protobuf::UnknownFieldSet::AddGroup
UnknownFieldSet * AddGroup(int number)
Definition: unknown_field_set.cc:168
google::protobuf::UnknownField::Delete
void Delete()
Definition: unknown_field_set.cc:240
google::protobuf::UnknownFieldSet::ParseFromArray
bool ParseFromArray(const void *data, int size)
Definition: unknown_field_set.cc:235
google::protobuf::UnknownFieldSet::DeleteSubrange
void DeleteSubrange(int start, int num)
Definition: unknown_field_set.cc:182
google::protobuf::UnknownFieldSet::UnknownFieldSet
UnknownFieldSet()
Definition: unknown_field_set.h:293
unknown_field_set.h
google::protobuf::io::CodedInputStream::ConsumedEntireMessage
bool ConsumedEntireMessage()
Definition: coded_stream.h:1072
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::StringSpaceUsedExcludingSelfLong
size_t StringSpaceUsedExcludingSelfLong(const std::string &str)
Definition: generated_message_util.cc:86
google::protobuf::UnknownFieldSet::AddVarint
void AddVarint(int number, uint64 value)
Definition: unknown_field_set.cc:134
google::protobuf::UnknownFieldSet::MergeFrom
void MergeFrom(const UnknownFieldSet &other)
Definition: unknown_field_set.cc:77
google::protobuf.internal::UnknownFieldParse
const char * UnknownFieldParse(uint32 tag, std::string *unknown, const char *ptr, ParseContext *ctx)
Definition: parse_context.cc:545
google::protobuf::io::ArrayInputStream
Definition: zero_copy_stream_impl_lite.h:65
googletest-filter-unittest.child
child
Definition: googletest-filter-unittest.py:62
google::protobuf::uint64
uint64_t uint64
Definition: protobuf/src/google/protobuf/stubs/port.h:156
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
google::protobuf.internal::FieldParser
const PROTOBUF_MUST_USE_RESULT char * FieldParser(uint64 tag, T &field_parser, const char *ptr, ParseContext *ctx)
Definition: parse_context.h:637
google::protobuf::UnknownFieldSet::ParseFromCodedStream
bool ParseFromCodedStream(io::CodedInputStream *input)
Definition: unknown_field_set.cc:224
n
GLdouble n
Definition: glcorearb.h:4153
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
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf::UnknownField::LengthDelimited::string_value
std::string * string_value
Definition: unknown_field_set.h:276
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::PackedEnumParser
const char * PackedEnumParser(void *object, const char *ptr, ParseContext *ctx)
Definition: parse_context.cc:419
common.h
size
GLsizeiptr size
Definition: glcorearb.h:2943
stl_util.h
google::protobuf::UnknownFieldSet
Definition: unknown_field_set.h:86
google::protobuf::io::CodedInputStream
Definition: coded_stream.h:173
wire_format.h
logging.h
metadata.h
google::protobuf::UnknownFieldSet::AddFixed32
void AddFixed32(int number, uint32 value)
Definition: unknown_field_set.cc:142
google::protobuf::UnknownFieldSet::AddFixed64
void AddFixed64(int number, uint64 value)
Definition: unknown_field_set.cc:150
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::UnknownField::SerializeLengthDelimitedNoTagToArray
uint8 * SerializeLengthDelimitedNoTagToArray(uint8 *target) const
Definition: unknown_field_set.cc:279
google::protobuf::UnknownField::DeepCopy
void DeepCopy(const UnknownField &other)
Definition: unknown_field_set.cc:253
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
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
google::protobuf::UnknownFieldSet::DeleteByNumber
void DeleteByNumber(int number)
Definition: unknown_field_set.cc:197
google::protobuf.internal::WireFormat::SkipMessage
static bool SkipMessage(io::CodedInputStream *input, UnknownFieldSet *unknown_fields)
Definition: wire_format.cc:147
google::protobuf.internal::WireFormatParser
const PROTOBUF_MUST_USE_RESULT char * WireFormatParser(T &field_parser, const char *ptr, ParseContext *ctx)
Definition: parse_context.h:684
google::protobuf::UnknownFieldSet::SpaceUsedLong
size_t SpaceUsedLong() const
Definition: unknown_field_set.cc:130
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::io::CodedOutputStream::WriteStringToArray
static uint8 * WriteStringToArray(const std::string &str, uint8 *target)
Definition: coded_stream.h:1286
GOOGLE_PROTOBUF_PARSER_ASSERT
#define GOOGLE_PROTOBUF_PARSER_ASSERT(predicate)
Definition: parse_context.h:633
google::protobuf::UnknownFieldSet::MergeFromCodedStream
bool MergeFromCodedStream(io::CodedInputStream *input)
Definition: unknown_field_set.cc:213
google::protobuf::UnknownFieldSet::SpaceUsedExcludingSelfLong
size_t SpaceUsedExcludingSelfLong() const
Definition: unknown_field_set.cc:107
RepeatedField
Definition: ruby/ext/google/protobuf_c/protobuf.h:395
GOOGLE_DCHECK_EQ
#define GOOGLE_DCHECK_EQ
Definition: logging.h:196


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