1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/
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>
45 // TODO(jasonh): some people seem to rely on protobufs to include this for them!
50 class CodedInputStream; // coded_stream.h
51 class CodedOutputStream; // coded_stream.h
52 class ZeroCopyInputStream; // zero_copy_stream.h
55 class WireFormat; // wire_format.h
56 class UnknownFieldSetFieldSkipperUsingCord;
57 // extension_set_heavy.cc
60 class Message; // message.h
61 class UnknownField; // below
63 // An UnknownFieldSet contains fields that were encountered while parsing a
64 // message but were not defined by its type. Keeping track of these can be
65 // useful, especially in that they may be written if the message is serialized
66 // again without being cleared in between. This means that software which
67 // simply receives messages and forwards them to other servers does not need
68 // to be updated every time a new field is added to the message definition.
70 // To get the UnknownFieldSet attached to any message, call
71 // Reflection::GetUnknownFields().
73 // This class is necessarily tied to the protocol buffer wire format, unlike
74 // the Reflection interface which is independent of any serialization scheme.
75 class LIBPROTOBUF_EXPORT UnknownFieldSet {
83 // Remove all fields and deallocate internal data objects
84 void ClearAndFreeMemory();
87 inline bool empty() const;
89 // Merge the contents of some other UnknownFieldSet with this one.
90 void MergeFrom(const UnknownFieldSet& other);
92 // Swaps the contents of some other UnknownFieldSet with this one.
93 inline void Swap(UnknownFieldSet* x);
95 // Computes (an estimate of) the total number of bytes currently used for
96 // storing the unknown fields in memory. Does NOT include
97 // sizeof(*this) in the calculation.
98 int SpaceUsedExcludingSelf() const;
100 // Version of SpaceUsed() including sizeof(*this).
101 int SpaceUsed() const;
103 // Returns the number of fields present in the UnknownFieldSet.
104 inline int field_count() const;
105 // Get a field in the set, where 0 <= index < field_count(). The fields
106 // appear in the order in which they were added.
107 inline const UnknownField& field(int index) const;
108 // Get a mutable pointer to a field in the set, where
109 // 0 <= index < field_count(). The fields appear in the order in which
111 inline UnknownField* mutable_field(int index);
113 // Adding fields ---------------------------------------------------
115 void AddVarint(int number, uint64 value);
116 void AddFixed32(int number, uint32 value);
117 void AddFixed64(int number, uint64 value);
118 void AddLengthDelimited(int number, const string& value);
119 string* AddLengthDelimited(int number);
120 UnknownFieldSet* AddGroup(int number);
122 // Adds an unknown field from another set.
123 void AddField(const UnknownField& field);
125 // Delete fields with indices in the range [start .. start+num-1].
126 // Caution: implementation moves all fields with indices [start+num .. ].
127 void DeleteSubrange(int start, int num);
129 // Delete all fields with a specific field number. The order of left fields
131 // Caution: implementation moves all fields after the first deleted field.
132 void DeleteByNumber(int number);
134 // Parsing helpers -------------------------------------------------
135 // These work exactly like the similarly-named methods of Message.
137 bool MergeFromCodedStream(io::CodedInputStream* input);
138 bool ParseFromCodedStream(io::CodedInputStream* input);
139 bool ParseFromZeroCopyStream(io::ZeroCopyInputStream* input);
140 bool ParseFromArray(const void* data, int size);
141 inline bool ParseFromString(const string& data) {
142 return ParseFromArray(data.data(), data.size());
147 void ClearFallback();
149 vector<UnknownField>* fields_;
151 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UnknownFieldSet);
154 // Represents one field in an UnknownFieldSet.
155 class LIBPROTOBUF_EXPORT UnknownField {
161 TYPE_LENGTH_DELIMITED,
165 // The field's tag number, as seen on the wire.
166 inline int number() const;
169 inline Type type() const;
171 // Accessors -------------------------------------------------------
172 // Each method works only for UnknownFields of the corresponding type.
174 inline uint64 varint() const;
175 inline uint32 fixed32() const;
176 inline uint64 fixed64() const;
177 inline const string& length_delimited() const;
178 inline const UnknownFieldSet& group() const;
180 inline void set_varint(uint64 value);
181 inline void set_fixed32(uint32 value);
182 inline void set_fixed64(uint64 value);
183 inline void set_length_delimited(const string& value);
184 inline string* mutable_length_delimited();
185 inline UnknownFieldSet* mutable_group();
187 // Serialization API.
188 // These methods can take advantage of the underlying implementation and may
189 // archieve a better performance than using getters to retrieve the data and
190 // do the serialization yourself.
191 void SerializeLengthDelimitedNoTag(io::CodedOutputStream* output) const;
192 uint8* SerializeLengthDelimitedNoTagToArray(uint8* target) const;
194 inline int GetLengthDelimitedSize() const;
197 friend class UnknownFieldSet;
199 // If this UnknownField contains a pointer, delete it.
202 // Make a deep copy of any pointers in this UnknownField.
206 unsigned int number_ : 29;
207 unsigned int type_ : 3;
213 string* string_value_;
215 UnknownFieldSet* group_;
219 // ===================================================================
220 // inline implementations
222 inline void UnknownFieldSet::Clear() {
223 if (fields_ != NULL) {
228 inline bool UnknownFieldSet::empty() const {
229 return fields_ == NULL || fields_->empty();
232 inline void UnknownFieldSet::Swap(UnknownFieldSet* x) {
233 std::swap(fields_, x->fields_);
236 inline int UnknownFieldSet::field_count() const {
237 return (fields_ == NULL) ? 0 : fields_->size();
239 inline const UnknownField& UnknownFieldSet::field(int index) const {
240 return (*fields_)[index];
242 inline UnknownField* UnknownFieldSet::mutable_field(int index) {
243 return &(*fields_)[index];
246 inline void UnknownFieldSet::AddLengthDelimited(
247 int number, const string& value) {
248 AddLengthDelimited(number)->assign(value);
252 inline int UnknownField::number() const { return number_; }
253 inline UnknownField::Type UnknownField::type() const {
254 return static_cast<Type>(type_);
257 inline uint64 UnknownField::varint () const {
258 assert(type_ == TYPE_VARINT);
261 inline uint32 UnknownField::fixed32() const {
262 assert(type_ == TYPE_FIXED32);
265 inline uint64 UnknownField::fixed64() const {
266 assert(type_ == TYPE_FIXED64);
269 inline const string& UnknownField::length_delimited() const {
270 assert(type_ == TYPE_LENGTH_DELIMITED);
271 return *length_delimited_.string_value_;
273 inline const UnknownFieldSet& UnknownField::group() const {
274 assert(type_ == TYPE_GROUP);
278 inline void UnknownField::set_varint(uint64 value) {
279 assert(type_ == TYPE_VARINT);
282 inline void UnknownField::set_fixed32(uint32 value) {
283 assert(type_ == TYPE_FIXED32);
286 inline void UnknownField::set_fixed64(uint64 value) {
287 assert(type_ == TYPE_FIXED64);
290 inline void UnknownField::set_length_delimited(const string& value) {
291 assert(type_ == TYPE_LENGTH_DELIMITED);
292 length_delimited_.string_value_->assign(value);
294 inline string* UnknownField::mutable_length_delimited() {
295 assert(type_ == TYPE_LENGTH_DELIMITED);
296 return length_delimited_.string_value_;
298 inline UnknownFieldSet* UnknownField::mutable_group() {
299 assert(type_ == TYPE_GROUP);
303 inline int UnknownField::GetLengthDelimitedSize() const {
304 GOOGLE_DCHECK_EQ(TYPE_LENGTH_DELIMITED, type_);
305 return length_delimited_.string_value_->size();
308 } // namespace protobuf
310 } // namespace google
311 #endif // GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__