TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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: [email protected] (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>
45 
46 namespace google {
47 namespace protobuf {
48  namespace io {
49  class CodedInputStream; // coded_stream.h
50  class CodedOutputStream; // coded_stream.h
51  class ZeroCopyInputStream; // zero_copy_stream.h
52  }
53  namespace internal {
54  class WireFormat; // wire_format.h
55  class MessageSetFieldSkipperUsingCord;
56  // extension_set_heavy.cc
57  }
58 
59 class Message; // message.h
60 class UnknownField; // below
61 
62 // An UnknownFieldSet contains fields that were encountered while parsing a
63 // message but were not defined by its type. Keeping track of these can be
64 // useful, especially in that they may be written if the message is serialized
65 // again without being cleared in between. This means that software which
66 // simply receives messages and forwards them to other servers does not need
67 // to be updated every time a new field is added to the message definition.
68 //
69 // To get the UnknownFieldSet attached to any message, call
70 // Reflection::GetUnknownFields().
71 //
72 // This class is necessarily tied to the protocol buffer wire format, unlike
73 // the Reflection interface which is independent of any serialization scheme.
75  public:
77  ~UnknownFieldSet();
78 
79  // Remove all fields.
80  inline void Clear();
81 
82  // Remove all fields and deallocate internal data objects
83  void ClearAndFreeMemory();
84 
85  // Is this set empty?
86  inline bool empty() const;
87 
88  // Merge the contents of some other UnknownFieldSet with this one.
89  void MergeFrom(const UnknownFieldSet& other);
90 
91  // Swaps the contents of some other UnknownFieldSet with this one.
92  inline void Swap(UnknownFieldSet* x);
93 
94  // Computes (an estimate of) the total number of bytes currently used for
95  // storing the unknown fields in memory. Does NOT include
96  // sizeof(*this) in the calculation.
97  int SpaceUsedExcludingSelf() const;
98 
99  // Version of SpaceUsed() including sizeof(*this).
100  int SpaceUsed() const;
101 
102  // Returns the number of fields present in the UnknownFieldSet.
103  inline int field_count() const;
104  // Get a field in the set, where 0 <= index < field_count(). The fields
105  // appear in the order in which they were added.
106  inline const UnknownField& field(int index) const;
107  // Get a mutable pointer to a field in the set, where
108  // 0 <= index < field_count(). The fields appear in the order in which
109  // they were added.
110  inline UnknownField* mutable_field(int index);
111 
112  // Adding fields ---------------------------------------------------
113 
114  void AddVarint(int number, uint64 value);
115  void AddFixed32(int number, uint32 value);
116  void AddFixed64(int number, uint64 value);
117  void AddLengthDelimited(int number, const string& value);
118  string* AddLengthDelimited(int number);
119  UnknownFieldSet* AddGroup(int number);
120 
121  // Adds an unknown field from another set.
122  void AddField(const UnknownField& field);
123 
124  // Delete fields with indices in the range [start .. start+num-1].
125  // Caution: implementation moves all fields with indices [start+num .. ].
126  void DeleteSubrange(int start, int num);
127 
128  // Delete all fields with a specific field number. The order of left fields
129  // is preserved.
130  // Caution: implementation moves all fields after the first deleted field.
131  void DeleteByNumber(int number);
132 
133  // Parsing helpers -------------------------------------------------
134  // These work exactly like the similarly-named methods of Message.
135 
136  bool MergeFromCodedStream(io::CodedInputStream* input);
137  bool ParseFromCodedStream(io::CodedInputStream* input);
138  bool ParseFromZeroCopyStream(io::ZeroCopyInputStream* input);
139  bool ParseFromArray(const void* data, int size);
140  inline bool ParseFromString(const string& data) {
141  return ParseFromArray(data.data(), static_cast<int>(data.size()));
142  }
143 
144  private:
145 
146  void ClearFallback();
147 
148  vector<UnknownField>* fields_;
149 
151 };
152 
153 // Represents one field in an UnknownFieldSet.
155  public:
156  enum Type {
161  TYPE_GROUP
162  };
163 
164  // The field's tag number, as seen on the wire.
165  inline int number() const;
166 
167  // The field type.
168  inline Type type() const;
169 
170  // Accessors -------------------------------------------------------
171  // Each method works only for UnknownFields of the corresponding type.
172 
173  inline uint64 varint() const;
174  inline uint32 fixed32() const;
175  inline uint64 fixed64() const;
176  inline const string& length_delimited() const;
177  inline const UnknownFieldSet& group() const;
178 
179  inline void set_varint(uint64 value);
180  inline void set_fixed32(uint32 value);
181  inline void set_fixed64(uint64 value);
182  inline void set_length_delimited(const string& value);
183  inline string* mutable_length_delimited();
184  inline UnknownFieldSet* mutable_group();
185 
186  // Serialization API.
187  // These methods can take advantage of the underlying implementation and may
188  // archieve a better performance than using getters to retrieve the data and
189  // do the serialization yourself.
190  void SerializeLengthDelimitedNoTag(io::CodedOutputStream* output) const;
191  uint8* SerializeLengthDelimitedNoTagToArray(uint8* target) const;
192 
193  inline int GetLengthDelimitedSize() const;
194 
195  private:
196  friend class UnknownFieldSet;
197 
198  // If this UnknownField contains a pointer, delete it.
199  void Delete();
200 
201  // Make a deep copy of any pointers in this UnknownField.
202  void DeepCopy();
203 
204  // Set the wire type of this UnknownField. Should only be used when this
205  // UnknownField is being created.
206  inline void SetType(Type type);
207 
210  union {
214  mutable union {
215  string* string_value_;
216  } length_delimited_;
218  };
219 };
220 
221 // ===================================================================
222 // inline implementations
223 
224 inline void UnknownFieldSet::Clear() {
225  if (fields_ != NULL) {
226  ClearFallback();
227  }
228 }
229 
230 inline bool UnknownFieldSet::empty() const {
231  return fields_ == NULL || fields_->empty();
232 }
233 
235  std::swap(fields_, x->fields_);
236 }
237 
238 inline int UnknownFieldSet::field_count() const {
239  return (fields_ == NULL) ? 0 : static_cast<int>(fields_->size());
240 }
241 inline const UnknownField& UnknownFieldSet::field(int index) const {
242  return (*fields_)[index];
243 }
245  return &(*fields_)[index];
246 }
247 
249  int number, const string& value) {
250  AddLengthDelimited(number)->assign(value);
251 }
252 
253 
254 inline int UnknownField::number() const { return number_; }
256  return static_cast<Type>(type_);
257 }
258 
259 inline uint64 UnknownField::varint() const {
260  assert(type() == TYPE_VARINT);
261  return varint_;
262 }
264  assert(type() == TYPE_FIXED32);
265  return fixed32_;
266 }
268  assert(type() == TYPE_FIXED64);
269  return fixed64_;
270 }
271 inline const string& UnknownField::length_delimited() const {
272  assert(type() == TYPE_LENGTH_DELIMITED);
273  return *length_delimited_.string_value_;
274 }
275 inline const UnknownFieldSet& UnknownField::group() const {
276  assert(type() == TYPE_GROUP);
277  return *group_;
278 }
279 
281  assert(type() == TYPE_VARINT);
282  varint_ = value;
283 }
285  assert(type() == TYPE_FIXED32);
286  fixed32_ = value;
287 }
289  assert(type() == TYPE_FIXED64);
290  fixed64_ = value;
291 }
292 inline void UnknownField::set_length_delimited(const string& value) {
293  assert(type() == TYPE_LENGTH_DELIMITED);
294  length_delimited_.string_value_->assign(value);
295 }
297  assert(type() == TYPE_LENGTH_DELIMITED);
298  return length_delimited_.string_value_;
299 }
301  assert(type() == TYPE_GROUP);
302  return group_;
303 }
304 
307  return static_cast<int>(length_delimited_.string_value_->size());
308 }
309 
310 inline void UnknownField::SetType(Type type) {
311  type_ = type;
312 }
313 
314 
315 } // namespace protobuf
316 
317 } // namespace google
318 #endif // GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__
const UnknownField & field(int index) const
Definition: unknown_field_set.h:241
void set_fixed32(uint32 value)
Definition: unknown_field_set.h:284
Definition: zero_copy_stream.h:124
Type type() const
Definition: unknown_field_set.h:255
const string & length_delimited() const
Definition: unknown_field_set.h:271
#define GOOGLE_DCHECK_EQ
Definition: common.h:753
Definition: unknown_field_set.h:74
uint8_t uint8
Definition: common.h:175
arena_t NULL
Definition: jemalloc_internal.h:624
Type
Definition: unknown_field_set.h:156
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
Definition: common.h:89
UnknownFieldSet * mutable_group()
Definition: unknown_field_set.h:300
void set_varint(uint64 value)
Definition: unknown_field_set.h:280
uint64 varint_
Definition: unknown_field_set.h:211
#define output
Definition: wire_format_lite.h:381
UnknownFieldSet * group_
Definition: unknown_field_set.h:217
uint32 type_
Definition: unknown_field_set.h:209
int GetLengthDelimitedSize() const
Definition: unknown_field_set.h:305
uint64 fixed64() const
Definition: unknown_field_set.h:267
const UnknownFieldSet & group() const
Definition: unknown_field_set.h:275
void Clear()
Definition: unknown_field_set.h:224
Definition: coded_stream.h:156
Definition: unknown_field_set.h:158
uint64 fixed64_
Definition: unknown_field_set.h:213
uint32 fixed32_
Definition: unknown_field_set.h:212
Definition: coded_stream.h:621
string * mutable_length_delimited()
Definition: unknown_field_set.h:296
union google::protobuf::UnknownField::@39::@41 length_delimited_
#define input
Definition: wire_format_lite.h:242
uint32_t uint32
Definition: common.h:177
void Swap(UnknownFieldSet *x)
Definition: unknown_field_set.h:234
Definition: unknown_field_set.h:161
vector< UnknownField > * fields_
Definition: unknown_field_set.h:148
uint64_t uint64
Definition: common.h:178
uint32 number_
Definition: unknown_field_set.h:208
Definition: unknown_field_set.h:157
void set_length_delimited(const string &value)
Definition: unknown_field_set.h:292
void SetType(Type type)
Definition: unknown_field_set.h:310
uint32 fixed32() const
Definition: unknown_field_set.h:263
Definition: document.h:390
UnknownField * mutable_field(int index)
Definition: unknown_field_set.h:244
#define LIBPROTOBUF_EXPORT
Definition: common.h:105
int field_count() const
Definition: unknown_field_set.h:238
int number() const
Definition: unknown_field_set.h:254
bool empty() const
Definition: unknown_field_set.h:230
uint64 varint() const
Definition: unknown_field_set.h:259
Definition: unknown_field_set.h:154
Definition: unknown_field_set.h:159
void AddLengthDelimited(int number, const string &value)
Definition: unknown_field_set.h:248
Definition: BnetFileGenerator.h:47
const FieldDescriptor value
Definition: descriptor.h:1522
bool ParseFromString(const string &data)
Definition: unknown_field_set.h:140
G3D::int16 x
Definition: Vector2int16.h:37
void set_fixed64(uint64 value)
Definition: unknown_field_set.h:288
Type
Type of JSON value.
Definition: rapidjson.h:642
string * string_value_
Definition: unknown_field_set.h:215