aboutsummaryrefslogtreecommitdiff
path: root/src/google/protobuf/unknown_field_set.h
blob: 421846215ff69142f51248d55a4f6f363bde8fb9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.
// http://code.google.com/p/protobuf/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Author: kenton@google.com (Kenton Varda)
//  Based on original Protocol Buffers design by
//  Sanjay Ghemawat, Jeff Dean, and others.
//
// Contains classes used to keep track of unrecognized fields seen while
// parsing a protocol message.

#ifndef GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__
#define GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__

#include <string>
#include <map>
#include <vector>
#include <google/protobuf/repeated_field.h>

namespace google {
namespace protobuf {

class Message;                      // message.h
class UnknownField;                 // below

// An UnknownFieldSet contains fields that were encountered while parsing a
// message but were not defined by its type.  Keeping track of these can be
// useful, especially in that they may be written if the message is serialized
// again without being cleared in between.  This means that software which
// simply receives messages and forwards them to other servers does not need
// to be updated every time a new field is added to the message definition.
//
// To get the UnknownFieldSet attached to any message, call
// Message::Reflection::GetUnknownFields().
//
// This class is necessarily tied to the protocol buffer wire format, unlike
// the Reflection interface which is independent of any serialization scheme.
class LIBPROTOBUF_EXPORT UnknownFieldSet {
 public:
  UnknownFieldSet();
  ~UnknownFieldSet();

  // Remove all fields.
  void Clear();

  // Is this set empty?
  inline bool empty() const;

  // Merge the contents of some other UnknownFieldSet with this one.
  void MergeFrom(const UnknownFieldSet& other);

  // Returns the number of fields present in the UnknownFieldSet.
  inline int field_count() const;
  // Get a field in the set, where 0 <= index < field_count().  The fields
  // appear in arbitrary order.
  inline const UnknownField& field(int index) const;
  // Get a mutable pointer to a field in the set, where
  // 0 <= index < field_count().  The fields appear in arbitrary order.
  inline UnknownField* mutable_field(int index);

  // Find a field by field number.  Returns NULL if not found.
  const UnknownField* FindFieldByNumber(int number) const;

  // Add a field by field number.  If the field number already exists, returns
  // the existing UnknownField.
  UnknownField* AddField(int number);

 private:
  // "Active" fields are ones which have been added since the last time Clear()
  // was called.  Inactive fields are objects we are keeping around incase
  // they become active again.

  struct Internal {
    // Contains all UnknownFields that have been allocated for this
    // UnknownFieldSet, including ones not currently active.  Keyed by
    // field number.  We intentionally try to reuse UnknownField objects for
    // the same field number they were used for originally because this makes
    // it more likely that the previously-allocated memory will have the right
    // layout.
    map<int, UnknownField*> fields_;

    // Contains the fields from fields_ that are currently active.
    vector<UnknownField*> active_fields_;
  };

  // We want an UnknownFieldSet to use no more space than a single pointer
  // until the first field is added.
  Internal* internal_;

  // Don't keep more inactive fields than this.
  static const int kMaxInactiveFields = 100;

  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UnknownFieldSet);
};

// Represents one field in an UnknownFieldSet.
//
// UnknownFiled's accessors are similar to those that would be produced by the
// protocol compiler for the fields:
//   repeated uint64 varint;
//   repeated fixed32 fixed32;
//   repeated fixed64 fixed64;
//   repeated bytes length_delimited;
//   repeated UnknownFieldSet group;
// (OK, so the last one isn't actually a valid field type but you get the
// idea.)
class LIBPROTOBUF_EXPORT UnknownField {
 public:
  ~UnknownField();

  // Clears all fields.
  void Clear();

  // Merge the contents of some other UnknownField with this one.  For each
  // wire type, the values are simply concatenated.
  void MergeFrom(const UnknownField& other);

  // The field's tag number, as seen on the wire.
  inline int number() const;

  // The index of this UnknownField within the UknownFieldSet (e.g.
  // set.field(field.index()) == field).
  inline int index() const;

  inline int varint_size          () const;
  inline int fixed32_size         () const;
  inline int fixed64_size         () const;
  inline int length_delimited_size() const;
  inline int group_size           () const;

  inline uint64 varint (int index) const;
  inline uint32 fixed32(int index) const;
  inline uint64 fixed64(int index) const;
  inline const string& length_delimited(int index) const;
  inline const UnknownFieldSet& group(int index) const;

  inline void set_varint (int index, uint64 value);
  inline void set_fixed32(int index, uint32 value);
  inline void set_fixed64(int index, uint64 value);
  inline void set_length_delimited(int index, const string& value);
  inline string* mutable_length_delimited(int index);
  inline UnknownFieldSet* mutable_group(int index);

  inline void add_varint (uint64 value);
  inline void add_fixed32(uint32 value);
  inline void add_fixed64(uint64 value);
  inline void add_length_delimited(const string& value);
  inline string* add_length_delimited();
  inline UnknownFieldSet* add_group();

  inline void clear_varint ();
  inline void clear_fixed32();
  inline void clear_fixed64();
  inline void clear_length_delimited();
  inline void clear_group();

  inline const RepeatedField   <uint64         >& varint          () const;
  inline const RepeatedField   <uint32         >& fixed32         () const;
  inline const RepeatedField   <uint64         >& fixed64         () const;
  inline const RepeatedPtrField<string         >& length_delimited() const;
  inline const RepeatedPtrField<UnknownFieldSet>& group           () const;

  inline RepeatedField   <uint64         >* mutable_varint          ();
  inline RepeatedField   <uint32         >* mutable_fixed32         ();
  inline RepeatedField   <uint64         >* mutable_fixed64         ();
  inline RepeatedPtrField<string         >* mutable_length_delimited();
  inline RepeatedPtrField<UnknownFieldSet>* mutable_group           ();

 private:
  friend class UnknownFieldSet;
  UnknownField(int number);

  int number_;
  int index_;

  RepeatedField   <uint64         > varint_;
  RepeatedField   <uint32         > fixed32_;
  RepeatedField   <uint64         > fixed64_;
  RepeatedPtrField<string         > length_delimited_;
  RepeatedPtrField<UnknownFieldSet> group_;

  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UnknownField);
};

// ===================================================================
// inline implementations

inline bool UnknownFieldSet::empty() const {
  return internal_ == NULL || internal_->active_fields_.empty();
}

inline int UnknownFieldSet::field_count() const {
  return (internal_ == NULL) ? 0 : internal_->active_fields_.size();
}
inline const UnknownField& UnknownFieldSet::field(int index) const {
  return *(internal_->active_fields_[index]);
}
inline UnknownField* UnknownFieldSet::mutable_field(int index) {
  return internal_->active_fields_[index];
}

inline int UnknownField::number() const { return number_; }
inline int UnknownField::index () const { return index_; }

inline int UnknownField::varint_size          () const {return varint_.size();}
inline int UnknownField::fixed32_size         () const {return fixed32_.size();}
inline int UnknownField::fixed64_size         () const {return fixed64_.size();}
inline int UnknownField::length_delimited_size() const {
  return length_delimited_.size();
}
inline int UnknownField::group_size           () const {return group_.size();}

inline uint64 UnknownField::varint (int index) const {
  return varint_.Get(index);
}
inline uint32 UnknownField::fixed32(int index) const {
  return fixed32_.Get(index);
}
inline uint64 UnknownField::fixed64(int index) const {
  return fixed64_.Get(index);
}
inline const string& UnknownField::length_delimited(int index) const {
  return length_delimited_.Get(index);
}
inline const UnknownFieldSet& UnknownField::group(int index) const {
  return group_.Get(index);
}

inline void UnknownField::set_varint (int index, uint64 value) {
  varint_.Set(index, value);
}
inline void UnknownField::set_fixed32(int index, uint32 value) {
  fixed32_.Set(index, value);
}
inline void UnknownField::set_fixed64(int index, uint64 value) {
  fixed64_.Set(index, value);
}
inline void UnknownField::set_length_delimited(int index, const string& value) {
  length_delimited_.Mutable(index)->assign(value);
}
inline string* UnknownField::mutable_length_delimited(int index) {
  return length_delimited_.Mutable(index);
}
inline UnknownFieldSet* UnknownField::mutable_group(int index) {
  return group_.Mutable(index);
}

inline void UnknownField::add_varint (uint64 value) {
  varint_.Add(value);
}
inline void UnknownField::add_fixed32(uint32 value) {
  fixed32_.Add(value);
}
inline void UnknownField::add_fixed64(uint64 value) {
  fixed64_.Add(value);
}
inline void UnknownField::add_length_delimited(const string& value) {
  length_delimited_.Add()->assign(value);
}
inline string* UnknownField::add_length_delimited() {
  return length_delimited_.Add();
}
inline UnknownFieldSet* UnknownField::add_group() {
  return group_.Add();
}

inline void UnknownField::clear_varint () { varint_.Clear(); }
inline void UnknownField::clear_fixed32() { varint_.Clear(); }
inline void UnknownField::clear_fixed64() { varint_.Clear(); }
inline void UnknownField::clear_length_delimited() {
  length_delimited_.Clear();
}
inline void UnknownField::clear_group() { group_.Clear(); }

inline const RepeatedField<uint64>& UnknownField::varint () const {
  return varint_;
}
inline const RepeatedField<uint32>& UnknownField::fixed32() const {
  return fixed32_;
}
inline const RepeatedField<uint64>& UnknownField::fixed64() const {
  return fixed64_;
}
inline const RepeatedPtrField<string>& UnknownField::length_delimited() const {
  return length_delimited_;
}
inline const RepeatedPtrField<UnknownFieldSet>& UnknownField::group() const {
  return group_;
}

inline RepeatedField<uint64>* UnknownField::mutable_varint () {
  return &varint_;
}
inline RepeatedField<uint32>* UnknownField::mutable_fixed32() {
  return &fixed32_;
}
inline RepeatedField<uint64>* UnknownField::mutable_fixed64() {
  return &fixed64_;
}
inline RepeatedPtrField<string>* UnknownField::mutable_length_delimited() {
  return &length_delimited_;
}
inline RepeatedPtrField<UnknownFieldSet>* UnknownField::mutable_group() {
  return &group_;
}

}  // namespace protobuf

}  // namespace google
#endif  // GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__