| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // http://code.google.com/p/protobuf/ | 3 // http://code.google.com/p/protobuf/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 // Author: kenton@google.com (Kenton Varda) | 31 // Author: kenton@google.com (Kenton Varda) |
| 32 // Based on original Protocol Buffers design by | 32 // Based on original Protocol Buffers design by |
| 33 // Sanjay Ghemawat, Jeff Dean, and others. | 33 // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 // | 34 // |
| 35 // Contains classes used to keep track of unrecognized fields seen while | 35 // Contains classes used to keep track of unrecognized fields seen while |
| 36 // parsing a protocol message. | 36 // parsing a protocol message. |
| 37 | 37 |
| 38 #ifndef GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__ | 38 #ifndef GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__ |
| 39 #define GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__ | 39 #define GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__ |
| 40 | 40 |
| 41 #include <assert.h> |
| 41 #include <string> | 42 #include <string> |
| 42 #include <vector> | 43 #include <vector> |
| 43 #include <google/protobuf/repeated_field.h> | 44 #include <google/protobuf/stubs/common.h> |
| 45 // TODO(jasonh): some people seem to rely on protobufs to include this for them! |
| 44 | 46 |
| 45 namespace google { | 47 namespace google { |
| 46 namespace protobuf { | 48 namespace protobuf { |
| 49 namespace io { |
| 50 class CodedInputStream; // coded_stream.h |
| 51 class CodedOutputStream; // coded_stream.h |
| 52 class ZeroCopyInputStream; // zero_copy_stream.h |
| 53 } |
| 54 namespace internal { |
| 55 class WireFormat; // wire_format.h |
| 56 class UnknownFieldSetFieldSkipperUsingCord; |
| 57 // extension_set_heavy.cc |
| 58 } |
| 47 | 59 |
| 48 class Message; // message.h | 60 class Message; // message.h |
| 49 class UnknownField; // below | 61 class UnknownField; // below |
| 50 | 62 |
| 51 // An UnknownFieldSet contains fields that were encountered while parsing a | 63 // 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 | 64 // 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 | 65 // 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 | 66 // again without being cleared in between. This means that software which |
| 55 // simply receives messages and forwards them to other servers does not need | 67 // 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. | 68 // to be updated every time a new field is added to the message definition. |
| 57 // | 69 // |
| 58 // To get the UnknownFieldSet attached to any message, call | 70 // To get the UnknownFieldSet attached to any message, call |
| 59 // Reflection::GetUnknownFields(). | 71 // Reflection::GetUnknownFields(). |
| 60 // | 72 // |
| 61 // This class is necessarily tied to the protocol buffer wire format, unlike | 73 // This class is necessarily tied to the protocol buffer wire format, unlike |
| 62 // the Reflection interface which is independent of any serialization scheme. | 74 // the Reflection interface which is independent of any serialization scheme. |
| 63 class LIBPROTOBUF_EXPORT UnknownFieldSet { | 75 class LIBPROTOBUF_EXPORT UnknownFieldSet { |
| 64 public: | 76 public: |
| 65 UnknownFieldSet(); | 77 UnknownFieldSet(); |
| 66 ~UnknownFieldSet(); | 78 ~UnknownFieldSet(); |
| 67 | 79 |
| 68 // Remove all fields. | 80 // Remove all fields. |
| 69 inline void Clear(); | 81 inline void Clear(); |
| 70 | 82 |
| 83 // Remove all fields and deallocate internal data objects |
| 84 void ClearAndFreeMemory(); |
| 85 |
| 71 // Is this set empty? | 86 // Is this set empty? |
| 72 inline bool empty() const; | 87 inline bool empty() const; |
| 73 | 88 |
| 74 // Merge the contents of some other UnknownFieldSet with this one. | 89 // Merge the contents of some other UnknownFieldSet with this one. |
| 75 void MergeFrom(const UnknownFieldSet& other); | 90 void MergeFrom(const UnknownFieldSet& other); |
| 76 | 91 |
| 77 // Swaps the contents of some other UnknownFieldSet with this one. | 92 // Swaps the contents of some other UnknownFieldSet with this one. |
| 78 inline void Swap(UnknownFieldSet* x); | 93 inline void Swap(UnknownFieldSet* x); |
| 79 | 94 |
| 80 // Computes (an estimate of) the total number of bytes currently used for | 95 // Computes (an estimate of) the total number of bytes currently used for |
| (...skipping 19 matching lines...) Expand all Loading... |
| 100 void AddVarint(int number, uint64 value); | 115 void AddVarint(int number, uint64 value); |
| 101 void AddFixed32(int number, uint32 value); | 116 void AddFixed32(int number, uint32 value); |
| 102 void AddFixed64(int number, uint64 value); | 117 void AddFixed64(int number, uint64 value); |
| 103 void AddLengthDelimited(int number, const string& value); | 118 void AddLengthDelimited(int number, const string& value); |
| 104 string* AddLengthDelimited(int number); | 119 string* AddLengthDelimited(int number); |
| 105 UnknownFieldSet* AddGroup(int number); | 120 UnknownFieldSet* AddGroup(int number); |
| 106 | 121 |
| 107 // Adds an unknown field from another set. | 122 // Adds an unknown field from another set. |
| 108 void AddField(const UnknownField& field); | 123 void AddField(const UnknownField& field); |
| 109 | 124 |
| 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); |
| 128 |
| 129 // Delete all fields with a specific field number. The order of left fields |
| 130 // is preserved. |
| 131 // Caution: implementation moves all fields after the first deleted field. |
| 132 void DeleteByNumber(int number); |
| 133 |
| 110 // Parsing helpers ------------------------------------------------- | 134 // Parsing helpers ------------------------------------------------- |
| 111 // These work exactly like the similarly-named methods of Message. | 135 // These work exactly like the similarly-named methods of Message. |
| 112 | 136 |
| 113 bool MergeFromCodedStream(io::CodedInputStream* input); | 137 bool MergeFromCodedStream(io::CodedInputStream* input); |
| 114 bool ParseFromCodedStream(io::CodedInputStream* input); | 138 bool ParseFromCodedStream(io::CodedInputStream* input); |
| 115 bool ParseFromZeroCopyStream(io::ZeroCopyInputStream* input); | 139 bool ParseFromZeroCopyStream(io::ZeroCopyInputStream* input); |
| 116 bool ParseFromArray(const void* data, int size); | 140 bool ParseFromArray(const void* data, int size); |
| 117 inline bool ParseFromString(const string& data) { | 141 inline bool ParseFromString(const string& data) { |
| 118 return ParseFromArray(data.data(), data.size()); | 142 return ParseFromArray(data.data(), data.size()); |
| 119 } | 143 } |
| 120 | 144 |
| 121 private: | 145 private: |
| 146 |
| 122 void ClearFallback(); | 147 void ClearFallback(); |
| 123 | 148 |
| 124 vector<UnknownField>* fields_; | 149 vector<UnknownField>* fields_; |
| 125 | 150 |
| 126 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UnknownFieldSet); | 151 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UnknownFieldSet); |
| 127 }; | 152 }; |
| 128 | 153 |
| 129 // Represents one field in an UnknownFieldSet. | 154 // Represents one field in an UnknownFieldSet. |
| 130 class LIBPROTOBUF_EXPORT UnknownField { | 155 class LIBPROTOBUF_EXPORT UnknownField { |
| 131 public: | 156 public: |
| (...skipping 20 matching lines...) Expand all Loading... |
| 152 inline const string& length_delimited() const; | 177 inline const string& length_delimited() const; |
| 153 inline const UnknownFieldSet& group() const; | 178 inline const UnknownFieldSet& group() const; |
| 154 | 179 |
| 155 inline void set_varint(uint64 value); | 180 inline void set_varint(uint64 value); |
| 156 inline void set_fixed32(uint32 value); | 181 inline void set_fixed32(uint32 value); |
| 157 inline void set_fixed64(uint64 value); | 182 inline void set_fixed64(uint64 value); |
| 158 inline void set_length_delimited(const string& value); | 183 inline void set_length_delimited(const string& value); |
| 159 inline string* mutable_length_delimited(); | 184 inline string* mutable_length_delimited(); |
| 160 inline UnknownFieldSet* mutable_group(); | 185 inline UnknownFieldSet* mutable_group(); |
| 161 | 186 |
| 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; |
| 193 |
| 194 inline int GetLengthDelimitedSize() const; |
| 195 |
| 162 private: | 196 private: |
| 163 friend class UnknownFieldSet; | 197 friend class UnknownFieldSet; |
| 164 | 198 |
| 165 // If this UnknownField contains a pointer, delete it. | 199 // If this UnknownField contains a pointer, delete it. |
| 166 void Delete(); | 200 void Delete(); |
| 167 | 201 |
| 168 // Make a deep copy of any pointers in this UnknownField. | 202 // Make a deep copy of any pointers in this UnknownField. |
| 169 void DeepCopy(); | 203 void DeepCopy(); |
| 170 | 204 |
| 205 |
| 171 unsigned int number_ : 29; | 206 unsigned int number_ : 29; |
| 172 unsigned int type_ : 3; | 207 unsigned int type_ : 3; |
| 173 union { | 208 union { |
| 174 uint64 varint_; | 209 uint64 varint_; |
| 175 uint32 fixed32_; | 210 uint32 fixed32_; |
| 176 uint64 fixed64_; | 211 uint64 fixed64_; |
| 177 string* length_delimited_; | 212 mutable union { |
| 213 string* string_value_; |
| 214 } length_delimited_; |
| 178 UnknownFieldSet* group_; | 215 UnknownFieldSet* group_; |
| 179 }; | 216 }; |
| 180 }; | 217 }; |
| 181 | 218 |
| 182 // =================================================================== | 219 // =================================================================== |
| 183 // inline implementations | 220 // inline implementations |
| 184 | 221 |
| 185 inline void UnknownFieldSet::Clear() { | 222 inline void UnknownFieldSet::Clear() { |
| 186 if (fields_ != NULL) { | 223 if (fields_ != NULL) { |
| 187 ClearFallback(); | 224 ClearFallback(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 204 } | 241 } |
| 205 inline UnknownField* UnknownFieldSet::mutable_field(int index) { | 242 inline UnknownField* UnknownFieldSet::mutable_field(int index) { |
| 206 return &(*fields_)[index]; | 243 return &(*fields_)[index]; |
| 207 } | 244 } |
| 208 | 245 |
| 209 inline void UnknownFieldSet::AddLengthDelimited( | 246 inline void UnknownFieldSet::AddLengthDelimited( |
| 210 int number, const string& value) { | 247 int number, const string& value) { |
| 211 AddLengthDelimited(number)->assign(value); | 248 AddLengthDelimited(number)->assign(value); |
| 212 } | 249 } |
| 213 | 250 |
| 251 |
| 214 inline int UnknownField::number() const { return number_; } | 252 inline int UnknownField::number() const { return number_; } |
| 215 inline UnknownField::Type UnknownField::type() const { | 253 inline UnknownField::Type UnknownField::type() const { |
| 216 return static_cast<Type>(type_); | 254 return static_cast<Type>(type_); |
| 217 } | 255 } |
| 218 | 256 |
| 219 inline uint64 UnknownField::varint () const { | 257 inline uint64 UnknownField::varint () const { |
| 220 GOOGLE_DCHECK_EQ(type_, TYPE_VARINT); | 258 assert(type_ == TYPE_VARINT); |
| 221 return varint_; | 259 return varint_; |
| 222 } | 260 } |
| 223 inline uint32 UnknownField::fixed32() const { | 261 inline uint32 UnknownField::fixed32() const { |
| 224 GOOGLE_DCHECK_EQ(type_, TYPE_FIXED32); | 262 assert(type_ == TYPE_FIXED32); |
| 225 return fixed32_; | 263 return fixed32_; |
| 226 } | 264 } |
| 227 inline uint64 UnknownField::fixed64() const { | 265 inline uint64 UnknownField::fixed64() const { |
| 228 GOOGLE_DCHECK_EQ(type_, TYPE_FIXED64); | 266 assert(type_ == TYPE_FIXED64); |
| 229 return fixed64_; | 267 return fixed64_; |
| 230 } | 268 } |
| 231 inline const string& UnknownField::length_delimited() const { | 269 inline const string& UnknownField::length_delimited() const { |
| 232 GOOGLE_DCHECK_EQ(type_, TYPE_LENGTH_DELIMITED); | 270 assert(type_ == TYPE_LENGTH_DELIMITED); |
| 233 return *length_delimited_; | 271 return *length_delimited_.string_value_; |
| 234 } | 272 } |
| 235 inline const UnknownFieldSet& UnknownField::group() const { | 273 inline const UnknownFieldSet& UnknownField::group() const { |
| 236 GOOGLE_DCHECK_EQ(type_, TYPE_GROUP); | 274 assert(type_ == TYPE_GROUP); |
| 237 return *group_; | 275 return *group_; |
| 238 } | 276 } |
| 239 | 277 |
| 240 inline void UnknownField::set_varint(uint64 value) { | 278 inline void UnknownField::set_varint(uint64 value) { |
| 241 GOOGLE_DCHECK_EQ(type_, TYPE_VARINT); | 279 assert(type_ == TYPE_VARINT); |
| 242 varint_ = value; | 280 varint_ = value; |
| 243 } | 281 } |
| 244 inline void UnknownField::set_fixed32(uint32 value) { | 282 inline void UnknownField::set_fixed32(uint32 value) { |
| 245 GOOGLE_DCHECK_EQ(type_, TYPE_FIXED32); | 283 assert(type_ == TYPE_FIXED32); |
| 246 fixed32_ = value; | 284 fixed32_ = value; |
| 247 } | 285 } |
| 248 inline void UnknownField::set_fixed64(uint64 value) { | 286 inline void UnknownField::set_fixed64(uint64 value) { |
| 249 GOOGLE_DCHECK_EQ(type_, TYPE_FIXED64); | 287 assert(type_ == TYPE_FIXED64); |
| 250 fixed64_ = value; | 288 fixed64_ = value; |
| 251 } | 289 } |
| 252 inline void UnknownField::set_length_delimited(const string& value) { | 290 inline void UnknownField::set_length_delimited(const string& value) { |
| 253 GOOGLE_DCHECK_EQ(type_, TYPE_LENGTH_DELIMITED); | 291 assert(type_ == TYPE_LENGTH_DELIMITED); |
| 254 length_delimited_->assign(value); | 292 length_delimited_.string_value_->assign(value); |
| 255 } | 293 } |
| 256 inline string* UnknownField::mutable_length_delimited() { | 294 inline string* UnknownField::mutable_length_delimited() { |
| 257 GOOGLE_DCHECK_EQ(type_, TYPE_LENGTH_DELIMITED); | 295 assert(type_ == TYPE_LENGTH_DELIMITED); |
| 258 return length_delimited_; | 296 return length_delimited_.string_value_; |
| 259 } | 297 } |
| 260 inline UnknownFieldSet* UnknownField::mutable_group() { | 298 inline UnknownFieldSet* UnknownField::mutable_group() { |
| 261 GOOGLE_DCHECK_EQ(type_, TYPE_GROUP); | 299 assert(type_ == TYPE_GROUP); |
| 262 return group_; | 300 return group_; |
| 263 } | 301 } |
| 264 | 302 |
| 303 inline int UnknownField::GetLengthDelimitedSize() const { |
| 304 GOOGLE_DCHECK_EQ(TYPE_LENGTH_DELIMITED, type_); |
| 305 return length_delimited_.string_value_->size(); |
| 306 } |
| 307 |
| 265 } // namespace protobuf | 308 } // namespace protobuf |
| 266 | 309 |
| 267 } // namespace google | 310 } // namespace google |
| 268 #endif // GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__ | 311 #endif // GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__ |
| OLD | NEW |