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__
43
#include <google/protobuf/repeated_field.h>
48
class Message; // message.h
49
class UnknownField; // below
51
// An UnknownFieldSet contains fields that were encountered while parsing a
52
// message but were not defined by its type. Keeping track of these can be
53
// useful, especially in that they may be written if the message is serialized
54
// again without being cleared in between. This means that software which
55
// simply receives messages and forwards them to other servers does not need
56
// to be updated every time a new field is added to the message definition.
58
// To get the UnknownFieldSet attached to any message, call
59
// Reflection::GetUnknownFields().
61
// This class is necessarily tied to the protocol buffer wire format, unlike
62
// the Reflection interface which is independent of any serialization scheme.
63
class LIBPROTOBUF_EXPORT UnknownFieldSet {
72
inline bool empty() const;
74
// Merge the contents of some other UnknownFieldSet with this one.
75
void MergeFrom(const UnknownFieldSet& other);
77
// Swaps the contents of some other UnknownFieldSet with this one.
78
inline void Swap(UnknownFieldSet* x);
80
// Computes (an estimate of) the total number of bytes currently used for
81
// storing the unknown fields in memory. Does NOT include
82
// sizeof(*this) in the calculation.
83
int SpaceUsedExcludingSelf() const;
85
// Version of SpaceUsed() including sizeof(*this).
86
int SpaceUsed() const;
88
// Returns the number of fields present in the UnknownFieldSet.
89
inline int field_count() const;
90
// Get a field in the set, where 0 <= index < field_count(). The fields
91
// appear in the order in which they were added.
92
inline const UnknownField& field(int index) const;
93
// Get a mutable pointer to a field in the set, where
94
// 0 <= index < field_count(). The fields appear in the order in which
96
inline UnknownField* mutable_field(int index);
98
// Adding fields ---------------------------------------------------
100
void AddVarint(int number, uint64 value);
101
void AddFixed32(int number, uint32 value);
102
void AddFixed64(int number, uint64 value);
103
void AddLengthDelimited(int number, const string& value);
104
string* AddLengthDelimited(int number);
105
UnknownFieldSet* AddGroup(int number);
107
// Adds an unknown field from another set.
108
void AddField(const UnknownField& field);
110
// Parsing helpers -------------------------------------------------
111
// These work exactly like the similarly-named methods of Message.
113
bool MergeFromCodedStream(io::CodedInputStream* input);
114
bool ParseFromCodedStream(io::CodedInputStream* input);
115
bool ParseFromZeroCopyStream(io::ZeroCopyInputStream* input);
116
bool ParseFromArray(const void* data, int size);
117
inline bool ParseFromString(const string& data) {
118
return ParseFromArray(data.data(), data.size());
122
void ClearFallback();
124
vector<UnknownField>* fields_;
126
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UnknownFieldSet);
129
// Represents one field in an UnknownFieldSet.
130
class LIBPROTOBUF_EXPORT UnknownField {
136
TYPE_LENGTH_DELIMITED,
140
// The field's tag number, as seen on the wire.
141
inline int number() const;
144
inline Type type() const;
146
// Accessors -------------------------------------------------------
147
// Each method works only for UnknownFields of the corresponding type.
149
inline uint64 varint() const;
150
inline uint32 fixed32() const;
151
inline uint64 fixed64() const;
152
inline const string& length_delimited() const;
153
inline const UnknownFieldSet& group() const;
155
inline void set_varint(uint64 value);
156
inline void set_fixed32(uint32 value);
157
inline void set_fixed64(uint64 value);
158
inline void set_length_delimited(const string& value);
159
inline string* mutable_length_delimited();
160
inline UnknownFieldSet* mutable_group();
163
friend class UnknownFieldSet;
165
// If this UnknownField contains a pointer, delete it.
168
// Make a deep copy of any pointers in this UnknownField.
171
unsigned int number_ : 29;
172
unsigned int type_ : 3;
177
string* length_delimited_;
178
UnknownFieldSet* group_;
182
// ===================================================================
183
// inline implementations
185
inline void UnknownFieldSet::Clear() {
186
if (fields_ != NULL) {
191
inline bool UnknownFieldSet::empty() const {
192
return fields_ == NULL || fields_->empty();
195
inline void UnknownFieldSet::Swap(UnknownFieldSet* x) {
196
std::swap(fields_, x->fields_);
199
inline int UnknownFieldSet::field_count() const {
200
return (fields_ == NULL) ? 0 : fields_->size();
202
inline const UnknownField& UnknownFieldSet::field(int index) const {
203
return (*fields_)[index];
205
inline UnknownField* UnknownFieldSet::mutable_field(int index) {
206
return &(*fields_)[index];
209
inline void UnknownFieldSet::AddLengthDelimited(
210
int number, const string& value) {
211
AddLengthDelimited(number)->assign(value);
214
inline int UnknownField::number() const { return number_; }
215
inline UnknownField::Type UnknownField::type() const {
216
return static_cast<Type>(type_);
219
inline uint64 UnknownField::varint () const {
220
GOOGLE_DCHECK_EQ(type_, TYPE_VARINT);
223
inline uint32 UnknownField::fixed32() const {
224
GOOGLE_DCHECK_EQ(type_, TYPE_FIXED32);
227
inline uint64 UnknownField::fixed64() const {
228
GOOGLE_DCHECK_EQ(type_, TYPE_FIXED64);
231
inline const string& UnknownField::length_delimited() const {
232
GOOGLE_DCHECK_EQ(type_, TYPE_LENGTH_DELIMITED);
233
return *length_delimited_;
235
inline const UnknownFieldSet& UnknownField::group() const {
236
GOOGLE_DCHECK_EQ(type_, TYPE_GROUP);
240
inline void UnknownField::set_varint(uint64 value) {
241
GOOGLE_DCHECK_EQ(type_, TYPE_VARINT);
244
inline void UnknownField::set_fixed32(uint32 value) {
245
GOOGLE_DCHECK_EQ(type_, TYPE_FIXED32);
248
inline void UnknownField::set_fixed64(uint64 value) {
249
GOOGLE_DCHECK_EQ(type_, TYPE_FIXED64);
252
inline void UnknownField::set_length_delimited(const string& value) {
253
GOOGLE_DCHECK_EQ(type_, TYPE_LENGTH_DELIMITED);
254
length_delimited_->assign(value);
256
inline string* UnknownField::mutable_length_delimited() {
257
GOOGLE_DCHECK_EQ(type_, TYPE_LENGTH_DELIMITED);
258
return length_delimited_;
260
inline UnknownFieldSet* UnknownField::mutable_group() {
261
GOOGLE_DCHECK_EQ(type_, TYPE_GROUP);
265
} // namespace protobuf
267
} // namespace google
268
#endif // GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__