1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
35 // Contains classes used to keep track of unrecognized fields seen while
36 // parsing a protocol message.
38 #ifndef GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__
39 #define GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__
44 #include <google/protobuf/stubs/common.h>
49 class CodedInputStream; // coded_stream.h
50 class CodedOutputStream; // coded_stream.h
51 class ZeroCopyInputStream; // zero_copy_stream.h
54 class WireFormat; // wire_format.h
55 class MessageSetFieldSkipperUsingCord;
56 // extension_set_heavy.cc
59 class Message; // message.h
60 class UnknownField; // below
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.
69 // To get the UnknownFieldSet attached to any message, call
70 // Reflection::GetUnknownFields().
72 // This class is necessarily tied to the protocol buffer wire format, unlike
73 // the Reflection interface which is independent of any serialization scheme.
74 class LIBPROTOBUF_EXPORT UnknownFieldSet {
82 // Remove all fields and deallocate internal data objects
83 void ClearAndFreeMemory();
86 inline bool empty() const;
88 // Merge the contents of some other UnknownFieldSet with this one.
89 void MergeFrom(const UnknownFieldSet& other);
91 // Swaps the contents of some other UnknownFieldSet with this one.
92 inline void Swap(UnknownFieldSet* x);
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;
99 // Version of SpaceUsed() including sizeof(*this).
100 int SpaceUsed() const;
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
110 inline UnknownField* mutable_field(int index);
112 // Adding fields ---------------------------------------------------
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);
121 // Adds an unknown field from another set.
122 void AddField(const UnknownField& field);
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);
128 // Delete all fields with a specific field number. The order of left fields
130 // Caution: implementation moves all fields after the first deleted field.
131 void DeleteByNumber(int number);
133 // Parsing helpers -------------------------------------------------
134 // These work exactly like the similarly-named methods of Message.
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()));
146 void ClearFallback();
148 vector<UnknownField>* fields_;
150 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UnknownFieldSet);
153 // Represents one field in an UnknownFieldSet.
154 class LIBPROTOBUF_EXPORT UnknownField {
160 TYPE_LENGTH_DELIMITED,
164 // The field's tag number, as seen on the wire.
165 inline int number() const;
168 inline Type type() const;
170 // Accessors -------------------------------------------------------
171 // Each method works only for UnknownFields of the corresponding type.
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;
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();
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;
193 inline int GetLengthDelimitedSize() const;
196 friend class UnknownFieldSet;
198 // If this UnknownField contains a pointer, delete it.
201 // Make a deep copy of any pointers in this UnknownField.
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);
215 string* string_value_;
217 UnknownFieldSet* group_;
221 // ===================================================================
222 // inline implementations
224 inline void UnknownFieldSet::Clear() {
225 if (fields_ != NULL) {
230 inline bool UnknownFieldSet::empty() const {
231 return fields_ == NULL || fields_->empty();
234 inline void UnknownFieldSet::Swap(UnknownFieldSet* x) {
235 std::swap(fields_, x->fields_);
238 inline int UnknownFieldSet::field_count() const {
239 return (fields_ == NULL) ? 0 : static_cast<int>(fields_->size());
241 inline const UnknownField& UnknownFieldSet::field(int index) const {
242 return (*fields_)[index];
244 inline UnknownField* UnknownFieldSet::mutable_field(int index) {
245 return &(*fields_)[index];
248 inline void UnknownFieldSet::AddLengthDelimited(
249 int number, const string& value) {
250 AddLengthDelimited(number)->assign(value);
254 inline int UnknownField::number() const { return number_; }
255 inline UnknownField::Type UnknownField::type() const {
256 return static_cast<Type>(type_);
259 inline uint64 UnknownField::varint() const {
260 assert(type() == TYPE_VARINT);
263 inline uint32 UnknownField::fixed32() const {
264 assert(type() == TYPE_FIXED32);
267 inline uint64 UnknownField::fixed64() const {
268 assert(type() == TYPE_FIXED64);
271 inline const string& UnknownField::length_delimited() const {
272 assert(type() == TYPE_LENGTH_DELIMITED);
273 return *length_delimited_.string_value_;
275 inline const UnknownFieldSet& UnknownField::group() const {
276 assert(type() == TYPE_GROUP);
280 inline void UnknownField::set_varint(uint64 value) {
281 assert(type() == TYPE_VARINT);
284 inline void UnknownField::set_fixed32(uint32 value) {
285 assert(type() == TYPE_FIXED32);
288 inline void UnknownField::set_fixed64(uint64 value) {
289 assert(type() == TYPE_FIXED64);
292 inline void UnknownField::set_length_delimited(const string& value) {
293 assert(type() == TYPE_LENGTH_DELIMITED);
294 length_delimited_.string_value_->assign(value);
296 inline string* UnknownField::mutable_length_delimited() {
297 assert(type() == TYPE_LENGTH_DELIMITED);
298 return length_delimited_.string_value_;
300 inline UnknownFieldSet* UnknownField::mutable_group() {
301 assert(type() == TYPE_GROUP);
305 inline int UnknownField::GetLengthDelimitedSize() const {
306 GOOGLE_DCHECK_EQ(TYPE_LENGTH_DELIMITED, type());
307 return static_cast<int>(length_delimited_.string_value_->size());
310 inline void UnknownField::SetType(Type type) {
315 } // namespace protobuf
317 } // namespace google
318 #endif // GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__