message.h 49 KB

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