unknown_field_set.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // Contains classes used to keep track of unrecognized fields seen while
  35. // parsing a protocol message.
  36. #ifndef GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__
  37. #define GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__
  38. #include <assert.h>
  39. #include <string>
  40. #include <vector>
  41. #include <google/protobuf/stubs/common.h>
  42. namespace google {
  43. namespace protobuf {
  44. namespace io {
  45. class CodedInputStream; // coded_stream.h
  46. class CodedOutputStream; // coded_stream.h
  47. class ZeroCopyInputStream; // zero_copy_stream.h
  48. }
  49. namespace internal {
  50. class WireFormat; // wire_format.h
  51. class MessageSetFieldSkipperUsingCord;
  52. // extension_set_heavy.cc
  53. }
  54. class Message; // message.h
  55. class UnknownField; // below
  56. // An UnknownFieldSet contains fields that were encountered while parsing a
  57. // message but were not defined by its type. Keeping track of these can be
  58. // useful, especially in that they may be written if the message is serialized
  59. // again without being cleared in between. This means that software which
  60. // simply receives messages and forwards them to other servers does not need
  61. // to be updated every time a new field is added to the message definition.
  62. //
  63. // To get the UnknownFieldSet attached to any message, call
  64. // Reflection::GetUnknownFields().
  65. //
  66. // This class is necessarily tied to the protocol buffer wire format, unlike
  67. // the Reflection interface which is independent of any serialization scheme.
  68. class LIBPROTOBUF_EXPORT UnknownFieldSet {
  69. public:
  70. UnknownFieldSet();
  71. ~UnknownFieldSet();
  72. // Remove all fields.
  73. inline void Clear();
  74. // Remove all fields and deallocate internal data objects
  75. void ClearAndFreeMemory();
  76. // Is this set empty?
  77. inline bool empty() const;
  78. // Merge the contents of some other UnknownFieldSet with this one.
  79. void MergeFrom(const UnknownFieldSet& other);
  80. // Swaps the contents of some other UnknownFieldSet with this one.
  81. inline void Swap(UnknownFieldSet* x);
  82. // Computes (an estimate of) the total number of bytes currently used for
  83. // storing the unknown fields in memory. Does NOT include
  84. // sizeof(*this) in the calculation.
  85. int SpaceUsedExcludingSelf() const;
  86. // Version of SpaceUsed() including sizeof(*this).
  87. 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
  95. // they were added.
  96. inline UnknownField* mutable_field(int index);
  97. // Adding fields ---------------------------------------------------
  98. void AddVarint(int number, uint64 value);
  99. void AddFixed32(int number, uint32 value);
  100. void AddFixed64(int number, uint64 value);
  101. void AddLengthDelimited(int number, const string& value);
  102. string* AddLengthDelimited(int number);
  103. UnknownFieldSet* AddGroup(int number);
  104. // Adds an unknown field from another set.
  105. void AddField(const UnknownField& field);
  106. // Delete fields with indices in the range [start .. start+num-1].
  107. // Caution: implementation moves all fields with indices [start+num .. ].
  108. void DeleteSubrange(int start, int num);
  109. // Delete all fields with a specific field number. The order of left fields
  110. // is preserved.
  111. // Caution: implementation moves all fields after the first deleted field.
  112. void DeleteByNumber(int number);
  113. // Parsing helpers -------------------------------------------------
  114. // These work exactly like the similarly-named methods of Message.
  115. bool MergeFromCodedStream(io::CodedInputStream* input);
  116. bool ParseFromCodedStream(io::CodedInputStream* input);
  117. bool ParseFromZeroCopyStream(io::ZeroCopyInputStream* input);
  118. bool ParseFromArray(const void* data, int size);
  119. inline bool ParseFromString(const string& data) {
  120. return ParseFromArray(data.data(), static_cast<int>(data.size()));
  121. }
  122. private:
  123. void ClearFallback();
  124. vector<UnknownField>* fields_;
  125. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UnknownFieldSet);
  126. };
  127. // Represents one field in an UnknownFieldSet.
  128. class LIBPROTOBUF_EXPORT UnknownField {
  129. public:
  130. enum Type {
  131. TYPE_VARINT,
  132. TYPE_FIXED32,
  133. TYPE_FIXED64,
  134. TYPE_LENGTH_DELIMITED,
  135. TYPE_GROUP
  136. };
  137. // The field's tag number, as seen on the wire.
  138. inline int number() const;
  139. // The field type.
  140. inline Type type() const;
  141. // Accessors -------------------------------------------------------
  142. // Each method works only for UnknownFields of the corresponding type.
  143. inline uint64 varint() const;
  144. inline uint32 fixed32() const;
  145. inline uint64 fixed64() const;
  146. inline const string& length_delimited() const;
  147. inline const UnknownFieldSet& group() const;
  148. inline void set_varint(uint64 value);
  149. inline void set_fixed32(uint32 value);
  150. inline void set_fixed64(uint64 value);
  151. inline void set_length_delimited(const string& value);
  152. inline string* mutable_length_delimited();
  153. inline UnknownFieldSet* mutable_group();
  154. // Serialization API.
  155. // These methods can take advantage of the underlying implementation and may
  156. // archieve a better performance than using getters to retrieve the data and
  157. // do the serialization yourself.
  158. void SerializeLengthDelimitedNoTag(io::CodedOutputStream* output) const;
  159. uint8* SerializeLengthDelimitedNoTagToArray(uint8* target) const;
  160. inline int GetLengthDelimitedSize() const;
  161. private:
  162. friend class UnknownFieldSet;
  163. // If this UnknownField contains a pointer, delete it.
  164. void Delete();
  165. // Make a deep copy of any pointers in this UnknownField.
  166. void DeepCopy();
  167. // Set the wire type of this UnknownField. Should only be used when this
  168. // UnknownField is being created.
  169. inline void SetType(Type type);
  170. uint32 number_;
  171. uint32 type_;
  172. union {
  173. uint64 varint_;
  174. uint32 fixed32_;
  175. uint64 fixed64_;
  176. mutable union {
  177. string* string_value_;
  178. } length_delimited_;
  179. UnknownFieldSet* group_;
  180. };
  181. };
  182. // ===================================================================
  183. // inline implementations
  184. inline void UnknownFieldSet::Clear() {
  185. if (fields_ != NULL) {
  186. ClearFallback();
  187. }
  188. }
  189. inline bool UnknownFieldSet::empty() const {
  190. return fields_ == NULL || fields_->empty();
  191. }
  192. inline void UnknownFieldSet::Swap(UnknownFieldSet* x) {
  193. std::swap(fields_, x->fields_);
  194. }
  195. inline int UnknownFieldSet::field_count() const {
  196. return (fields_ == NULL) ? 0 : static_cast<int>(fields_->size());
  197. }
  198. inline const UnknownField& UnknownFieldSet::field(int index) const {
  199. return (*fields_)[index];
  200. }
  201. inline UnknownField* UnknownFieldSet::mutable_field(int index) {
  202. return &(*fields_)[index];
  203. }
  204. inline void UnknownFieldSet::AddLengthDelimited(
  205. int number, const string& value) {
  206. AddLengthDelimited(number)->assign(value);
  207. }
  208. inline int UnknownField::number() const { return number_; }
  209. inline UnknownField::Type UnknownField::type() const {
  210. return static_cast<Type>(type_);
  211. }
  212. inline uint64 UnknownField::varint() const {
  213. assert(type() == TYPE_VARINT);
  214. return varint_;
  215. }
  216. inline uint32 UnknownField::fixed32() const {
  217. assert(type() == TYPE_FIXED32);
  218. return fixed32_;
  219. }
  220. inline uint64 UnknownField::fixed64() const {
  221. assert(type() == TYPE_FIXED64);
  222. return fixed64_;
  223. }
  224. inline const string& UnknownField::length_delimited() const {
  225. assert(type() == TYPE_LENGTH_DELIMITED);
  226. return *length_delimited_.string_value_;
  227. }
  228. inline const UnknownFieldSet& UnknownField::group() const {
  229. assert(type() == TYPE_GROUP);
  230. return *group_;
  231. }
  232. inline void UnknownField::set_varint(uint64 value) {
  233. assert(type() == TYPE_VARINT);
  234. varint_ = value;
  235. }
  236. inline void UnknownField::set_fixed32(uint32 value) {
  237. assert(type() == TYPE_FIXED32);
  238. fixed32_ = value;
  239. }
  240. inline void UnknownField::set_fixed64(uint64 value) {
  241. assert(type() == TYPE_FIXED64);
  242. fixed64_ = value;
  243. }
  244. inline void UnknownField::set_length_delimited(const string& value) {
  245. assert(type() == TYPE_LENGTH_DELIMITED);
  246. length_delimited_.string_value_->assign(value);
  247. }
  248. inline string* UnknownField::mutable_length_delimited() {
  249. assert(type() == TYPE_LENGTH_DELIMITED);
  250. return length_delimited_.string_value_;
  251. }
  252. inline UnknownFieldSet* UnknownField::mutable_group() {
  253. assert(type() == TYPE_GROUP);
  254. return group_;
  255. }
  256. inline int UnknownField::GetLengthDelimitedSize() const {
  257. GOOGLE_DCHECK_EQ(TYPE_LENGTH_DELIMITED, type());
  258. return static_cast<int>(length_delimited_.string_value_->size());
  259. }
  260. inline void UnknownField::SetType(Type type) {
  261. type_ = type;
  262. }
  263. } // namespace protobuf
  264. } // namespace google
  265. #endif // GOOGLE_PROTOBUF_UNKNOWN_FIELD_SET_H__