You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

message_lite.h 30 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 <string>
  41. #include <google/protobuf/stubs/common.h>
  42. #include <google/protobuf/stubs/logging.h>
  43. #include <google/protobuf/io/coded_stream.h>
  44. #include <google/protobuf/arena.h>
  45. #include <google/protobuf/stubs/once.h>
  46. #include <google/protobuf/port.h>
  47. #include <google/protobuf/stubs/strutil.h>
  48. #include <google/protobuf/explicitly_constructed.h>
  49. #include <google/protobuf/metadata_lite.h>
  50. #include <google/protobuf/stubs/hash.h> // TODO(b/211442718): cleanup
  51. // clang-format off
  52. #include <google/protobuf/port_def.inc>
  53. // clang-format on
  54. #ifdef SWIG
  55. #error "You cannot SWIG proto headers"
  56. #endif
  57. namespace google
  58. {
  59. namespace protobuf
  60. {
  61. template<typename T>
  62. class RepeatedPtrField;
  63. class FastReflectionMessageMutator;
  64. class FastReflectionStringSetter;
  65. class Reflection;
  66. namespace io
  67. {
  68. class CodedInputStream;
  69. class CodedOutputStream;
  70. class ZeroCopyInputStream;
  71. class ZeroCopyOutputStream;
  72. } // namespace io
  73. namespace internal
  74. {
  75. class SwapFieldHelper;
  76. // See parse_context.h for explanation
  77. class ParseContext;
  78. class ExtensionSet;
  79. class LazyField;
  80. class RepeatedPtrFieldBase;
  81. class TcParser;
  82. class WireFormatLite;
  83. class WeakFieldMap;
  84. template<typename Type>
  85. class GenericTypeHandler; // defined in repeated_field.h
  86. // We compute sizes as size_t but cache them as int. This function converts a
  87. // computed size to a cached size. Since we don't proceed with serialization
  88. // if the total size was > INT_MAX, it is not important what this function
  89. // returns for inputs > INT_MAX. However this case should not error or
  90. // GOOGLE_CHECK-fail, because the full size_t resolution is still returned from
  91. // ByteSizeLong() and checked against INT_MAX; we can catch the overflow
  92. // there.
  93. inline int ToCachedSize(size_t size)
  94. {
  95. return static_cast<int>(size);
  96. }
  97. // We mainly calculate sizes in terms of size_t, but some functions that
  98. // compute sizes return "int". These int sizes are expected to always be
  99. // positive. This function is more efficient than casting an int to size_t
  100. // directly on 64-bit platforms because it avoids making the compiler emit a
  101. // sign extending instruction, which we don't want and don't want to pay for.
  102. inline size_t FromIntSize(int size)
  103. {
  104. // Convert to unsigned before widening so sign extension is not necessary.
  105. return static_cast<unsigned int>(size);
  106. }
  107. // For cases where a legacy function returns an integer size. We GOOGLE_DCHECK()
  108. // that the conversion will fit within an integer; if this is false then we
  109. // are losing information.
  110. inline int ToIntSize(size_t size)
  111. {
  112. GOOGLE_DCHECK_LE(size, static_cast<size_t>(INT_MAX));
  113. return static_cast<int>(size);
  114. }
  115. // Default empty string object. Don't use this directly. Instead, call
  116. // GetEmptyString() to get the reference. This empty string is aligned with a
  117. // minimum alignment of 8 bytes to match the requirement of ArenaStringPtr.
  118. PROTOBUF_EXPORT extern ExplicitlyConstructedArenaString
  119. fixed_address_empty_string;
  120. PROTOBUF_EXPORT constexpr const std::string& GetEmptyStringAlreadyInited()
  121. {
  122. return fixed_address_empty_string.get();
  123. }
  124. PROTOBUF_EXPORT size_t StringSpaceUsedExcludingSelfLong(const std::string& str);
  125. } // namespace internal
  126. // Interface to light weight protocol messages.
  127. //
  128. // This interface is implemented by all protocol message objects. Non-lite
  129. // messages additionally implement the Message interface, which is a
  130. // subclass of MessageLite. Use MessageLite instead when you only need
  131. // the subset of features which it supports -- namely, nothing that uses
  132. // descriptors or reflection. You can instruct the protocol compiler
  133. // to generate classes which implement only MessageLite, not the full
  134. // Message interface, by adding the following line to the .proto file:
  135. //
  136. // option optimize_for = LITE_RUNTIME;
  137. //
  138. // This is particularly useful on resource-constrained systems where
  139. // the full protocol buffers runtime library is too big.
  140. //
  141. // Note that on non-constrained systems (e.g. servers) when you need
  142. // to link in lots of protocol definitions, a better way to reduce
  143. // total code footprint is to use optimize_for = CODE_SIZE. This
  144. // will make the generated code smaller while still supporting all the
  145. // same features (at the expense of speed). optimize_for = LITE_RUNTIME
  146. // is best when you only have a small number of message types linked
  147. // into your binary, in which case the size of the protocol buffers
  148. // runtime itself is the biggest problem.
  149. //
  150. // Users must not derive from this class. Only the protocol compiler and
  151. // the internal library are allowed to create subclasses.
  152. class PROTOBUF_EXPORT MessageLite
  153. {
  154. public:
  155. constexpr MessageLite()
  156. {
  157. }
  158. virtual ~MessageLite() = default;
  159. // Basic Operations ------------------------------------------------
  160. // Get the name of this message type, e.g. "foo.bar.BazProto".
  161. virtual std::string GetTypeName() const = 0;
  162. // Construct a new instance of the same type. Ownership is passed to the
  163. // caller.
  164. MessageLite* New() const
  165. {
  166. return New(nullptr);
  167. }
  168. // Construct a new instance on the arena. Ownership is passed to the caller
  169. // if arena is a nullptr.
  170. virtual MessageLite* New(Arena* arena) const = 0;
  171. // Returns user-owned arena; nullptr if it's message owned.
  172. Arena* GetArena() const
  173. {
  174. return _internal_metadata_.user_arena();
  175. }
  176. // Clear all fields of the message and set them to their default values.
  177. // Clear() assumes that any memory allocated to hold parts of the message
  178. // will likely be needed again, so the memory used may not be freed.
  179. // To ensure that all memory used by a Message is freed, you must delete it.
  180. virtual void Clear() = 0;
  181. // Quickly check if all required fields have values set.
  182. virtual bool IsInitialized() const = 0;
  183. // This is not implemented for Lite messages -- it just returns "(cannot
  184. // determine missing fields for lite message)". However, it is implemented
  185. // for full messages. See message.h.
  186. virtual std::string InitializationErrorString() const;
  187. // If |other| is the exact same class as this, calls MergeFrom(). Otherwise,
  188. // results are undefined (probably crash).
  189. virtual void CheckTypeAndMergeFrom(const MessageLite& other) = 0;
  190. // These methods return a human-readable summary of the message. Note that
  191. // since the MessageLite interface does not support reflection, there is very
  192. // little information that these methods can provide. They are shadowed by
  193. // methods of the same name on the Message interface which provide much more
  194. // information. The methods here are intended primarily to facilitate code
  195. // reuse for logic that needs to interoperate with both full and lite protos.
  196. //
  197. // The format of the returned string is subject to change, so please do not
  198. // assume it will remain stable over time.
  199. std::string DebugString() const;
  200. std::string ShortDebugString() const
  201. {
  202. return DebugString();
  203. }
  204. // MessageLite::DebugString is already Utf8 Safe. This is to add compatibility
  205. // with Message.
  206. std::string Utf8DebugString() const
  207. {
  208. return DebugString();
  209. }
  210. // Parsing ---------------------------------------------------------
  211. // Methods for parsing in protocol buffer format. Most of these are
  212. // just simple wrappers around MergeFromCodedStream(). Clear() will be
  213. // called before merging the input.
  214. // Fill the message with a protocol buffer parsed from the given input
  215. // stream. Returns false on a read error or if the input is in the wrong
  216. // format. A successful return does not indicate the entire input is
  217. // consumed, ensure you call ConsumedEntireMessage() to check that if
  218. // applicable.
  219. PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromCodedStream(
  220. io::CodedInputStream* input
  221. );
  222. // Like ParseFromCodedStream(), but accepts messages that are missing
  223. // required fields.
  224. PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromCodedStream(
  225. io::CodedInputStream* input
  226. );
  227. // Read a protocol buffer from the given zero-copy input stream. If
  228. // successful, the entire input will be consumed.
  229. PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromZeroCopyStream(
  230. io::ZeroCopyInputStream* input
  231. );
  232. // Like ParseFromZeroCopyStream(), but accepts messages that are missing
  233. // required fields.
  234. PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromZeroCopyStream(
  235. io::ZeroCopyInputStream* input
  236. );
  237. // Parse a protocol buffer from a file descriptor. If successful, the entire
  238. // input will be consumed.
  239. PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromFileDescriptor(
  240. int file_descriptor
  241. );
  242. // Like ParseFromFileDescriptor(), but accepts messages that are missing
  243. // required fields.
  244. PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromFileDescriptor(
  245. int file_descriptor
  246. );
  247. // Parse a protocol buffer from a C++ istream. If successful, the entire
  248. // input will be consumed.
  249. PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromIstream(std::istream* input);
  250. // Like ParseFromIstream(), but accepts messages that are missing
  251. // required fields.
  252. PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromIstream(
  253. std::istream* input
  254. );
  255. // Read a protocol buffer from the given zero-copy input stream, expecting
  256. // the message to be exactly "size" bytes long. If successful, exactly
  257. // this many bytes will have been consumed from the input.
  258. bool MergePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, int size);
  259. // Like ParseFromBoundedZeroCopyStream(), but accepts messages that are
  260. // missing required fields.
  261. bool MergeFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, int size);
  262. PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromBoundedZeroCopyStream(
  263. io::ZeroCopyInputStream* input, int size
  264. );
  265. // Like ParseFromBoundedZeroCopyStream(), but accepts messages that are
  266. // missing required fields.
  267. PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromBoundedZeroCopyStream(
  268. io::ZeroCopyInputStream* input, int size
  269. );
  270. // Parses a protocol buffer contained in a string. Returns true on success.
  271. // This function takes a string in the (non-human-readable) binary wire
  272. // format, matching the encoding output by MessageLite::SerializeToString().
  273. // If you'd like to convert a human-readable string into a protocol buffer
  274. // object, see google::protobuf::TextFormat::ParseFromString().
  275. PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromString(ConstStringParam data);
  276. // Like ParseFromString(), but accepts messages that are missing
  277. // required fields.
  278. PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromString(
  279. ConstStringParam data
  280. );
  281. // Parse a protocol buffer contained in an array of bytes.
  282. PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParseFromArray(const void* data, int size);
  283. // Like ParseFromArray(), but accepts messages that are missing
  284. // required fields.
  285. PROTOBUF_ATTRIBUTE_REINITIALIZES bool ParsePartialFromArray(const void* data, int size);
  286. // Reads a protocol buffer from the stream and merges it into this
  287. // Message. Singular fields read from the what is
  288. // already in the Message and repeated fields are appended to those
  289. // already present.
  290. //
  291. // It is the responsibility of the caller to call input->LastTagWas()
  292. // (for groups) or input->ConsumedEntireMessage() (for non-groups) after
  293. // this returns to verify that the message's end was delimited correctly.
  294. //
  295. // ParseFromCodedStream() is implemented as Clear() followed by
  296. // MergeFromCodedStream().
  297. bool MergeFromCodedStream(io::CodedInputStream* input);
  298. // Like MergeFromCodedStream(), but succeeds even if required fields are
  299. // missing in the input.
  300. //
  301. // MergeFromCodedStream() is just implemented as MergePartialFromCodedStream()
  302. // followed by IsInitialized().
  303. bool MergePartialFromCodedStream(io::CodedInputStream* input);
  304. // Merge a protocol buffer contained in a string.
  305. bool MergeFromString(ConstStringParam data);
  306. // Serialization ---------------------------------------------------
  307. // Methods for serializing in protocol buffer format. Most of these
  308. // are just simple wrappers around ByteSize() and SerializeWithCachedSizes().
  309. // Write a protocol buffer of this message to the given output. Returns
  310. // false on a write error. If the message is missing required fields,
  311. // this may GOOGLE_CHECK-fail.
  312. bool SerializeToCodedStream(io::CodedOutputStream* output) const;
  313. // Like SerializeToCodedStream(), but allows missing required fields.
  314. bool SerializePartialToCodedStream(io::CodedOutputStream* output) const;
  315. // Write the message to the given zero-copy output stream. All required
  316. // fields must be set.
  317. bool SerializeToZeroCopyStream(io::ZeroCopyOutputStream* output) const;
  318. // Like SerializeToZeroCopyStream(), but allows missing required fields.
  319. bool SerializePartialToZeroCopyStream(io::ZeroCopyOutputStream* output) const;
  320. // Serialize the message and store it in the given string. All required
  321. // fields must be set.
  322. bool SerializeToString(std::string* output) const;
  323. // Like SerializeToString(), but allows missing required fields.
  324. bool SerializePartialToString(std::string* output) const;
  325. // Serialize the message and store it in the given byte array. All required
  326. // fields must be set.
  327. bool SerializeToArray(void* data, int size) const;
  328. // Like SerializeToArray(), but allows missing required fields.
  329. bool SerializePartialToArray(void* data, int size) const;
  330. // Make a string encoding the message. Is equivalent to calling
  331. // SerializeToString() on a string and using that. Returns the empty
  332. // string if SerializeToString() would have returned an error.
  333. // Note: If you intend to generate many such strings, you may
  334. // reduce heap fragmentation by instead re-using the same string
  335. // object with calls to SerializeToString().
  336. std::string SerializeAsString() const;
  337. // Like SerializeAsString(), but allows missing required fields.
  338. std::string SerializePartialAsString() const;
  339. // Serialize the message and write it to the given file descriptor. All
  340. // required fields must be set.
  341. bool SerializeToFileDescriptor(int file_descriptor) const;
  342. // Like SerializeToFileDescriptor(), but allows missing required fields.
  343. bool SerializePartialToFileDescriptor(int file_descriptor) const;
  344. // Serialize the message and write it to the given C++ ostream. All
  345. // required fields must be set.
  346. bool SerializeToOstream(std::ostream* output) const;
  347. // Like SerializeToOstream(), but allows missing required fields.
  348. bool SerializePartialToOstream(std::ostream* output) const;
  349. // Like SerializeToString(), but appends to the data to the string's
  350. // existing contents. All required fields must be set.
  351. bool AppendToString(std::string* output) const;
  352. // Like AppendToString(), but allows missing required fields.
  353. bool AppendPartialToString(std::string* output) const;
  354. // Computes the serialized size of the message. This recursively calls
  355. // ByteSizeLong() on all embedded messages.
  356. //
  357. // ByteSizeLong() is generally linear in the number of fields defined for the
  358. // proto.
  359. virtual size_t ByteSizeLong() const = 0;
  360. // Legacy ByteSize() API.
  361. PROTOBUF_DEPRECATED_MSG("Please use ByteSizeLong() instead")
  362. int ByteSize() const
  363. {
  364. return internal::ToIntSize(ByteSizeLong());
  365. }
  366. // Serializes the message without recomputing the size. The message must not
  367. // have changed since the last call to ByteSize(), and the value returned by
  368. // ByteSize must be non-negative. Otherwise the results are undefined.
  369. void SerializeWithCachedSizes(io::CodedOutputStream* output) const
  370. {
  371. output->SetCur(_InternalSerialize(output->Cur(), output->EpsCopy()));
  372. }
  373. // Functions below here are not part of the public interface. It isn't
  374. // enforced, but they should be treated as private, and will be private
  375. // at some future time. Unfortunately the implementation of the "friend"
  376. // keyword in GCC is broken at the moment, but we expect it will be fixed.
  377. // Like SerializeWithCachedSizes, but writes directly to *target, returning
  378. // a pointer to the byte immediately after the last byte written. "target"
  379. // must point at a byte array of at least ByteSize() bytes. Whether to use
  380. // deterministic serialization, e.g., maps in sorted order, is determined by
  381. // CodedOutputStream::IsDefaultSerializationDeterministic().
  382. uint8_t* SerializeWithCachedSizesToArray(uint8_t* target) const;
  383. // Returns the result of the last call to ByteSize(). An embedded message's
  384. // size is needed both to serialize it (because embedded messages are
  385. // length-delimited) and to compute the outer message's size. Caching
  386. // the size avoids computing it multiple times.
  387. //
  388. // ByteSize() does not automatically use the cached size when available
  389. // because this would require invalidating it every time the message was
  390. // modified, which would be too hard and expensive. (E.g. if a deeply-nested
  391. // sub-message is changed, all of its parents' cached sizes would need to be
  392. // invalidated, which is too much work for an otherwise inlined setter
  393. // method.)
  394. virtual int GetCachedSize() const = 0;
  395. virtual const char* _InternalParse(const char* /*ptr*/, internal::ParseContext* /*ctx*/)
  396. {
  397. return nullptr;
  398. }
  399. virtual void OnDemandRegisterArenaDtor(Arena* /*arena*/)
  400. {
  401. }
  402. protected:
  403. template<typename T>
  404. static T* CreateMaybeMessage(Arena* arena)
  405. {
  406. return Arena::CreateMaybeMessage<T>(arena);
  407. }
  408. inline explicit MessageLite(Arena* arena, bool is_message_owned = false) :
  409. _internal_metadata_(arena, is_message_owned)
  410. {
  411. }
  412. // Returns the arena, if any, that directly owns this message and its internal
  413. // memory (Arena::Own is different in that the arena doesn't directly own the
  414. // internal memory). This method is used in proto's implementation for
  415. // swapping, moving and setting allocated, for deciding whether the ownership
  416. // of this message or its internal memory could be changed.
  417. Arena* GetOwningArena() const
  418. {
  419. return _internal_metadata_.owning_arena();
  420. }
  421. // Returns the arena, used for allocating internal objects(e.g., child
  422. // messages, etc), or owning incoming objects (e.g., set allocated).
  423. Arena* GetArenaForAllocation() const
  424. {
  425. return _internal_metadata_.arena();
  426. }
  427. // Returns true if this message is enabled for message-owned arena (MOA)
  428. // trials. No lite messages are eligible for MOA.
  429. static bool InMoaTrial()
  430. {
  431. return false;
  432. }
  433. internal::InternalMetadata _internal_metadata_;
  434. public:
  435. enum ParseFlags
  436. {
  437. kMerge = 0,
  438. kParse = 1,
  439. kMergePartial = 2,
  440. kParsePartial = 3,
  441. kMergeWithAliasing = 4,
  442. kParseWithAliasing = 5,
  443. kMergePartialWithAliasing = 6,
  444. kParsePartialWithAliasing = 7
  445. };
  446. template<ParseFlags flags, typename T>
  447. bool ParseFrom(const T& input);
  448. // Fast path when conditions match (ie. non-deterministic)
  449. // uint8_t* _InternalSerialize(uint8_t* ptr) const;
  450. virtual uint8_t* _InternalSerialize(
  451. uint8_t* ptr, io::EpsCopyOutputStream* stream
  452. ) const = 0;
  453. // Identical to IsInitialized() except that it logs an error message.
  454. bool IsInitializedWithErrors() const
  455. {
  456. if (IsInitialized())
  457. return true;
  458. LogInitializationErrorMessage();
  459. return false;
  460. }
  461. private:
  462. friend class FastReflectionMessageMutator;
  463. friend class FastReflectionStringSetter;
  464. friend class Message;
  465. friend class Reflection;
  466. friend class internal::ExtensionSet;
  467. friend class internal::LazyField;
  468. friend class internal::SwapFieldHelper;
  469. friend class internal::TcParser;
  470. friend class internal::WeakFieldMap;
  471. friend class internal::WireFormatLite;
  472. template<typename Type>
  473. friend class Arena::InternalHelper;
  474. template<typename Type>
  475. friend class internal::GenericTypeHandler;
  476. void LogInitializationErrorMessage() const;
  477. bool MergeFromImpl(io::CodedInputStream* input, ParseFlags parse_flags);
  478. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageLite);
  479. };
  480. namespace internal
  481. {
  482. template<bool alias>
  483. bool MergeFromImpl(StringPiece input, MessageLite* msg, MessageLite::ParseFlags parse_flags);
  484. extern template bool MergeFromImpl<false>(StringPiece input, MessageLite* msg, MessageLite::ParseFlags parse_flags);
  485. extern template bool MergeFromImpl<true>(StringPiece input, MessageLite* msg, MessageLite::ParseFlags parse_flags);
  486. template<bool alias>
  487. bool MergeFromImpl(io::ZeroCopyInputStream* input, MessageLite* msg, MessageLite::ParseFlags parse_flags);
  488. extern template bool MergeFromImpl<false>(io::ZeroCopyInputStream* input, MessageLite* msg, MessageLite::ParseFlags parse_flags);
  489. extern template bool MergeFromImpl<true>(io::ZeroCopyInputStream* input, MessageLite* msg, MessageLite::ParseFlags parse_flags);
  490. struct BoundedZCIS
  491. {
  492. io::ZeroCopyInputStream* zcis;
  493. int limit;
  494. };
  495. template<bool alias>
  496. bool MergeFromImpl(BoundedZCIS input, MessageLite* msg, MessageLite::ParseFlags parse_flags);
  497. extern template bool MergeFromImpl<false>(BoundedZCIS input, MessageLite* msg, MessageLite::ParseFlags parse_flags);
  498. extern template bool MergeFromImpl<true>(BoundedZCIS input, MessageLite* msg, MessageLite::ParseFlags parse_flags);
  499. template<typename T>
  500. struct SourceWrapper;
  501. template<bool alias, typename T>
  502. bool MergeFromImpl(const SourceWrapper<T>& input, MessageLite* msg, MessageLite::ParseFlags parse_flags)
  503. {
  504. return input.template MergeInto<alias>(msg, parse_flags);
  505. }
  506. } // namespace internal
  507. template<MessageLite::ParseFlags flags, typename T>
  508. bool MessageLite::ParseFrom(const T& input)
  509. {
  510. if (flags & kParse)
  511. Clear();
  512. constexpr bool alias = (flags & kMergeWithAliasing) != 0;
  513. return internal::MergeFromImpl<alias>(input, this, flags);
  514. }
  515. // ===================================================================
  516. // Shutdown support.
  517. // Shut down the entire protocol buffers library, deleting all static-duration
  518. // objects allocated by the library or by generated .pb.cc files.
  519. //
  520. // There are two reasons you might want to call this:
  521. // * You use a draconian definition of "memory leak" in which you expect
  522. // every single malloc() to have a corresponding free(), even for objects
  523. // which live until program exit.
  524. // * You are writing a dynamically-loaded library which needs to clean up
  525. // after itself when the library is unloaded.
  526. //
  527. // It is safe to call this multiple times. However, it is not safe to use
  528. // any other part of the protocol buffers library after
  529. // ShutdownProtobufLibrary() has been called. Furthermore this call is not
  530. // thread safe, user needs to synchronize multiple calls.
  531. PROTOBUF_EXPORT void ShutdownProtobufLibrary();
  532. namespace internal
  533. {
  534. // Register a function to be called when ShutdownProtocolBuffers() is called.
  535. PROTOBUF_EXPORT void OnShutdown(void (*func)());
  536. // Run an arbitrary function on an arg
  537. PROTOBUF_EXPORT void OnShutdownRun(void (*f)(const void*), const void* arg);
  538. template<typename T>
  539. T* OnShutdownDelete(T* p)
  540. {
  541. OnShutdownRun([](const void* pp)
  542. { delete static_cast<const T*>(pp); },
  543. p);
  544. return p;
  545. }
  546. } // namespace internal
  547. } // namespace protobuf
  548. } // namespace google
  549. #include <google/protobuf/port_undef.inc>
  550. #endif // GOOGLE_PROTOBUF_MESSAGE_LITE_H__