message.h 53 KB

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