message_lite.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. // Authors: wink@google.com (Wink Saville),
  31. // kenton@google.com (Kenton Varda)
  32. // Based on original Protocol Buffers design by
  33. // Sanjay Ghemawat, Jeff Dean, and others.
  34. //
  35. // Defines MessageLite, the abstract interface implemented by all (lite
  36. // and non-lite) protocol message objects.
  37. #ifndef GOOGLE_PROTOBUF_MESSAGE_LITE_H__
  38. #define GOOGLE_PROTOBUF_MESSAGE_LITE_H__
  39. #include <climits>
  40. #include <google/protobuf/stubs/common.h>
  41. #include <google/protobuf/stubs/logging.h>
  42. #include <google/protobuf/stubs/once.h>
  43. #include <google/protobuf/arena.h>
  44. #include <google/protobuf/stubs/port.h>
  45. namespace google {
  46. namespace protobuf {
  47. template <typename T>
  48. class RepeatedPtrField;
  49. namespace io {
  50. class CodedInputStream;
  51. class CodedOutputStream;
  52. class ZeroCopyInputStream;
  53. class ZeroCopyOutputStream;
  54. }
  55. namespace internal {
  56. class RepeatedPtrFieldBase;
  57. class WireFormatLite;
  58. class WeakFieldMap;
  59. #ifndef SWIG
  60. // We compute sizes as size_t but cache them as int. This function converts a
  61. // computed size to a cached size. Since we don't proceed with serialization
  62. // if the total size was > INT_MAX, it is not important what this function
  63. // returns for inputs > INT_MAX. However this case should not error or
  64. // GOOGLE_CHECK-fail, because the full size_t resolution is still returned from
  65. // ByteSizeLong() and checked against INT_MAX; we can catch the overflow
  66. // there.
  67. inline int ToCachedSize(size_t size) { return static_cast<int>(size); }
  68. // We mainly calculate sizes in terms of size_t, but some functions that
  69. // compute sizes return "int". These int sizes are expected to always be
  70. // positive. This function is more efficient than casting an int to size_t
  71. // directly on 64-bit platforms because it avoids making the compiler emit a
  72. // sign extending instruction, which we don't want and don't want to pay for.
  73. inline size_t FromIntSize(int size) {
  74. // Convert to unsigned before widening so sign extension is not necessary.
  75. return static_cast<unsigned int>(size);
  76. }
  77. // For cases where a legacy function returns an integer size. We GOOGLE_DCHECK()
  78. // that the conversion will fit within an integer; if this is false then we
  79. // are losing information.
  80. inline int ToIntSize(size_t size) {
  81. GOOGLE_DCHECK_LE(size, static_cast<size_t>(INT_MAX));
  82. return static_cast<int>(size);
  83. }
  84. // This type wraps a variable whose constructor and destructor are explicitly
  85. // called. It is particularly useful for a global variable, without its
  86. // constructor and destructor run on start and end of the program lifetime.
  87. // This circumvents the initial construction order fiasco, while keeping
  88. // the address of the empty string a compile time constant.
  89. //
  90. // Pay special attention to the initialization state of the object.
  91. // 1. The object is "uninitialized" to begin with.
  92. // 2. Call DefaultConstruct() only if the object is uninitialized.
  93. // After the call, the object becomes "initialized".
  94. // 3. Call get() and get_mutable() only if the object is initialized.
  95. // 4. Call Destruct() only if the object is initialized.
  96. // After the call, the object becomes uninitialized.
  97. template <typename T>
  98. class ExplicitlyConstructed {
  99. public:
  100. void DefaultConstruct() {
  101. new (&union_) T();
  102. }
  103. void Destruct() {
  104. get_mutable()->~T();
  105. }
  106. constexpr const T& get() const { return reinterpret_cast<const T&>(union_); }
  107. T* get_mutable() { return reinterpret_cast<T*>(&union_); }
  108. private:
  109. // Prefer c++14 aligned_storage, but for compatibility this will do.
  110. union AlignedUnion {
  111. char space[sizeof(T)];
  112. int64 align_to_int64;
  113. void* align_to_ptr;
  114. } union_;
  115. };
  116. // Default empty string object. Don't use this directly. Instead, call
  117. // GetEmptyString() to get the reference.
  118. LIBPROTOBUF_EXPORT extern ExplicitlyConstructed<::std::string> fixed_address_empty_string;
  119. LIBPROTOBUF_EXPORT inline const ::std::string& GetEmptyStringAlreadyInited() {
  120. return fixed_address_empty_string.get();
  121. }
  122. LIBPROTOBUF_EXPORT size_t StringSpaceUsedExcludingSelfLong(const string& str);
  123. #endif // SWIG
  124. } // namespace internal
  125. // Interface to light weight protocol messages.
  126. //
  127. // This interface is implemented by all protocol message objects. Non-lite
  128. // messages additionally implement the Message interface, which is a
  129. // subclass of MessageLite. Use MessageLite instead when you only need
  130. // the subset of features which it supports -- namely, nothing that uses
  131. // descriptors or reflection. You can instruct the protocol compiler
  132. // to generate classes which implement only MessageLite, not the full
  133. // Message interface, by adding the following line to the .proto file:
  134. //
  135. // option optimize_for = LITE_RUNTIME;
  136. //
  137. // This is particularly useful on resource-constrained systems where
  138. // the full protocol buffers runtime library is too big.
  139. //
  140. // Note that on non-constrained systems (e.g. servers) when you need
  141. // to link in lots of protocol definitions, a better way to reduce
  142. // total code footprint is to use optimize_for = CODE_SIZE. This
  143. // will make the generated code smaller while still supporting all the
  144. // same features (at the expense of speed). optimize_for = LITE_RUNTIME
  145. // is best when you only have a small number of message types linked
  146. // into your binary, in which case the size of the protocol buffers
  147. // runtime itself is the biggest problem.
  148. class LIBPROTOBUF_EXPORT MessageLite {
  149. public:
  150. inline MessageLite() {}
  151. virtual ~MessageLite() {}
  152. // Basic Operations ------------------------------------------------
  153. // Get the name of this message type, e.g. "foo.bar.BazProto".
  154. virtual string GetTypeName() const = 0;
  155. // Construct a new instance of the same type. Ownership is passed to the
  156. // caller.
  157. virtual MessageLite* New() const = 0;
  158. // Construct a new instance on the arena. Ownership is passed to the caller
  159. // if arena is a NULL. Default implementation for backwards compatibility.
  160. virtual MessageLite* New(::google::protobuf::Arena* arena) const;
  161. // Get the arena, if any, associated with this message. Virtual method
  162. // required for generic operations but most arena-related operations should
  163. // use the GetArenaNoVirtual() generated-code method. Default implementation
  164. // to reduce code size by avoiding the need for per-type implementations
  165. // when types do not implement arena support.
  166. virtual ::google::protobuf::Arena* GetArena() const { return NULL; }
  167. // Get a pointer that may be equal to this message's arena, or may not be.
  168. // If the value returned by this method is equal to some arena pointer, then
  169. // this message is on that arena; however, if this message is on some arena,
  170. // this method may or may not return that arena's pointer. As a tradeoff,
  171. // this method may be more efficient than GetArena(). The intent is to allow
  172. // underlying representations that use e.g. tagged pointers to sometimes
  173. // store the arena pointer directly, and sometimes in a more indirect way,
  174. // and allow a fastpath comparison against the arena pointer when it's easy
  175. // to obtain.
  176. virtual void* GetMaybeArenaPointer() const { return GetArena(); }
  177. // Clear all fields of the message and set them to their default values.
  178. // Clear() avoids freeing memory, assuming that any memory allocated
  179. // to hold parts of the message will be needed again to hold the next
  180. // message. If you actually want to free the memory used by a Message,
  181. // you must delete it.
  182. virtual void Clear() = 0;
  183. // Quickly check if all required fields have values set.
  184. virtual bool IsInitialized() const = 0;
  185. // This is not implemented for Lite messages -- it just returns "(cannot
  186. // determine missing fields for lite message)". However, it is implemented
  187. // for full messages. See message.h.
  188. virtual string InitializationErrorString() const;
  189. // If |other| is the exact same class as this, calls MergeFrom(). Otherwise,
  190. // results are undefined (probably crash).
  191. virtual void CheckTypeAndMergeFrom(const MessageLite& other) = 0;
  192. // Parsing ---------------------------------------------------------
  193. // Methods for parsing in protocol buffer format. Most of these are
  194. // just simple wrappers around MergeFromCodedStream(). Clear() will be
  195. // called before merging the input.
  196. // Fill the message with a protocol buffer parsed from the given input
  197. // stream. Returns false on a read error or if the input is in the wrong
  198. // format. A successful return does not indicate the entire input is
  199. // consumed, ensure you call ConsumedEntireMessage() to check that if
  200. // applicable.
  201. bool ParseFromCodedStream(io::CodedInputStream* input);
  202. // Like ParseFromCodedStream(), but accepts messages that are missing
  203. // required fields.
  204. bool ParsePartialFromCodedStream(io::CodedInputStream* input);
  205. // Read a protocol buffer from the given zero-copy input stream. If
  206. // successful, the entire input will be consumed.
  207. bool ParseFromZeroCopyStream(io::ZeroCopyInputStream* input);
  208. // Like ParseFromZeroCopyStream(), but accepts messages that are missing
  209. // required fields.
  210. bool ParsePartialFromZeroCopyStream(io::ZeroCopyInputStream* input);
  211. // Read a protocol buffer from the given zero-copy input stream, expecting
  212. // the message to be exactly "size" bytes long. If successful, exactly
  213. // this many bytes will have been consumed from the input.
  214. bool ParseFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, int size);
  215. // Like ParseFromBoundedZeroCopyStream(), but accepts messages that are
  216. // missing required fields.
  217. bool ParsePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input,
  218. int size);
  219. // Parses a protocol buffer contained in a string. Returns true on success.
  220. // This function takes a string in the (non-human-readable) binary wire
  221. // format, matching the encoding output by MessageLite::SerializeToString().
  222. // If you'd like to convert a human-readable string into a protocol buffer
  223. // object, see google::protobuf::TextFormat::ParseFromString().
  224. bool ParseFromString(const string& data);
  225. // Like ParseFromString(), but accepts messages that are missing
  226. // required fields.
  227. bool ParsePartialFromString(const string& data);
  228. // Parse a protocol buffer contained in an array of bytes.
  229. bool ParseFromArray(const void* data, int size);
  230. // Like ParseFromArray(), but accepts messages that are missing
  231. // required fields.
  232. bool ParsePartialFromArray(const void* data, int size);
  233. // Reads a protocol buffer from the stream and merges it into this
  234. // Message. Singular fields read from the what is
  235. // already in the Message and repeated fields are appended to those
  236. // already present.
  237. //
  238. // It is the responsibility of the caller to call input->LastTagWas()
  239. // (for groups) or input->ConsumedEntireMessage() (for non-groups) after
  240. // this returns to verify that the message's end was delimited correctly.
  241. //
  242. // ParsefromCodedStream() is implemented as Clear() followed by
  243. // MergeFromCodedStream().
  244. bool MergeFromCodedStream(io::CodedInputStream* input);
  245. // Like MergeFromCodedStream(), but succeeds even if required fields are
  246. // missing in the input.
  247. //
  248. // MergeFromCodedStream() is just implemented as MergePartialFromCodedStream()
  249. // followed by IsInitialized().
  250. virtual bool MergePartialFromCodedStream(io::CodedInputStream* input) = 0;
  251. // Serialization ---------------------------------------------------
  252. // Methods for serializing in protocol buffer format. Most of these
  253. // are just simple wrappers around ByteSize() and SerializeWithCachedSizes().
  254. // Write a protocol buffer of this message to the given output. Returns
  255. // false on a write error. If the message is missing required fields,
  256. // this may GOOGLE_CHECK-fail.
  257. bool SerializeToCodedStream(io::CodedOutputStream* output) const;
  258. // Like SerializeToCodedStream(), but allows missing required fields.
  259. bool SerializePartialToCodedStream(io::CodedOutputStream* output) const;
  260. // Write the message to the given zero-copy output stream. All required
  261. // fields must be set.
  262. bool SerializeToZeroCopyStream(io::ZeroCopyOutputStream* output) const;
  263. // Like SerializeToZeroCopyStream(), but allows missing required fields.
  264. bool SerializePartialToZeroCopyStream(io::ZeroCopyOutputStream* output) const;
  265. // Serialize the message and store it in the given string. All required
  266. // fields must be set.
  267. bool SerializeToString(string* output) const;
  268. // Like SerializeToString(), but allows missing required fields.
  269. bool SerializePartialToString(string* output) const;
  270. // Serialize the message and store it in the given byte array. All required
  271. // fields must be set.
  272. bool SerializeToArray(void* data, int size) const;
  273. // Like SerializeToArray(), but allows missing required fields.
  274. bool SerializePartialToArray(void* data, int size) const;
  275. // Make a string encoding the message. Is equivalent to calling
  276. // SerializeToString() on a string and using that. Returns the empty
  277. // string if SerializeToString() would have returned an error.
  278. // Note: If you intend to generate many such strings, you may
  279. // reduce heap fragmentation by instead re-using the same string
  280. // object with calls to SerializeToString().
  281. string SerializeAsString() const;
  282. // Like SerializeAsString(), but allows missing required fields.
  283. string SerializePartialAsString() const;
  284. // Like SerializeToString(), but appends to the data to the string's existing
  285. // contents. All required fields must be set.
  286. bool AppendToString(string* output) const;
  287. // Like AppendToString(), but allows missing required fields.
  288. bool AppendPartialToString(string* output) const;
  289. // Computes the serialized size of the message. This recursively calls
  290. // ByteSizeLong() on all embedded messages.
  291. //
  292. // ByteSizeLong() is generally linear in the number of fields defined for the
  293. // proto.
  294. virtual size_t ByteSizeLong() const = 0;
  295. // Legacy ByteSize() API.
  296. PROTOBUF_RUNTIME_DEPRECATED("Please use ByteSizeLong() instead")
  297. int ByteSize() const {
  298. return internal::ToIntSize(ByteSizeLong());
  299. }
  300. // Serializes the message without recomputing the size. The message must not
  301. // have changed since the last call to ByteSize(), and the value returned by
  302. // ByteSize must be non-negative. Otherwise the results are undefined.
  303. virtual void SerializeWithCachedSizes(
  304. io::CodedOutputStream* output) const;
  305. // Functions below here are not part of the public interface. It isn't
  306. // enforced, but they should be treated as private, and will be private
  307. // at some future time. Unfortunately the implementation of the "friend"
  308. // keyword in GCC is broken at the moment, but we expect it will be fixed.
  309. // Like SerializeWithCachedSizes, but writes directly to *target, returning
  310. // a pointer to the byte immediately after the last byte written. "target"
  311. // must point at a byte array of at least ByteSize() bytes. Whether to use
  312. // deterministic serialization, e.g., maps in sorted order, is determined by
  313. // CodedOutputStream::IsDefaultSerializationDeterministic().
  314. virtual uint8* SerializeWithCachedSizesToArray(uint8* target) const;
  315. // Returns the result of the last call to ByteSize(). An embedded message's
  316. // size is needed both to serialize it (because embedded messages are
  317. // length-delimited) and to compute the outer message's size. Caching
  318. // the size avoids computing it multiple times.
  319. //
  320. // ByteSize() does not automatically use the cached size when available
  321. // because this would require invalidating it every time the message was
  322. // modified, which would be too hard and expensive. (E.g. if a deeply-nested
  323. // sub-message is changed, all of its parents' cached sizes would need to be
  324. // invalidated, which is too much work for an otherwise inlined setter
  325. // method.)
  326. virtual int GetCachedSize() const = 0;
  327. virtual uint8* InternalSerializeWithCachedSizesToArray(bool deterministic,
  328. uint8* target) const;
  329. protected:
  330. // CastToBase allows generated code to cast a RepeatedPtrField<T> to
  331. // RepeatedPtrFieldBase. We try to restrict access to RepeatedPtrFieldBase
  332. // because it is an implementation detail that user code should not access
  333. // directly.
  334. template <typename T>
  335. static ::google::protobuf::internal::RepeatedPtrFieldBase* CastToBase(
  336. ::google::protobuf::RepeatedPtrField<T>* repeated) {
  337. return repeated;
  338. }
  339. template <typename T>
  340. static const ::google::protobuf::internal::RepeatedPtrFieldBase& CastToBase(
  341. const ::google::protobuf::RepeatedPtrField<T>& repeated) {
  342. return repeated;
  343. }
  344. template <typename T>
  345. static T* CreateMaybeMessage(Arena* arena) {
  346. return Arena::CreateMaybeMessage<T>(arena);
  347. }
  348. private:
  349. // TODO(gerbens) make this a pure abstract function
  350. virtual const void* InternalGetTable() const { return NULL; }
  351. friend class internal::WireFormatLite;
  352. friend class Message;
  353. friend class internal::WeakFieldMap;
  354. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageLite);
  355. };
  356. namespace internal {
  357. extern bool LIBPROTOBUF_EXPORT proto3_preserve_unknown_;
  358. // DO NOT USE: For migration only. Will be removed when Proto3 defaults to
  359. // preserve unknowns.
  360. inline bool GetProto3PreserveUnknownsDefault() {
  361. return proto3_preserve_unknown_;
  362. }
  363. // DO NOT USE: For migration only. Will be removed when Proto3 defaults to
  364. // preserve unknowns.
  365. void LIBPROTOBUF_EXPORT SetProto3PreserveUnknownsDefault(bool preserve);
  366. } // namespace internal
  367. } // namespace protobuf
  368. } // namespace google
  369. #endif // GOOGLE_PROTOBUF_MESSAGE_LITE_H__