诸暨麻将添加redis
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

1267 righe
59 KiB

  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. // Defines Message, the abstract interface implemented by non-lite
  35. // protocol message objects. Although it's possible to implement this
  36. // interface manually, most users will use the protocol compiler to
  37. // generate implementations.
  38. //
  39. // Example usage:
  40. //
  41. // Say you have a message defined as:
  42. //
  43. // message Foo {
  44. // optional string text = 1;
  45. // repeated int32 numbers = 2;
  46. // }
  47. //
  48. // Then, if you used the protocol compiler to generate a class from the above
  49. // definition, you could use it like so:
  50. //
  51. // std::string data; // Will store a serialized version of the message.
  52. //
  53. // {
  54. // // Create a message and serialize it.
  55. // Foo foo;
  56. // foo.set_text("Hello World!");
  57. // foo.add_numbers(1);
  58. // foo.add_numbers(5);
  59. // foo.add_numbers(42);
  60. //
  61. // foo.SerializeToString(&data);
  62. // }
  63. //
  64. // {
  65. // // Parse the serialized message and check that it contains the
  66. // // correct data.
  67. // Foo foo;
  68. // foo.ParseFromString(data);
  69. //
  70. // assert(foo.text() == "Hello World!");
  71. // assert(foo.numbers_size() == 3);
  72. // assert(foo.numbers(0) == 1);
  73. // assert(foo.numbers(1) == 5);
  74. // assert(foo.numbers(2) == 42);
  75. // }
  76. //
  77. // {
  78. // // Same as the last block, but do it dynamically via the Message
  79. // // reflection interface.
  80. // Message* foo = new Foo;
  81. // const Descriptor* descriptor = foo->GetDescriptor();
  82. //
  83. // // Get the descriptors for the fields we're interested in and verify
  84. // // their types.
  85. // const FieldDescriptor* text_field = descriptor->FindFieldByName("text");
  86. // assert(text_field != nullptr);
  87. // assert(text_field->type() == FieldDescriptor::TYPE_STRING);
  88. // assert(text_field->label() == FieldDescriptor::LABEL_OPTIONAL);
  89. // const FieldDescriptor* numbers_field = descriptor->
  90. // FindFieldByName("numbers");
  91. // assert(numbers_field != nullptr);
  92. // assert(numbers_field->type() == FieldDescriptor::TYPE_INT32);
  93. // assert(numbers_field->label() == FieldDescriptor::LABEL_REPEATED);
  94. //
  95. // // Parse the message.
  96. // foo->ParseFromString(data);
  97. //
  98. // // Use the reflection interface to examine the contents.
  99. // const Reflection* reflection = foo->GetReflection();
  100. // assert(reflection->GetString(*foo, text_field) == "Hello World!");
  101. // assert(reflection->FieldSize(*foo, numbers_field) == 3);
  102. // assert(reflection->GetRepeatedInt32(*foo, numbers_field, 0) == 1);
  103. // assert(reflection->GetRepeatedInt32(*foo, numbers_field, 1) == 5);
  104. // assert(reflection->GetRepeatedInt32(*foo, numbers_field, 2) == 42);
  105. //
  106. // delete foo;
  107. // }
  108. #ifndef GOOGLE_PROTOBUF_MESSAGE_H__
  109. #define GOOGLE_PROTOBUF_MESSAGE_H__
  110. #include <iosfwd>
  111. #include <string>
  112. #include <type_traits>
  113. #include <vector>
  114. #include <google/protobuf/stubs/casts.h>
  115. #include <google/protobuf/stubs/common.h>
  116. #include <google/protobuf/arena.h>
  117. #include <google/protobuf/descriptor.h>
  118. #include <google/protobuf/generated_message_reflection.h>
  119. #include <google/protobuf/message_lite.h>
  120. #include <google/protobuf/port.h>
  121. #define GOOGLE_PROTOBUF_HAS_ONEOF
  122. #define GOOGLE_PROTOBUF_HAS_ARENAS
  123. #include <google/protobuf/port_def.inc>
  124. #ifdef SWIG
  125. #error "You cannot SWIG proto headers"
  126. #endif
  127. namespace google {
  128. namespace protobuf {
  129. // Defined in this file.
  130. class Message;
  131. class Reflection;
  132. class MessageFactory;
  133. // Defined in other files.
  134. class AssignDescriptorsHelper;
  135. class DynamicMessageFactory;
  136. class MapKey;
  137. class MapValueRef;
  138. class MapIterator;
  139. class MapReflectionTester;
  140. namespace internal {
  141. struct DescriptorTable;
  142. class MapFieldBase;
  143. }
  144. class UnknownFieldSet; // unknown_field_set.h
  145. namespace io {
  146. class ZeroCopyInputStream; // zero_copy_stream.h
  147. class ZeroCopyOutputStream; // zero_copy_stream.h
  148. class CodedInputStream; // coded_stream.h
  149. class CodedOutputStream; // coded_stream.h
  150. } // namespace io
  151. namespace python {
  152. class MapReflectionFriend; // scalar_map_container.h
  153. }
  154. namespace expr {
  155. class CelMapReflectionFriend; // field_backed_map_impl.cc
  156. }
  157. namespace internal {
  158. class MapFieldPrinterHelper; // text_format.cc
  159. }
  160. namespace internal {
  161. class ReflectionAccessor; // message.cc
  162. class ReflectionOps; // reflection_ops.h
  163. class MapKeySorter; // wire_format.cc
  164. class WireFormat; // wire_format.h
  165. class MapFieldReflectionTest; // map_test.cc
  166. } // namespace internal
  167. template <typename T>
  168. class RepeatedField; // repeated_field.h
  169. template <typename T>
  170. class RepeatedPtrField; // repeated_field.h
  171. // A container to hold message metadata.
  172. struct Metadata {
  173. const Descriptor* descriptor;
  174. const Reflection* reflection;
  175. };
  176. // Abstract interface for protocol messages.
  177. //
  178. // See also MessageLite, which contains most every-day operations. Message
  179. // adds descriptors and reflection on top of that.
  180. //
  181. // The methods of this class that are virtual but not pure-virtual have
  182. // default implementations based on reflection. Message classes which are
  183. // optimized for speed will want to override these with faster implementations,
  184. // but classes optimized for code size may be happy with keeping them. See
  185. // the optimize_for option in descriptor.proto.
  186. class PROTOBUF_EXPORT Message : public MessageLite {
  187. public:
  188. inline Message() {}
  189. ~Message() override {}
  190. // Basic Operations ------------------------------------------------
  191. // Construct a new instance of the same type. Ownership is passed to the
  192. // caller. (This is also defined in MessageLite, but is defined again here
  193. // for return-type covariance.)
  194. Message* New() const override = 0;
  195. // Construct a new instance on the arena. Ownership is passed to the caller
  196. // if arena is a nullptr. Default implementation allows for API compatibility
  197. // during the Arena transition.
  198. Message* New(Arena* arena) const override {
  199. Message* message = New();
  200. if (arena != nullptr) {
  201. arena->Own(message);
  202. }
  203. return message;
  204. }
  205. // Make this message into a copy of the given message. The given message
  206. // must have the same descriptor, but need not necessarily be the same class.
  207. // By default this is just implemented as "Clear(); MergeFrom(from);".
  208. virtual void CopyFrom(const Message& from);
  209. // Merge the fields from the given message into this message. Singular
  210. // fields will be overwritten, if specified in from, except for embedded
  211. // messages which will be merged. Repeated fields will be concatenated.
  212. // The given message must be of the same type as this message (i.e. the
  213. // exact same class).
  214. virtual void MergeFrom(const Message& from);
  215. // Verifies that IsInitialized() returns true. GOOGLE_CHECK-fails otherwise, with
  216. // a nice error message.
  217. void CheckInitialized() const;
  218. // Slowly build a list of all required fields that are not set.
  219. // This is much, much slower than IsInitialized() as it is implemented
  220. // purely via reflection. Generally, you should not call this unless you
  221. // have already determined that an error exists by calling IsInitialized().
  222. void FindInitializationErrors(std::vector<std::string>* errors) const;
  223. // Like FindInitializationErrors, but joins all the strings, delimited by
  224. // commas, and returns them.
  225. std::string InitializationErrorString() const override;
  226. // Clears all unknown fields from this message and all embedded messages.
  227. // Normally, if unknown tag numbers are encountered when parsing a message,
  228. // the tag and value are stored in the message's UnknownFieldSet and
  229. // then written back out when the message is serialized. This allows servers
  230. // which simply route messages to other servers to pass through messages
  231. // that have new field definitions which they don't yet know about. However,
  232. // this behavior can have security implications. To avoid it, call this
  233. // method after parsing.
  234. //
  235. // See Reflection::GetUnknownFields() for more on unknown fields.
  236. virtual void DiscardUnknownFields();
  237. // Computes (an estimate of) the total number of bytes currently used for
  238. // storing the message in memory. The default implementation calls the
  239. // Reflection object's SpaceUsed() method.
  240. //
  241. // SpaceUsed() is noticeably slower than ByteSize(), as it is implemented
  242. // using reflection (rather than the generated code implementation for
  243. // ByteSize()). Like ByteSize(), its CPU time is linear in the number of
  244. // fields defined for the proto.
  245. virtual size_t SpaceUsedLong() const;
  246. PROTOBUF_DEPRECATED_MSG("Please use SpaceUsedLong() instead")
  247. int SpaceUsed() const { return internal::ToIntSize(SpaceUsedLong()); }
  248. // Debugging & Testing----------------------------------------------
  249. // Generates a human readable form of this message, useful for debugging
  250. // and other purposes.
  251. std::string DebugString() const;
  252. // Like DebugString(), but with less whitespace.
  253. std::string ShortDebugString() const;
  254. // Like DebugString(), but do not escape UTF-8 byte sequences.
  255. std::string Utf8DebugString() const;
  256. // Convenience function useful in GDB. Prints DebugString() to stdout.
  257. void PrintDebugString() const;
  258. // Reflection-based methods ----------------------------------------
  259. // These methods are pure-virtual in MessageLite, but Message provides
  260. // reflection-based default implementations.
  261. std::string GetTypeName() const override;
  262. void Clear() override;
  263. // Returns whether all required fields have been set. Note that required
  264. // fields no longer exist starting in proto3.
  265. bool IsInitialized() const override;
  266. void CheckTypeAndMergeFrom(const MessageLite& other) override;
  267. // Reflective parser
  268. const char* _InternalParse(const char* ptr,
  269. internal::ParseContext* ctx) override;
  270. size_t ByteSizeLong() const override;
  271. uint8* InternalSerializeWithCachedSizesToArray(
  272. uint8* target, io::EpsCopyOutputStream* stream) const override;
  273. private:
  274. // This is called only by the default implementation of ByteSize(), to
  275. // update the cached size. If you override ByteSize(), you do not need
  276. // to override this. If you do not override ByteSize(), you MUST override
  277. // this; the default implementation will crash.
  278. //
  279. // The method is private because subclasses should never call it; only
  280. // override it. Yes, C++ lets you do that. Crazy, huh?
  281. virtual void SetCachedSize(int size) const;
  282. public:
  283. // Introspection ---------------------------------------------------
  284. // Get a non-owning pointer to a Descriptor for this message's type. This
  285. // describes what fields the message contains, the types of those fields, etc.
  286. // This object remains property of the Message.
  287. const Descriptor* GetDescriptor() const { return GetMetadata().descriptor; }
  288. // Get a non-owning pointer to the Reflection interface for this Message,
  289. // which can be used to read and modify the fields of the Message dynamically
  290. // (in other words, without knowing the message type at compile time). This
  291. // object remains property of the Message.
  292. const Reflection* GetReflection() const { return GetMetadata().reflection; }
  293. protected:
  294. // Get a struct containing the metadata for the Message, which is used in turn
  295. // to implement GetDescriptor() and GetReflection() above.
  296. virtual Metadata GetMetadata() const = 0;
  297. private:
  298. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Message);
  299. };
  300. namespace internal {
  301. // Forward-declare interfaces used to implement RepeatedFieldRef.
  302. // These are protobuf internals that users shouldn't care about.
  303. class RepeatedFieldAccessor;
  304. } // namespace internal
  305. // Forward-declare RepeatedFieldRef templates. The second type parameter is
  306. // used for SFINAE tricks. Users should ignore it.
  307. template <typename T, typename Enable = void>
  308. class RepeatedFieldRef;
  309. template <typename T, typename Enable = void>
  310. class MutableRepeatedFieldRef;
  311. // This interface contains methods that can be used to dynamically access
  312. // and modify the fields of a protocol message. Their semantics are
  313. // similar to the accessors the protocol compiler generates.
  314. //
  315. // To get the Reflection for a given Message, call Message::GetReflection().
  316. //
  317. // This interface is separate from Message only for efficiency reasons;
  318. // the vast majority of implementations of Message will share the same
  319. // implementation of Reflection (GeneratedMessageReflection,
  320. // defined in generated_message.h), and all Messages of a particular class
  321. // should share the same Reflection object (though you should not rely on
  322. // the latter fact).
  323. //
  324. // There are several ways that these methods can be used incorrectly. For
  325. // example, any of the following conditions will lead to undefined
  326. // results (probably assertion failures):
  327. // - The FieldDescriptor is not a field of this message type.
  328. // - The method called is not appropriate for the field's type. For
  329. // each field type in FieldDescriptor::TYPE_*, there is only one
  330. // Get*() method, one Set*() method, and one Add*() method that is
  331. // valid for that type. It should be obvious which (except maybe
  332. // for TYPE_BYTES, which are represented using strings in C++).
  333. // - A Get*() or Set*() method for singular fields is called on a repeated
  334. // field.
  335. // - GetRepeated*(), SetRepeated*(), or Add*() is called on a non-repeated
  336. // field.
  337. // - The Message object passed to any method is not of the right type for
  338. // this Reflection object (i.e. message.GetReflection() != reflection).
  339. //
  340. // You might wonder why there is not any abstract representation for a field
  341. // of arbitrary type. E.g., why isn't there just a "GetField()" method that
  342. // returns "const Field&", where "Field" is some class with accessors like
  343. // "GetInt32Value()". The problem is that someone would have to deal with
  344. // allocating these Field objects. For generated message classes, having to
  345. // allocate space for an additional object to wrap every field would at least
  346. // double the message's memory footprint, probably worse. Allocating the
  347. // objects on-demand, on the other hand, would be expensive and prone to
  348. // memory leaks. So, instead we ended up with this flat interface.
  349. class PROTOBUF_EXPORT Reflection final {
  350. public:
  351. // Get the UnknownFieldSet for the message. This contains fields which
  352. // were seen when the Message was parsed but were not recognized according
  353. // to the Message's definition.
  354. const UnknownFieldSet& GetUnknownFields(const Message& message) const;
  355. // Get a mutable pointer to the UnknownFieldSet for the message. This
  356. // contains fields which were seen when the Message was parsed but were not
  357. // recognized according to the Message's definition.
  358. UnknownFieldSet* MutableUnknownFields(Message* message) const;
  359. // Estimate the amount of memory used by the message object.
  360. size_t SpaceUsedLong(const Message& message) const;
  361. PROTOBUF_DEPRECATED_MSG("Please use SpaceUsedLong() instead")
  362. int SpaceUsed(const Message& message) const {
  363. return internal::ToIntSize(SpaceUsedLong(message));
  364. }
  365. // Check if the given non-repeated field is set.
  366. bool HasField(const Message& message, const FieldDescriptor* field) const;
  367. // Get the number of elements of a repeated field.
  368. int FieldSize(const Message& message, const FieldDescriptor* field) const;
  369. // Clear the value of a field, so that HasField() returns false or
  370. // FieldSize() returns zero.
  371. void ClearField(Message* message, const FieldDescriptor* field) const;
  372. // Check if the oneof is set. Returns true if any field in oneof
  373. // is set, false otherwise.
  374. bool HasOneof(const Message& message,
  375. const OneofDescriptor* oneof_descriptor) const;
  376. void ClearOneof(Message* message,
  377. const OneofDescriptor* oneof_descriptor) const;
  378. // Returns the field descriptor if the oneof is set. nullptr otherwise.
  379. const FieldDescriptor* GetOneofFieldDescriptor(
  380. const Message& message, const OneofDescriptor* oneof_descriptor) const;
  381. // Removes the last element of a repeated field.
  382. // We don't provide a way to remove any element other than the last
  383. // because it invites inefficient use, such as O(n^2) filtering loops
  384. // that should have been O(n). If you want to remove an element other
  385. // than the last, the best way to do it is to re-arrange the elements
  386. // (using Swap()) so that the one you want removed is at the end, then
  387. // call RemoveLast().
  388. void RemoveLast(Message* message, const FieldDescriptor* field) const;
  389. // Removes the last element of a repeated message field, and returns the
  390. // pointer to the caller. Caller takes ownership of the returned pointer.
  391. Message* ReleaseLast(Message* message, const FieldDescriptor* field) const;
  392. // Swap the complete contents of two messages.
  393. void Swap(Message* message1, Message* message2) const;
  394. // Swap fields listed in fields vector of two messages.
  395. void SwapFields(Message* message1, Message* message2,
  396. const std::vector<const FieldDescriptor*>& fields) const;
  397. // Swap two elements of a repeated field.
  398. void SwapElements(Message* message, const FieldDescriptor* field, int index1,
  399. int index2) const;
  400. // List all fields of the message which are currently set, except for unknown
  401. // fields, but including extension known to the parser (i.e. compiled in).
  402. // Singular fields will only be listed if HasField(field) would return true
  403. // and repeated fields will only be listed if FieldSize(field) would return
  404. // non-zero. Fields (both normal fields and extension fields) will be listed
  405. // ordered by field number.
  406. // Use Reflection::GetUnknownFields() or message.unknown_fields() to also get
  407. // access to fields/extensions unknown to the parser.
  408. void ListFields(const Message& message,
  409. std::vector<const FieldDescriptor*>* output) const;
  410. // Singular field getters ------------------------------------------
  411. // These get the value of a non-repeated field. They return the default
  412. // value for fields that aren't set.
  413. int32 GetInt32(const Message& message, const FieldDescriptor* field) const;
  414. int64 GetInt64(const Message& message, const FieldDescriptor* field) const;
  415. uint32 GetUInt32(const Message& message, const FieldDescriptor* field) const;
  416. uint64 GetUInt64(const Message& message, const FieldDescriptor* field) const;
  417. float GetFloat(const Message& message, const FieldDescriptor* field) const;
  418. double GetDouble(const Message& message, const FieldDescriptor* field) const;
  419. bool GetBool(const Message& message, const FieldDescriptor* field) const;
  420. std::string GetString(const Message& message,
  421. const FieldDescriptor* field) const;
  422. const EnumValueDescriptor* GetEnum(const Message& message,
  423. const FieldDescriptor* field) const;
  424. // GetEnumValue() returns an enum field's value as an integer rather than
  425. // an EnumValueDescriptor*. If the integer value does not correspond to a
  426. // known value descriptor, a new value descriptor is created. (Such a value
  427. // will only be present when the new unknown-enum-value semantics are enabled
  428. // for a message.)
  429. int GetEnumValue(const Message& message, const FieldDescriptor* field) const;
  430. // See MutableMessage() for the meaning of the "factory" parameter.
  431. const Message& GetMessage(const Message& message,
  432. const FieldDescriptor* field,
  433. MessageFactory* factory = nullptr) const;
  434. // Get a string value without copying, if possible.
  435. //
  436. // GetString() necessarily returns a copy of the string. This can be
  437. // inefficient when the std::string is already stored in a std::string object
  438. // in the underlying message. GetStringReference() will return a reference to
  439. // the underlying std::string in this case. Otherwise, it will copy the
  440. // string into *scratch and return that.
  441. //
  442. // Note: It is perfectly reasonable and useful to write code like:
  443. // str = reflection->GetStringReference(message, field, &str);
  444. // This line would ensure that only one copy of the string is made
  445. // regardless of the field's underlying representation. When initializing
  446. // a newly-constructed string, though, it's just as fast and more
  447. // readable to use code like:
  448. // std::string str = reflection->GetString(message, field);
  449. const std::string& GetStringReference(const Message& message,
  450. const FieldDescriptor* field,
  451. std::string* scratch) const;
  452. // Singular field mutators -----------------------------------------
  453. // These mutate the value of a non-repeated field.
  454. void SetInt32(Message* message, const FieldDescriptor* field,
  455. int32 value) const;
  456. void SetInt64(Message* message, const FieldDescriptor* field,
  457. int64 value) const;
  458. void SetUInt32(Message* message, const FieldDescriptor* field,
  459. uint32 value) const;
  460. void SetUInt64(Message* message, const FieldDescriptor* field,
  461. uint64 value) const;
  462. void SetFloat(Message* message, const FieldDescriptor* field,
  463. float value) const;
  464. void SetDouble(Message* message, const FieldDescriptor* field,
  465. double value) const;
  466. void SetBool(Message* message, const FieldDescriptor* field,
  467. bool value) const;
  468. void SetString(Message* message, const FieldDescriptor* field,
  469. std::string value) const;
  470. void SetEnum(Message* message, const FieldDescriptor* field,
  471. const EnumValueDescriptor* value) const;
  472. // Set an enum field's value with an integer rather than EnumValueDescriptor.
  473. // For proto3 this is just setting the enum field to the value specified, for
  474. // proto2 it's more complicated. If value is a known enum value the field is
  475. // set as usual. If the value is unknown then it is added to the unknown field
  476. // set. Note this matches the behavior of parsing unknown enum values.
  477. // If multiple calls with unknown values happen than they are all added to the
  478. // unknown field set in order of the calls.
  479. void SetEnumValue(Message* message, const FieldDescriptor* field,
  480. int value) const;
  481. // Get a mutable pointer to a field with a message type. If a MessageFactory
  482. // is provided, it will be used to construct instances of the sub-message;
  483. // otherwise, the default factory is used. If the field is an extension that
  484. // does not live in the same pool as the containing message's descriptor (e.g.
  485. // it lives in an overlay pool), then a MessageFactory must be provided.
  486. // If you have no idea what that meant, then you probably don't need to worry
  487. // about it (don't provide a MessageFactory). WARNING: If the
  488. // FieldDescriptor is for a compiled-in extension, then
  489. // factory->GetPrototype(field->message_type()) MUST return an instance of
  490. // the compiled-in class for this type, NOT DynamicMessage.
  491. Message* MutableMessage(Message* message, const FieldDescriptor* field,
  492. MessageFactory* factory = nullptr) const;
  493. // Replaces the message specified by 'field' with the already-allocated object
  494. // sub_message, passing ownership to the message. If the field contained a
  495. // message, that message is deleted. If sub_message is nullptr, the field is
  496. // cleared.
  497. void SetAllocatedMessage(Message* message, Message* sub_message,
  498. const FieldDescriptor* field) const;
  499. // Releases the message specified by 'field' and returns the pointer,
  500. // ReleaseMessage() will return the message the message object if it exists.
  501. // Otherwise, it may or may not return nullptr. In any case, if the return
  502. // value is non-null, the caller takes ownership of the pointer.
  503. // If the field existed (HasField() is true), then the returned pointer will
  504. // be the same as the pointer returned by MutableMessage().
  505. // This function has the same effect as ClearField().
  506. Message* ReleaseMessage(Message* message, const FieldDescriptor* field,
  507. MessageFactory* factory = nullptr) const;
  508. // Repeated field getters ------------------------------------------
  509. // These get the value of one element of a repeated field.
  510. int32 GetRepeatedInt32(const Message& message, const FieldDescriptor* field,
  511. int index) const;
  512. int64 GetRepeatedInt64(const Message& message, const FieldDescriptor* field,
  513. int index) const;
  514. uint32 GetRepeatedUInt32(const Message& message, const FieldDescriptor* field,
  515. int index) const;
  516. uint64 GetRepeatedUInt64(const Message& message, const FieldDescriptor* field,
  517. int index) const;
  518. float GetRepeatedFloat(const Message& message, const FieldDescriptor* field,
  519. int index) const;
  520. double GetRepeatedDouble(const Message& message, const FieldDescriptor* field,
  521. int index) const;
  522. bool GetRepeatedBool(const Message& message, const FieldDescriptor* field,
  523. int index) const;
  524. std::string GetRepeatedString(const Message& message,
  525. const FieldDescriptor* field, int index) const;
  526. const EnumValueDescriptor* GetRepeatedEnum(const Message& message,
  527. const FieldDescriptor* field,
  528. int index) const;
  529. // GetRepeatedEnumValue() returns an enum field's value as an integer rather
  530. // than an EnumValueDescriptor*. If the integer value does not correspond to a
  531. // known value descriptor, a new value descriptor is created. (Such a value
  532. // will only be present when the new unknown-enum-value semantics are enabled
  533. // for a message.)
  534. int GetRepeatedEnumValue(const Message& message, const FieldDescriptor* field,
  535. int index) const;
  536. const Message& GetRepeatedMessage(const Message& message,
  537. const FieldDescriptor* field,
  538. int index) const;
  539. // See GetStringReference(), above.
  540. const std::string& GetRepeatedStringReference(const Message& message,
  541. const FieldDescriptor* field,
  542. int index,
  543. std::string* scratch) const;
  544. // Repeated field mutators -----------------------------------------
  545. // These mutate the value of one element of a repeated field.
  546. void SetRepeatedInt32(Message* message, const FieldDescriptor* field,
  547. int index, int32 value) const;
  548. void SetRepeatedInt64(Message* message, const FieldDescriptor* field,
  549. int index, int64 value) const;
  550. void SetRepeatedUInt32(Message* message, const FieldDescriptor* field,
  551. int index, uint32 value) const;
  552. void SetRepeatedUInt64(Message* message, const FieldDescriptor* field,
  553. int index, uint64 value) const;
  554. void SetRepeatedFloat(Message* message, const FieldDescriptor* field,
  555. int index, float value) const;
  556. void SetRepeatedDouble(Message* message, const FieldDescriptor* field,
  557. int index, double value) const;
  558. void SetRepeatedBool(Message* message, const FieldDescriptor* field,
  559. int index, bool value) const;
  560. void SetRepeatedString(Message* message, const FieldDescriptor* field,
  561. int index, std::string value) const;
  562. void SetRepeatedEnum(Message* message, const FieldDescriptor* field,
  563. int index, const EnumValueDescriptor* value) const;
  564. // Set an enum field's value with an integer rather than EnumValueDescriptor.
  565. // For proto3 this is just setting the enum field to the value specified, for
  566. // proto2 it's more complicated. If value is a known enum value the field is
  567. // set as usual. If the value is unknown then it is added to the unknown field
  568. // set. Note this matches the behavior of parsing unknown enum values.
  569. // If multiple calls with unknown values happen than they are all added to the
  570. // unknown field set in order of the calls.
  571. void SetRepeatedEnumValue(Message* message, const FieldDescriptor* field,
  572. int index, int value) const;
  573. // Get a mutable pointer to an element of a repeated field with a message
  574. // type.
  575. Message* MutableRepeatedMessage(Message* message,
  576. const FieldDescriptor* field,
  577. int index) const;
  578. // Repeated field adders -------------------------------------------
  579. // These add an element to a repeated field.
  580. void AddInt32(Message* message, const FieldDescriptor* field,
  581. int32 value) const;
  582. void AddInt64(Message* message, const FieldDescriptor* field,
  583. int64 value) const;
  584. void AddUInt32(Message* message, const FieldDescriptor* field,
  585. uint32 value) const;
  586. void AddUInt64(Message* message, const FieldDescriptor* field,
  587. uint64 value) const;
  588. void AddFloat(Message* message, const FieldDescriptor* field,
  589. float value) const;
  590. void AddDouble(Message* message, const FieldDescriptor* field,
  591. double value) const;
  592. void AddBool(Message* message, const FieldDescriptor* field,
  593. bool value) const;
  594. void AddString(Message* message, const FieldDescriptor* field,
  595. std::string value) const;
  596. void AddEnum(Message* message, const FieldDescriptor* field,
  597. const EnumValueDescriptor* value) const;
  598. // Add an integer value to a repeated enum field rather than
  599. // EnumValueDescriptor. For proto3 this is just setting the enum field to the
  600. // value specified, for proto2 it's more complicated. If value is a known enum
  601. // value the field is set as usual. If the value is unknown then it is added
  602. // to the unknown field set. Note this matches the behavior of parsing unknown
  603. // enum values. If multiple calls with unknown values happen than they are all
  604. // added to the unknown field set in order of the calls.
  605. void AddEnumValue(Message* message, const FieldDescriptor* field,
  606. int value) const;
  607. // See MutableMessage() for comments on the "factory" parameter.
  608. Message* AddMessage(Message* message, const FieldDescriptor* field,
  609. MessageFactory* factory = nullptr) const;
  610. // Appends an already-allocated object 'new_entry' to the repeated field
  611. // specified by 'field' passing ownership to the message.
  612. void AddAllocatedMessage(Message* message, const FieldDescriptor* field,
  613. Message* new_entry) const;
  614. // Get a RepeatedFieldRef object that can be used to read the underlying
  615. // repeated field. The type parameter T must be set according to the
  616. // field's cpp type. The following table shows the mapping from cpp type
  617. // to acceptable T.
  618. //
  619. // field->cpp_type() T
  620. // CPPTYPE_INT32 int32
  621. // CPPTYPE_UINT32 uint32
  622. // CPPTYPE_INT64 int64
  623. // CPPTYPE_UINT64 uint64
  624. // CPPTYPE_DOUBLE double
  625. // CPPTYPE_FLOAT float
  626. // CPPTYPE_BOOL bool
  627. // CPPTYPE_ENUM generated enum type or int32
  628. // CPPTYPE_STRING std::string
  629. // CPPTYPE_MESSAGE generated message type or google::protobuf::Message
  630. //
  631. // A RepeatedFieldRef object can be copied and the resulted object will point
  632. // to the same repeated field in the same message. The object can be used as
  633. // long as the message is not destroyed.
  634. //
  635. // Note that to use this method users need to include the header file
  636. // "net/proto2/public/reflection.h" (which defines the RepeatedFieldRef
  637. // class templates).
  638. template <typename T>
  639. RepeatedFieldRef<T> GetRepeatedFieldRef(const Message& message,
  640. const FieldDescriptor* field) const;
  641. // Like GetRepeatedFieldRef() but return an object that can also be used
  642. // manipulate the underlying repeated field.
  643. template <typename T>
  644. MutableRepeatedFieldRef<T> GetMutableRepeatedFieldRef(
  645. Message* message, const FieldDescriptor* field) const;
  646. // DEPRECATED. Please use Get(Mutable)RepeatedFieldRef() for repeated field
  647. // access. The following repeated field accesors will be removed in the
  648. // future.
  649. //
  650. // Repeated field accessors -------------------------------------------------
  651. // The methods above, e.g. GetRepeatedInt32(msg, fd, index), provide singular
  652. // access to the data in a RepeatedField. The methods below provide aggregate
  653. // access by exposing the RepeatedField object itself with the Message.
  654. // Applying these templates to inappropriate types will lead to an undefined
  655. // reference at link time (e.g. GetRepeatedField<***double>), or possibly a
  656. // template matching error at compile time (e.g. GetRepeatedPtrField<File>).
  657. //
  658. // Usage example: my_doubs = refl->GetRepeatedField<double>(msg, fd);
  659. // DEPRECATED. Please use GetRepeatedFieldRef().
  660. //
  661. // for T = Cord and all protobuf scalar types except enums.
  662. template <typename T>
  663. PROTOBUF_DEPRECATED_MSG("Please use GetRepeatedFieldRef() instead")
  664. const RepeatedField<T>& GetRepeatedField(const Message&,
  665. const FieldDescriptor*) const;
  666. // DEPRECATED. Please use GetMutableRepeatedFieldRef().
  667. //
  668. // for T = Cord and all protobuf scalar types except enums.
  669. template <typename T>
  670. PROTOBUF_DEPRECATED_MSG("Please use GetMutableRepeatedFieldRef() instead")
  671. RepeatedField<T>* MutableRepeatedField(Message*,
  672. const FieldDescriptor*) const;
  673. // DEPRECATED. Please use GetRepeatedFieldRef().
  674. //
  675. // for T = std::string, google::protobuf::internal::StringPieceField
  676. // google::protobuf::Message & descendants.
  677. template <typename T>
  678. PROTOBUF_DEPRECATED_MSG("Please use GetRepeatedFieldRef() instead")
  679. const RepeatedPtrField<T>& GetRepeatedPtrField(const Message&,
  680. const FieldDescriptor*) const;
  681. // DEPRECATED. Please use GetMutableRepeatedFieldRef().
  682. //
  683. // for T = std::string, google::protobuf::internal::StringPieceField
  684. // google::protobuf::Message & descendants.
  685. template <typename T>
  686. PROTOBUF_DEPRECATED_MSG("Please use GetMutableRepeatedFieldRef() instead")
  687. RepeatedPtrField<T>* MutableRepeatedPtrField(Message*,
  688. const FieldDescriptor*) const;
  689. // Extensions ----------------------------------------------------------------
  690. // Try to find an extension of this message type by fully-qualified field
  691. // name. Returns nullptr if no extension is known for this name or number.
  692. PROTOBUF_DEPRECATED_MSG(
  693. "Please use DescriptorPool::FindExtensionByPrintableName instead")
  694. const FieldDescriptor* FindKnownExtensionByName(
  695. const std::string& name) const;
  696. // Try to find an extension of this message type by field number.
  697. // Returns nullptr if no extension is known for this name or number.
  698. PROTOBUF_DEPRECATED_MSG(
  699. "Please use DescriptorPool::FindExtensionByNumber instead")
  700. const FieldDescriptor* FindKnownExtensionByNumber(int number) const;
  701. // Feature Flags -------------------------------------------------------------
  702. // Does this message support storing arbitrary integer values in enum fields?
  703. // If |true|, GetEnumValue/SetEnumValue and associated repeated-field versions
  704. // take arbitrary integer values, and the legacy GetEnum() getter will
  705. // dynamically create an EnumValueDescriptor for any integer value without
  706. // one. If |false|, setting an unknown enum value via the integer-based
  707. // setters results in undefined behavior (in practice, GOOGLE_DCHECK-fails).
  708. //
  709. // Generic code that uses reflection to handle messages with enum fields
  710. // should check this flag before using the integer-based setter, and either
  711. // downgrade to a compatible value or use the UnknownFieldSet if not. For
  712. // example:
  713. //
  714. // int new_value = GetValueFromApplicationLogic();
  715. // if (reflection->SupportsUnknownEnumValues()) {
  716. // reflection->SetEnumValue(message, field, new_value);
  717. // } else {
  718. // if (field_descriptor->enum_type()->
  719. // FindValueByNumber(new_value) != nullptr) {
  720. // reflection->SetEnumValue(message, field, new_value);
  721. // } else if (emit_unknown_enum_values) {
  722. // reflection->MutableUnknownFields(message)->AddVarint(
  723. // field->number(), new_value);
  724. // } else {
  725. // // convert value to a compatible/default value.
  726. // new_value = CompatibleDowngrade(new_value);
  727. // reflection->SetEnumValue(message, field, new_value);
  728. // }
  729. // }
  730. bool SupportsUnknownEnumValues() const;
  731. // Returns the MessageFactory associated with this message. This can be
  732. // useful for determining if a message is a generated message or not, for
  733. // example:
  734. // if (message->GetReflection()->GetMessageFactory() ==
  735. // google::protobuf::MessageFactory::generated_factory()) {
  736. // // This is a generated message.
  737. // }
  738. // It can also be used to create more messages of this type, though
  739. // Message::New() is an easier way to accomplish this.
  740. MessageFactory* GetMessageFactory() const;
  741. private:
  742. // Obtain a pointer to a Repeated Field Structure and do some type checking:
  743. // on field->cpp_type(),
  744. // on field->field_option().ctype() (if ctype >= 0)
  745. // of field->message_type() (if message_type != nullptr).
  746. // We use 2 routine rather than 4 (const vs mutable) x (scalar vs pointer).
  747. void* MutableRawRepeatedField(Message* message, const FieldDescriptor* field,
  748. FieldDescriptor::CppType, int ctype,
  749. const Descriptor* message_type) const;
  750. const void* GetRawRepeatedField(const Message& message,
  751. const FieldDescriptor* field,
  752. FieldDescriptor::CppType cpptype, int ctype,
  753. const Descriptor* message_type) const;
  754. // The following methods are used to implement (Mutable)RepeatedFieldRef.
  755. // A Ref object will store a raw pointer to the repeated field data (obtained
  756. // from RepeatedFieldData()) and a pointer to a Accessor (obtained from
  757. // RepeatedFieldAccessor) which will be used to access the raw data.
  758. // Returns a raw pointer to the repeated field
  759. //
  760. // "cpp_type" and "message_type" are deduced from the type parameter T passed
  761. // to Get(Mutable)RepeatedFieldRef. If T is a generated message type,
  762. // "message_type" should be set to its descriptor. Otherwise "message_type"
  763. // should be set to nullptr. Implementations of this method should check
  764. // whether "cpp_type"/"message_type" is consistent with the actual type of the
  765. // field. We use 1 routine rather than 2 (const vs mutable) because it is
  766. // protected and it doesn't change the message.
  767. void* RepeatedFieldData(Message* message, const FieldDescriptor* field,
  768. FieldDescriptor::CppType cpp_type,
  769. const Descriptor* message_type) const;
  770. // The returned pointer should point to a singleton instance which implements
  771. // the RepeatedFieldAccessor interface.
  772. const internal::RepeatedFieldAccessor* RepeatedFieldAccessor(
  773. const FieldDescriptor* field) const;
  774. const Descriptor* const descriptor_;
  775. const internal::ReflectionSchema schema_;
  776. const DescriptorPool* const descriptor_pool_;
  777. MessageFactory* const message_factory_;
  778. // Last non weak field index. This is an optimization when most weak fields
  779. // are at the end of the containing message. If a message proto doesn't
  780. // contain weak fields, then this field equals descriptor_->field_count().
  781. int last_non_weak_field_index_;
  782. template <typename T, typename Enable>
  783. friend class RepeatedFieldRef;
  784. template <typename T, typename Enable>
  785. friend class MutableRepeatedFieldRef;
  786. friend class ::PROTOBUF_NAMESPACE_ID::MessageLayoutInspector;
  787. friend class ::PROTOBUF_NAMESPACE_ID::AssignDescriptorsHelper;
  788. friend class DynamicMessageFactory;
  789. friend class python::MapReflectionFriend;
  790. #define GOOGLE_PROTOBUF_HAS_CEL_MAP_REFLECTION_FRIEND
  791. friend class expr::CelMapReflectionFriend;
  792. friend class internal::MapFieldReflectionTest;
  793. friend class internal::MapKeySorter;
  794. friend class internal::WireFormat;
  795. friend class internal::ReflectionOps;
  796. // Needed for implementing text format for map.
  797. friend class internal::MapFieldPrinterHelper;
  798. friend class internal::ReflectionAccessor;
  799. Reflection(const Descriptor* descriptor,
  800. const internal::ReflectionSchema& schema,
  801. const DescriptorPool* pool, MessageFactory* factory);
  802. // Special version for specialized implementations of string. We can't
  803. // call MutableRawRepeatedField directly here because we don't have access to
  804. // FieldOptions::* which are defined in descriptor.pb.h. Including that
  805. // file here is not possible because it would cause a circular include cycle.
  806. // We use 1 routine rather than 2 (const vs mutable) because it is private
  807. // and mutable a repeated string field doesn't change the message.
  808. void* MutableRawRepeatedString(Message* message, const FieldDescriptor* field,
  809. bool is_string) const;
  810. friend class MapReflectionTester;
  811. // Returns true if key is in map. Returns false if key is not in map field.
  812. bool ContainsMapKey(const Message& message, const FieldDescriptor* field,
  813. const MapKey& key) const;
  814. // If key is in map field: Saves the value pointer to val and returns
  815. // false. If key in not in map field: Insert the key into map, saves
  816. // value pointer to val and retuns true.
  817. bool InsertOrLookupMapValue(Message* message, const FieldDescriptor* field,
  818. const MapKey& key, MapValueRef* val) const;
  819. // Delete and returns true if key is in the map field. Returns false
  820. // otherwise.
  821. bool DeleteMapValue(Message* message, const FieldDescriptor* field,
  822. const MapKey& key) const;
  823. // Returns a MapIterator referring to the first element in the map field.
  824. // If the map field is empty, this function returns the same as
  825. // reflection::MapEnd. Mutation to the field may invalidate the iterator.
  826. MapIterator MapBegin(Message* message, const FieldDescriptor* field) const;
  827. // Returns a MapIterator referring to the theoretical element that would
  828. // follow the last element in the map field. It does not point to any
  829. // real element. Mutation to the field may invalidate the iterator.
  830. MapIterator MapEnd(Message* message, const FieldDescriptor* field) const;
  831. // Get the number of <key, value> pair of a map field. The result may be
  832. // different from FieldSize which can have duplicate keys.
  833. int MapSize(const Message& message, const FieldDescriptor* field) const;
  834. // Help method for MapIterator.
  835. friend class MapIterator;
  836. internal::MapFieldBase* MutableMapData(Message* message,
  837. const FieldDescriptor* field) const;
  838. const internal::MapFieldBase* GetMapData(const Message& message,
  839. const FieldDescriptor* field) const;
  840. template <class T>
  841. const T& GetRawNonOneof(const Message& message,
  842. const FieldDescriptor* field) const;
  843. template <class T>
  844. T* MutableRawNonOneof(Message* message, const FieldDescriptor* field) const;
  845. template <typename Type>
  846. const Type& GetRaw(const Message& message,
  847. const FieldDescriptor* field) const;
  848. template <typename Type>
  849. inline Type* MutableRaw(Message* message, const FieldDescriptor* field) const;
  850. template <typename Type>
  851. inline const Type& DefaultRaw(const FieldDescriptor* field) const;
  852. inline const uint32* GetHasBits(const Message& message) const;
  853. inline uint32* MutableHasBits(Message* message) const;
  854. inline uint32 GetOneofCase(const Message& message,
  855. const OneofDescriptor* oneof_descriptor) const;
  856. inline uint32* MutableOneofCase(
  857. Message* message, const OneofDescriptor* oneof_descriptor) const;
  858. inline const internal::ExtensionSet& GetExtensionSet(
  859. const Message& message) const;
  860. inline internal::ExtensionSet* MutableExtensionSet(Message* message) const;
  861. inline Arena* GetArena(Message* message) const;
  862. inline const internal::InternalMetadataWithArena&
  863. GetInternalMetadataWithArena(const Message& message) const;
  864. internal::InternalMetadataWithArena* MutableInternalMetadataWithArena(
  865. Message* message) const;
  866. inline bool IsInlined(const FieldDescriptor* field) const;
  867. inline bool HasBit(const Message& message,
  868. const FieldDescriptor* field) const;
  869. inline void SetBit(Message* message, const FieldDescriptor* field) const;
  870. inline void ClearBit(Message* message, const FieldDescriptor* field) const;
  871. inline void SwapBit(Message* message1, Message* message2,
  872. const FieldDescriptor* field) const;
  873. // This function only swaps the field. Should swap corresponding has_bit
  874. // before or after using this function.
  875. void SwapField(Message* message1, Message* message2,
  876. const FieldDescriptor* field) const;
  877. void SwapOneofField(Message* message1, Message* message2,
  878. const OneofDescriptor* oneof_descriptor) const;
  879. inline bool HasOneofField(const Message& message,
  880. const FieldDescriptor* field) const;
  881. inline void SetOneofCase(Message* message,
  882. const FieldDescriptor* field) const;
  883. inline void ClearOneofField(Message* message,
  884. const FieldDescriptor* field) const;
  885. template <typename Type>
  886. inline const Type& GetField(const Message& message,
  887. const FieldDescriptor* field) const;
  888. template <typename Type>
  889. inline void SetField(Message* message, const FieldDescriptor* field,
  890. const Type& value) const;
  891. template <typename Type>
  892. inline Type* MutableField(Message* message,
  893. const FieldDescriptor* field) const;
  894. template <typename Type>
  895. inline const Type& GetRepeatedField(const Message& message,
  896. const FieldDescriptor* field,
  897. int index) const;
  898. template <typename Type>
  899. inline const Type& GetRepeatedPtrField(const Message& message,
  900. const FieldDescriptor* field,
  901. int index) const;
  902. template <typename Type>
  903. inline void SetRepeatedField(Message* message, const FieldDescriptor* field,
  904. int index, Type value) const;
  905. template <typename Type>
  906. inline Type* MutableRepeatedField(Message* message,
  907. const FieldDescriptor* field,
  908. int index) const;
  909. template <typename Type>
  910. inline void AddField(Message* message, const FieldDescriptor* field,
  911. const Type& value) const;
  912. template <typename Type>
  913. inline Type* AddField(Message* message, const FieldDescriptor* field) const;
  914. int GetExtensionNumberOrDie(const Descriptor* type) const;
  915. // Internal versions of EnumValue API perform no checking. Called after checks
  916. // by public methods.
  917. void SetEnumValueInternal(Message* message, const FieldDescriptor* field,
  918. int value) const;
  919. void SetRepeatedEnumValueInternal(Message* message,
  920. const FieldDescriptor* field, int index,
  921. int value) const;
  922. void AddEnumValueInternal(Message* message, const FieldDescriptor* field,
  923. int value) const;
  924. Message* UnsafeArenaReleaseMessage(Message* message,
  925. const FieldDescriptor* field,
  926. MessageFactory* factory = nullptr) const;
  927. void UnsafeArenaSetAllocatedMessage(Message* message, Message* sub_message,
  928. const FieldDescriptor* field) const;
  929. friend inline // inline so nobody can call this function.
  930. void
  931. RegisterAllTypesInternal(const Metadata* file_level_metadata, int size);
  932. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Reflection);
  933. };
  934. // Abstract interface for a factory for message objects.
  935. class PROTOBUF_EXPORT MessageFactory {
  936. public:
  937. inline MessageFactory() {}
  938. virtual ~MessageFactory();
  939. // Given a Descriptor, gets or constructs the default (prototype) Message
  940. // of that type. You can then call that message's New() method to construct
  941. // a mutable message of that type.
  942. //
  943. // Calling this method twice with the same Descriptor returns the same
  944. // object. The returned object remains property of the factory. Also, any
  945. // objects created by calling the prototype's New() method share some data
  946. // with the prototype, so these must be destroyed before the MessageFactory
  947. // is destroyed.
  948. //
  949. // The given descriptor must outlive the returned message, and hence must
  950. // outlive the MessageFactory.
  951. //
  952. // Some implementations do not support all types. GetPrototype() will
  953. // return nullptr if the descriptor passed in is not supported.
  954. //
  955. // This method may or may not be thread-safe depending on the implementation.
  956. // Each implementation should document its own degree thread-safety.
  957. virtual const Message* GetPrototype(const Descriptor* type) = 0;
  958. // Gets a MessageFactory which supports all generated, compiled-in messages.
  959. // In other words, for any compiled-in type FooMessage, the following is true:
  960. // MessageFactory::generated_factory()->GetPrototype(
  961. // FooMessage::descriptor()) == FooMessage::default_instance()
  962. // This factory supports all types which are found in
  963. // DescriptorPool::generated_pool(). If given a descriptor from any other
  964. // pool, GetPrototype() will return nullptr. (You can also check if a
  965. // descriptor is for a generated message by checking if
  966. // descriptor->file()->pool() == DescriptorPool::generated_pool().)
  967. //
  968. // This factory is 100% thread-safe; calling GetPrototype() does not modify
  969. // any shared data.
  970. //
  971. // This factory is a singleton. The caller must not delete the object.
  972. static MessageFactory* generated_factory();
  973. // For internal use only: Registers a .proto file at static initialization
  974. // time, to be placed in generated_factory. The first time GetPrototype()
  975. // is called with a descriptor from this file, |register_messages| will be
  976. // called, with the file name as the parameter. It must call
  977. // InternalRegisterGeneratedMessage() (below) to register each message type
  978. // in the file. This strange mechanism is necessary because descriptors are
  979. // built lazily, so we can't register types by their descriptor until we
  980. // know that the descriptor exists. |filename| must be a permanent string.
  981. static void InternalRegisterGeneratedFile(
  982. const google::protobuf::internal::DescriptorTable* table);
  983. // For internal use only: Registers a message type. Called only by the
  984. // functions which are registered with InternalRegisterGeneratedFile(),
  985. // above.
  986. static void InternalRegisterGeneratedMessage(const Descriptor* descriptor,
  987. const Message* prototype);
  988. private:
  989. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageFactory);
  990. };
  991. #define DECLARE_GET_REPEATED_FIELD(TYPE) \
  992. template <> \
  993. PROTOBUF_EXPORT const RepeatedField<TYPE>& \
  994. Reflection::GetRepeatedField<TYPE>(const Message& message, \
  995. const FieldDescriptor* field) const; \
  996. \
  997. template <> \
  998. PROTOBUF_EXPORT RepeatedField<TYPE>* Reflection::MutableRepeatedField<TYPE>( \
  999. Message * message, const FieldDescriptor* field) const;
  1000. DECLARE_GET_REPEATED_FIELD(int32)
  1001. DECLARE_GET_REPEATED_FIELD(int64)
  1002. DECLARE_GET_REPEATED_FIELD(uint32)
  1003. DECLARE_GET_REPEATED_FIELD(uint64)
  1004. DECLARE_GET_REPEATED_FIELD(float)
  1005. DECLARE_GET_REPEATED_FIELD(double)
  1006. DECLARE_GET_REPEATED_FIELD(bool)
  1007. #undef DECLARE_GET_REPEATED_FIELD
  1008. // Tries to downcast this message to a generated message type. Returns nullptr
  1009. // if this class is not an instance of T. This works even if RTTI is disabled.
  1010. //
  1011. // This also has the effect of creating a strong reference to T that will
  1012. // prevent the linker from stripping it out at link time. This can be important
  1013. // if you are using a DynamicMessageFactory that delegates to the generated
  1014. // factory.
  1015. template <typename T>
  1016. const T* DynamicCastToGenerated(const Message* from) {
  1017. // Compile-time assert that T is a generated type that has a
  1018. // default_instance() accessor, but avoid actually calling it.
  1019. const T& (*get_default_instance)() = &T::default_instance;
  1020. (void)get_default_instance;
  1021. // Compile-time assert that T is a subclass of google::protobuf::Message.
  1022. const Message* unused = static_cast<T*>(nullptr);
  1023. (void)unused;
  1024. #ifdef GOOGLE_PROTOBUF_NO_RTTI
  1025. bool ok = T::default_instance().GetReflection() == from->GetReflection();
  1026. return ok ? down_cast<const T*>(from) : nullptr;
  1027. #else
  1028. return dynamic_cast<const T*>(from);
  1029. #endif
  1030. }
  1031. template <typename T>
  1032. T* DynamicCastToGenerated(Message* from) {
  1033. const Message* message_const = from;
  1034. return const_cast<T*>(DynamicCastToGenerated<T>(message_const));
  1035. }
  1036. // Call this function to ensure that this message's reflection is linked into
  1037. // the binary:
  1038. //
  1039. // google::protobuf::LinkMessageReflection<FooMessage>();
  1040. //
  1041. // This will ensure that the following lookup will succeed:
  1042. //
  1043. // DescriptorPool::generated_pool()->FindMessageTypeByName("FooMessage");
  1044. //
  1045. // As a side-effect, it will also guarantee that anything else from the same
  1046. // .proto file will also be available for lookup in the generated pool.
  1047. //
  1048. // This function does not actually register the message, so it does not need
  1049. // to be called before the lookup. However it does need to occur in a function
  1050. // that cannot be stripped from the binary (ie. it must be reachable from main).
  1051. //
  1052. // Best practice is to call this function as close as possible to where the
  1053. // reflection is actually needed. This function is very cheap to call, so you
  1054. // should not need to worry about its runtime overhead except in the tightest
  1055. // of loops (on x86-64 it compiles into two "mov" instructions).
  1056. template <typename T>
  1057. void LinkMessageReflection() {
  1058. internal::StrongReference(T::default_instance);
  1059. }
  1060. // =============================================================================
  1061. // Implementation details for {Get,Mutable}RawRepeatedPtrField. We provide
  1062. // specializations for <std::string>, <StringPieceField> and <Message> and
  1063. // handle everything else with the default template which will match any type
  1064. // having a method with signature "static const google::protobuf::Descriptor*
  1065. // descriptor()". Such a type presumably is a descendant of google::protobuf::Message.
  1066. template <>
  1067. inline const RepeatedPtrField<std::string>&
  1068. Reflection::GetRepeatedPtrField<std::string>(
  1069. const Message& message, const FieldDescriptor* field) const {
  1070. return *static_cast<RepeatedPtrField<std::string>*>(
  1071. MutableRawRepeatedString(const_cast<Message*>(&message), field, true));
  1072. }
  1073. template <>
  1074. inline RepeatedPtrField<std::string>*
  1075. Reflection::MutableRepeatedPtrField<std::string>(
  1076. Message* message, const FieldDescriptor* field) const {
  1077. return static_cast<RepeatedPtrField<std::string>*>(
  1078. MutableRawRepeatedString(message, field, true));
  1079. }
  1080. // -----
  1081. template <>
  1082. inline const RepeatedPtrField<Message>& Reflection::GetRepeatedPtrField(
  1083. const Message& message, const FieldDescriptor* field) const {
  1084. return *static_cast<const RepeatedPtrField<Message>*>(GetRawRepeatedField(
  1085. message, field, FieldDescriptor::CPPTYPE_MESSAGE, -1, nullptr));
  1086. }
  1087. template <>
  1088. inline RepeatedPtrField<Message>* Reflection::MutableRepeatedPtrField(
  1089. Message* message, const FieldDescriptor* field) const {
  1090. return static_cast<RepeatedPtrField<Message>*>(MutableRawRepeatedField(
  1091. message, field, FieldDescriptor::CPPTYPE_MESSAGE, -1, nullptr));
  1092. }
  1093. template <typename PB>
  1094. inline const RepeatedPtrField<PB>& Reflection::GetRepeatedPtrField(
  1095. const Message& message, const FieldDescriptor* field) const {
  1096. return *static_cast<const RepeatedPtrField<PB>*>(
  1097. GetRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE, -1,
  1098. PB::default_instance().GetDescriptor()));
  1099. }
  1100. template <typename PB>
  1101. inline RepeatedPtrField<PB>* Reflection::MutableRepeatedPtrField(
  1102. Message* message, const FieldDescriptor* field) const {
  1103. return static_cast<RepeatedPtrField<PB>*>(
  1104. MutableRawRepeatedField(message, field, FieldDescriptor::CPPTYPE_MESSAGE,
  1105. -1, PB::default_instance().GetDescriptor()));
  1106. }
  1107. } // namespace protobuf
  1108. } // namespace google
  1109. #include <google/protobuf/port_undef.inc>
  1110. #endif // GOOGLE_PROTOBUF_MESSAGE_H__