generated_message_reflection.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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. // This header is logically internal, but is made public because it is used
  35. // from protocol-compiler-generated code, which may reside in other components.
  36. #ifndef GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__
  37. #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__
  38. #include <string>
  39. #include <vector>
  40. #include <google/protobuf/stubs/casts.h>
  41. #include <google/protobuf/stubs/common.h>
  42. // TODO(jasonh): Remove this once the compiler change to directly include this
  43. // is released to components.
  44. #include <google/protobuf/generated_enum_reflection.h>
  45. #include <google/protobuf/message.h>
  46. #include <google/protobuf/metadata.h>
  47. #include <google/protobuf/unknown_field_set.h>
  48. namespace google {
  49. namespace upb {
  50. namespace google_opensource {
  51. class GMR_Handlers;
  52. } // namespace google_opensource
  53. } // namespace upb
  54. namespace protobuf {
  55. class DescriptorPool;
  56. class MapKey;
  57. class MapValueRef;
  58. } // namespace protobuf
  59. namespace protobuf {
  60. namespace flat {
  61. class MetadataBuilder;
  62. } // namespace flat
  63. } // namespace protobuf
  64. namespace protobuf {
  65. namespace internal {
  66. class DefaultEmptyOneof;
  67. // Defined in this file.
  68. class GeneratedMessageReflection;
  69. // Defined in other files.
  70. class ExtensionSet; // extension_set.h
  71. class WeakFieldMap; // weak_field_map.h
  72. // This struct describes the internal layout of the message, hence this is
  73. // used to act on the message reflectively.
  74. // default_instance: The default instance of the message. This is only
  75. // used to obtain pointers to default instances of embedded
  76. // messages, which GetMessage() will return if the particular
  77. // sub-message has not been initialized yet. (Thus, all
  78. // embedded message fields *must* have non-NULL pointers
  79. // in the default instance.)
  80. // offsets: An array of ints giving the byte offsets.
  81. // For each oneof or weak field, the offset is relative to the
  82. // default_instance. These can be computed at compile time
  83. // using the
  84. // GOOGLE_PROTOBUF_GENERATED_DEFAULT_ONEOF_FIELD_OFFSET()
  85. // macro. For each none oneof field, the offset is related to
  86. // the start of the message object. These can be computed at
  87. // compile time using the
  88. // GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET() macro.
  89. // Besides offsets for all fields, this array also contains
  90. // offsets for oneof unions. The offset of the i-th oneof union
  91. // is offsets[descriptor->field_count() + i].
  92. // has_bit_indices: Mapping from field indexes to their index in the has
  93. // bit array.
  94. // has_bits_offset: Offset in the message of an array of uint32s of size
  95. // descriptor->field_count()/32, rounded up. This is a
  96. // bitfield where each bit indicates whether or not the
  97. // corresponding field of the message has been initialized.
  98. // The bit for field index i is obtained by the expression:
  99. // has_bits[i / 32] & (1 << (i % 32))
  100. // unknown_fields_offset: Offset in the message of the UnknownFieldSet for
  101. // the message.
  102. // extensions_offset: Offset in the message of the ExtensionSet for the
  103. // message, or -1 if the message type has no extension
  104. // ranges.
  105. // oneof_case_offset: Offset in the message of an array of uint32s of
  106. // size descriptor->oneof_decl_count(). Each uint32
  107. // indicates what field is set for each oneof.
  108. // object_size: The size of a message object of this type, as measured
  109. // by sizeof().
  110. // arena_offset: If a message doesn't have a unknown_field_set that stores
  111. // the arena, it must have a direct pointer to the arena.
  112. // weak_field_map_offset: If the message proto has weak fields, this is the
  113. // offset of _weak_field_map_ in the generated proto. Otherwise
  114. // -1.
  115. struct ReflectionSchema {
  116. public:
  117. // Size of a google::protobuf::Message object of this type.
  118. uint32 GetObjectSize() const { return static_cast<uint32>(object_size_); }
  119. // Offset of a non-oneof field. Getting a field offset is slightly more
  120. // efficient when we know statically that it is not a oneof field.
  121. uint32 GetFieldOffsetNonOneof(const FieldDescriptor* field) const {
  122. GOOGLE_DCHECK(!field->containing_oneof());
  123. return offsets_[field->index()];
  124. }
  125. // Offset of any field.
  126. uint32 GetFieldOffset(const FieldDescriptor* field) const {
  127. if (field->containing_oneof()) {
  128. size_t offset =
  129. static_cast<size_t>(field->containing_type()->field_count() +
  130. field->containing_oneof()->index());
  131. return offsets_[offset];
  132. } else {
  133. return GetFieldOffsetNonOneof(field);
  134. }
  135. }
  136. uint32 GetOneofCaseOffset(const OneofDescriptor* oneof_descriptor) const {
  137. return static_cast<uint32>(oneof_case_offset_) +
  138. static_cast<uint32>(
  139. static_cast<size_t>(oneof_descriptor->index()) * sizeof(uint32));
  140. }
  141. bool HasHasbits() const { return has_bits_offset_ != -1; }
  142. // Bit index within the bit array of hasbits. Bit order is low-to-high.
  143. uint32 HasBitIndex(const FieldDescriptor* field) const {
  144. GOOGLE_DCHECK(HasHasbits());
  145. return has_bit_indices_[field->index()];
  146. }
  147. // Byte offset of the hasbits array.
  148. uint32 HasBitsOffset() const {
  149. GOOGLE_DCHECK(HasHasbits());
  150. return static_cast<uint32>(has_bits_offset_);
  151. }
  152. // The offset of the InternalMetadataWithArena member.
  153. // For Lite this will actually be an InternalMetadataWithArenaLite.
  154. // The schema doesn't contain enough information to distinguish between
  155. // these two cases.
  156. uint32 GetMetadataOffset() const {
  157. return static_cast<uint32>(metadata_offset_);
  158. }
  159. // Whether this message has an ExtensionSet.
  160. bool HasExtensionSet() const { return extensions_offset_ != -1; }
  161. // The offset of the ExtensionSet in this message.
  162. uint32 GetExtensionSetOffset() const {
  163. GOOGLE_DCHECK(HasExtensionSet());
  164. return static_cast<uint32>(extensions_offset_);
  165. }
  166. // The off set of WeakFieldMap when the message contains weak fields.
  167. // The default is 0 for now.
  168. int GetWeakFieldMapOffset() const { return weak_field_map_offset_; }
  169. bool IsDefaultInstance(const Message& message) const {
  170. return &message == default_instance_;
  171. }
  172. // Returns a pointer to the default value for this field. The size and type
  173. // of the underlying data depends on the field's type.
  174. const void *GetFieldDefault(const FieldDescriptor* field) const {
  175. return reinterpret_cast<const uint8*>(default_instance_) +
  176. offsets_[field->index()];
  177. }
  178. bool HasWeakFields() const { return weak_field_map_offset_ > 0; }
  179. // These members are intended to be private, but we cannot actually make them
  180. // private because this prevents us from using aggregate initialization of
  181. // them, ie.
  182. //
  183. // ReflectionSchema schema = {a, b, c, d, e, ...};
  184. // private:
  185. const Message* default_instance_;
  186. const uint32* offsets_;
  187. const uint32* has_bit_indices_;
  188. int has_bits_offset_;
  189. int metadata_offset_;
  190. int extensions_offset_;
  191. int oneof_case_offset_;
  192. int object_size_;
  193. int weak_field_map_offset_;
  194. };
  195. // Structs that the code generator emits directly to describe a message.
  196. // These should never used directly except to build a ReflectionSchema
  197. // object.
  198. //
  199. // EXPERIMENTAL: these are changing rapidly, and may completely disappear
  200. // or merge with ReflectionSchema.
  201. struct MigrationSchema {
  202. int32 offsets_index;
  203. int32 has_bit_indices_index;
  204. int object_size;
  205. };
  206. // THIS CLASS IS NOT INTENDED FOR DIRECT USE. It is intended for use
  207. // by generated code. This class is just a big hack that reduces code
  208. // size.
  209. //
  210. // A GeneratedMessageReflection is an implementation of Reflection
  211. // which expects all fields to be backed by simple variables located in
  212. // memory. The locations are given using a base pointer and a set of
  213. // offsets.
  214. //
  215. // It is required that the user represents fields of each type in a standard
  216. // way, so that GeneratedMessageReflection can cast the void* pointer to
  217. // the appropriate type. For primitive fields and string fields, each field
  218. // should be represented using the obvious C++ primitive type. Enums and
  219. // Messages are different:
  220. // - Singular Message fields are stored as a pointer to a Message. These
  221. // should start out NULL, except for in the default instance where they
  222. // should start out pointing to other default instances.
  223. // - Enum fields are stored as an int. This int must always contain
  224. // a valid value, such that EnumDescriptor::FindValueByNumber() would
  225. // not return NULL.
  226. // - Repeated fields are stored as RepeatedFields or RepeatedPtrFields
  227. // of whatever type the individual field would be. Strings and
  228. // Messages use RepeatedPtrFields while everything else uses
  229. // RepeatedFields.
  230. class LIBPROTOBUF_EXPORT GeneratedMessageReflection PROTOBUF_FINAL : public Reflection {
  231. public:
  232. // Constructs a GeneratedMessageReflection.
  233. // Parameters:
  234. // descriptor: The descriptor for the message type being implemented.
  235. // schema: The description of the internal guts of the message.
  236. // pool: DescriptorPool to search for extension definitions. Only
  237. // used by FindKnownExtensionByName() and
  238. // FindKnownExtensionByNumber().
  239. // factory: MessageFactory to use to construct extension messages.
  240. GeneratedMessageReflection(const Descriptor* descriptor,
  241. const ReflectionSchema& schema,
  242. const DescriptorPool* pool,
  243. MessageFactory* factory);
  244. ~GeneratedMessageReflection();
  245. // implements Reflection -------------------------------------------
  246. const UnknownFieldSet& GetUnknownFields(const Message& message) const;
  247. UnknownFieldSet* MutableUnknownFields(Message* message) const;
  248. size_t SpaceUsedLong(const Message& message) const;
  249. bool HasField(const Message& message, const FieldDescriptor* field) const;
  250. int FieldSize(const Message& message, const FieldDescriptor* field) const;
  251. void ClearField(Message* message, const FieldDescriptor* field) const;
  252. bool HasOneof(const Message& message,
  253. const OneofDescriptor* oneof_descriptor) const;
  254. void ClearOneof(Message* message, const OneofDescriptor* field) const;
  255. void RemoveLast(Message* message, const FieldDescriptor* field) const;
  256. Message* ReleaseLast(Message* message, const FieldDescriptor* field) const;
  257. void Swap(Message* message1, Message* message2) const;
  258. void SwapFields(Message* message1, Message* message2,
  259. const std::vector<const FieldDescriptor*>& fields) const;
  260. void SwapElements(Message* message, const FieldDescriptor* field,
  261. int index1, int index2) const;
  262. void ListFields(const Message& message,
  263. std::vector<const FieldDescriptor*>* output) const;
  264. int32 GetInt32 (const Message& message,
  265. const FieldDescriptor* field) const;
  266. int64 GetInt64 (const Message& message,
  267. const FieldDescriptor* field) const;
  268. uint32 GetUInt32(const Message& message,
  269. const FieldDescriptor* field) const;
  270. uint64 GetUInt64(const Message& message,
  271. const FieldDescriptor* field) const;
  272. float GetFloat (const Message& message,
  273. const FieldDescriptor* field) const;
  274. double GetDouble(const Message& message,
  275. const FieldDescriptor* field) const;
  276. bool GetBool (const Message& message,
  277. const FieldDescriptor* field) const;
  278. string GetString(const Message& message,
  279. const FieldDescriptor* field) const;
  280. const string& GetStringReference(const Message& message,
  281. const FieldDescriptor* field,
  282. string* scratch) const;
  283. const EnumValueDescriptor* GetEnum(const Message& message,
  284. const FieldDescriptor* field) const;
  285. int GetEnumValue(const Message& message,
  286. const FieldDescriptor* field) const;
  287. const Message& GetMessage(const Message& message,
  288. const FieldDescriptor* field,
  289. MessageFactory* factory = NULL) const;
  290. const FieldDescriptor* GetOneofFieldDescriptor(
  291. const Message& message,
  292. const OneofDescriptor* oneof_descriptor) const;
  293. private:
  294. bool ContainsMapKey(const Message& message,
  295. const FieldDescriptor* field,
  296. const MapKey& key) const;
  297. bool InsertOrLookupMapValue(Message* message,
  298. const FieldDescriptor* field,
  299. const MapKey& key,
  300. MapValueRef* val) const;
  301. bool DeleteMapValue(Message* message,
  302. const FieldDescriptor* field,
  303. const MapKey& key) const;
  304. MapIterator MapBegin(
  305. Message* message,
  306. const FieldDescriptor* field) const;
  307. MapIterator MapEnd(
  308. Message* message,
  309. const FieldDescriptor* field) const;
  310. int MapSize(const Message& message, const FieldDescriptor* field) const;
  311. public:
  312. void SetInt32 (Message* message,
  313. const FieldDescriptor* field, int32 value) const;
  314. void SetInt64 (Message* message,
  315. const FieldDescriptor* field, int64 value) const;
  316. void SetUInt32(Message* message,
  317. const FieldDescriptor* field, uint32 value) const;
  318. void SetUInt64(Message* message,
  319. const FieldDescriptor* field, uint64 value) const;
  320. void SetFloat (Message* message,
  321. const FieldDescriptor* field, float value) const;
  322. void SetDouble(Message* message,
  323. const FieldDescriptor* field, double value) const;
  324. void SetBool (Message* message,
  325. const FieldDescriptor* field, bool value) const;
  326. void SetString(Message* message,
  327. const FieldDescriptor* field,
  328. const string& value) const;
  329. void SetEnum (Message* message, const FieldDescriptor* field,
  330. const EnumValueDescriptor* value) const;
  331. void SetEnumValue(Message* message, const FieldDescriptor* field,
  332. int value) const;
  333. Message* MutableMessage(Message* message, const FieldDescriptor* field,
  334. MessageFactory* factory = NULL) const;
  335. void SetAllocatedMessage(Message* message,
  336. Message* sub_message,
  337. const FieldDescriptor* field) const;
  338. Message* ReleaseMessage(Message* message, const FieldDescriptor* field,
  339. MessageFactory* factory = NULL) const;
  340. int32 GetRepeatedInt32 (const Message& message,
  341. const FieldDescriptor* field, int index) const;
  342. int64 GetRepeatedInt64 (const Message& message,
  343. const FieldDescriptor* field, int index) const;
  344. uint32 GetRepeatedUInt32(const Message& message,
  345. const FieldDescriptor* field, int index) const;
  346. uint64 GetRepeatedUInt64(const Message& message,
  347. const FieldDescriptor* field, int index) const;
  348. float GetRepeatedFloat (const Message& message,
  349. const FieldDescriptor* field, int index) const;
  350. double GetRepeatedDouble(const Message& message,
  351. const FieldDescriptor* field, int index) const;
  352. bool GetRepeatedBool (const Message& message,
  353. const FieldDescriptor* field, int index) const;
  354. string GetRepeatedString(const Message& message,
  355. const FieldDescriptor* field, int index) const;
  356. const string& GetRepeatedStringReference(const Message& message,
  357. const FieldDescriptor* field,
  358. int index, string* scratch) const;
  359. const EnumValueDescriptor* GetRepeatedEnum(const Message& message,
  360. const FieldDescriptor* field,
  361. int index) const;
  362. int GetRepeatedEnumValue(const Message& message,
  363. const FieldDescriptor* field,
  364. int index) const;
  365. const Message& GetRepeatedMessage(const Message& message,
  366. const FieldDescriptor* field,
  367. int index) const;
  368. // Set the value of a field.
  369. void SetRepeatedInt32 (Message* message,
  370. const FieldDescriptor* field, int index, int32 value) const;
  371. void SetRepeatedInt64 (Message* message,
  372. const FieldDescriptor* field, int index, int64 value) const;
  373. void SetRepeatedUInt32(Message* message,
  374. const FieldDescriptor* field, int index, uint32 value) const;
  375. void SetRepeatedUInt64(Message* message,
  376. const FieldDescriptor* field, int index, uint64 value) const;
  377. void SetRepeatedFloat (Message* message,
  378. const FieldDescriptor* field, int index, float value) const;
  379. void SetRepeatedDouble(Message* message,
  380. const FieldDescriptor* field, int index, double value) const;
  381. void SetRepeatedBool (Message* message,
  382. const FieldDescriptor* field, int index, bool value) const;
  383. void SetRepeatedString(Message* message,
  384. const FieldDescriptor* field, int index,
  385. const string& value) const;
  386. void SetRepeatedEnum(Message* message, const FieldDescriptor* field,
  387. int index, const EnumValueDescriptor* value) const;
  388. void SetRepeatedEnumValue(Message* message, const FieldDescriptor* field,
  389. int index, int value) const;
  390. // Get a mutable pointer to a field with a message type.
  391. Message* MutableRepeatedMessage(Message* message,
  392. const FieldDescriptor* field,
  393. int index) const;
  394. void AddInt32 (Message* message,
  395. const FieldDescriptor* field, int32 value) const;
  396. void AddInt64 (Message* message,
  397. const FieldDescriptor* field, int64 value) const;
  398. void AddUInt32(Message* message,
  399. const FieldDescriptor* field, uint32 value) const;
  400. void AddUInt64(Message* message,
  401. const FieldDescriptor* field, uint64 value) const;
  402. void AddFloat (Message* message,
  403. const FieldDescriptor* field, float value) const;
  404. void AddDouble(Message* message,
  405. const FieldDescriptor* field, double value) const;
  406. void AddBool (Message* message,
  407. const FieldDescriptor* field, bool value) const;
  408. void AddString(Message* message,
  409. const FieldDescriptor* field, const string& value) const;
  410. void AddEnum(Message* message,
  411. const FieldDescriptor* field,
  412. const EnumValueDescriptor* value) const;
  413. void AddEnumValue(Message* message,
  414. const FieldDescriptor* field,
  415. int value) const;
  416. Message* AddMessage(Message* message, const FieldDescriptor* field,
  417. MessageFactory* factory = NULL) const;
  418. void AddAllocatedMessage(
  419. Message* message, const FieldDescriptor* field,
  420. Message* new_entry) const;
  421. const FieldDescriptor* FindKnownExtensionByName(const string& name) const;
  422. const FieldDescriptor* FindKnownExtensionByNumber(int number) const;
  423. bool SupportsUnknownEnumValues() const;
  424. // This value for arena_offset_ indicates that there is no arena pointer in
  425. // this message (e.g., old generated code).
  426. static const int kNoArenaPointer = -1;
  427. // This value for unknown_field_offset_ indicates that there is no
  428. // UnknownFieldSet in this message, and that instead, we are using the
  429. // Zero-Overhead Arena Pointer trick. When this is the case, arena_offset_
  430. // actually indexes to an InternalMetadataWithArena instance, which can return
  431. // either an arena pointer or an UnknownFieldSet or both. It is never the case
  432. // that unknown_field_offset_ == kUnknownFieldSetInMetadata && arena_offset_
  433. // == kNoArenaPointer.
  434. static const int kUnknownFieldSetInMetadata = -1;
  435. protected:
  436. void* MutableRawRepeatedField(
  437. Message* message, const FieldDescriptor* field, FieldDescriptor::CppType,
  438. int ctype, const Descriptor* desc) const;
  439. const void* GetRawRepeatedField(
  440. const Message& message, const FieldDescriptor* field,
  441. FieldDescriptor::CppType, int ctype,
  442. const Descriptor* desc) const;
  443. virtual MessageFactory* GetMessageFactory() const;
  444. virtual void* RepeatedFieldData(
  445. Message* message, const FieldDescriptor* field,
  446. FieldDescriptor::CppType cpp_type,
  447. const Descriptor* message_type) const;
  448. private:
  449. friend class google::protobuf::flat::MetadataBuilder;
  450. friend class upb::google_opensource::GMR_Handlers;
  451. const Descriptor* const descriptor_;
  452. const ReflectionSchema schema_;
  453. const DescriptorPool* const descriptor_pool_;
  454. MessageFactory* const message_factory_;
  455. // Last non weak field index. This is an optimization when most weak fields
  456. // are at the end of the containing message. If a message proto doesn't
  457. // contain weak fields, then this field equals descriptor_->field_count().
  458. int last_non_weak_field_index_;
  459. template <class T>
  460. const T& GetRawNonOneof(const Message& message,
  461. const FieldDescriptor* field) const;
  462. template <class T>
  463. T* MutableRawNonOneof(Message* message, const FieldDescriptor* field) const;
  464. template <typename Type>
  465. const Type& GetRaw(const Message& message,
  466. const FieldDescriptor* field) const;
  467. template <typename Type>
  468. inline Type* MutableRaw(Message* message,
  469. const FieldDescriptor* field) const;
  470. template <typename Type>
  471. inline const Type& DefaultRaw(const FieldDescriptor* field) const;
  472. inline const uint32* GetHasBits(const Message& message) const;
  473. inline uint32* MutableHasBits(Message* message) const;
  474. inline uint32 GetOneofCase(
  475. const Message& message,
  476. const OneofDescriptor* oneof_descriptor) const;
  477. inline uint32* MutableOneofCase(
  478. Message* message,
  479. const OneofDescriptor* oneof_descriptor) const;
  480. inline const ExtensionSet& GetExtensionSet(const Message& message) const;
  481. inline ExtensionSet* MutableExtensionSet(Message* message) const;
  482. inline Arena* GetArena(Message* message) const;
  483. inline const InternalMetadataWithArena& GetInternalMetadataWithArena(
  484. const Message& message) const;
  485. inline InternalMetadataWithArena*
  486. MutableInternalMetadataWithArena(Message* message) const;
  487. inline bool HasBit(const Message& message,
  488. const FieldDescriptor* field) const;
  489. inline void SetBit(Message* message,
  490. const FieldDescriptor* field) const;
  491. inline void ClearBit(Message* message,
  492. const FieldDescriptor* field) const;
  493. inline void SwapBit(Message* message1,
  494. Message* message2,
  495. const FieldDescriptor* field) const;
  496. // This function only swaps the field. Should swap corresponding has_bit
  497. // before or after using this function.
  498. void SwapField(Message* message1,
  499. Message* message2,
  500. const FieldDescriptor* field) const;
  501. void SwapOneofField(Message* message1,
  502. Message* message2,
  503. const OneofDescriptor* oneof_descriptor) const;
  504. inline bool HasOneofField(const Message& message,
  505. const FieldDescriptor* field) const;
  506. inline void SetOneofCase(Message* message,
  507. const FieldDescriptor* field) const;
  508. inline void ClearOneofField(Message* message,
  509. const FieldDescriptor* field) const;
  510. template <typename Type>
  511. inline const Type& GetField(const Message& message,
  512. const FieldDescriptor* field) const;
  513. template <typename Type>
  514. inline void SetField(Message* message,
  515. const FieldDescriptor* field, const Type& value) const;
  516. template <typename Type>
  517. inline Type* MutableField(Message* message,
  518. const FieldDescriptor* field) const;
  519. template <typename Type>
  520. inline const Type& GetRepeatedField(const Message& message,
  521. const FieldDescriptor* field,
  522. int index) const;
  523. template <typename Type>
  524. inline const Type& GetRepeatedPtrField(const Message& message,
  525. const FieldDescriptor* field,
  526. int index) const;
  527. template <typename Type>
  528. inline void SetRepeatedField(Message* message,
  529. const FieldDescriptor* field, int index,
  530. Type value) const;
  531. template <typename Type>
  532. inline Type* MutableRepeatedField(Message* message,
  533. const FieldDescriptor* field,
  534. int index) const;
  535. template <typename Type>
  536. inline void AddField(Message* message,
  537. const FieldDescriptor* field, const Type& value) const;
  538. template <typename Type>
  539. inline Type* AddField(Message* message,
  540. const FieldDescriptor* field) const;
  541. int GetExtensionNumberOrDie(const Descriptor* type) const;
  542. // Internal versions of EnumValue API perform no checking. Called after checks
  543. // by public methods.
  544. void SetEnumValueInternal(Message* message,
  545. const FieldDescriptor* field,
  546. int value) const;
  547. void SetRepeatedEnumValueInternal(Message* message,
  548. const FieldDescriptor* field,
  549. int index,
  550. int value) const;
  551. void AddEnumValueInternal(Message* message,
  552. const FieldDescriptor* field,
  553. int value) const;
  554. Message* UnsafeArenaReleaseMessage(Message* message,
  555. const FieldDescriptor* field,
  556. MessageFactory* factory = NULL) const;
  557. void UnsafeArenaSetAllocatedMessage(Message* message,
  558. Message* sub_message,
  559. const FieldDescriptor* field) const;
  560. internal::MapFieldBase* MapData(
  561. Message* message, const FieldDescriptor* field) const;
  562. friend inline // inline so nobody can call this function.
  563. void
  564. RegisterAllTypesInternal(const Metadata* file_level_metadata, int size);
  565. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GeneratedMessageReflection);
  566. };
  567. // There are some places in proto2 where dynamic_cast would be useful as an
  568. // optimization. For example, take Message::MergeFrom(const Message& other).
  569. // For a given generated message FooMessage, we generate these two methods:
  570. // void MergeFrom(const FooMessage& other);
  571. // void MergeFrom(const Message& other);
  572. // The former method can be implemented directly in terms of FooMessage's
  573. // inline accessors, but the latter method must work with the reflection
  574. // interface. However, if the parameter to the latter method is actually of
  575. // type FooMessage, then we'd like to be able to just call the other method
  576. // as an optimization. So, we use dynamic_cast to check this.
  577. //
  578. // That said, dynamic_cast requires RTTI, which many people like to disable
  579. // for performance and code size reasons. When RTTI is not available, we
  580. // still need to produce correct results. So, in this case we have to fall
  581. // back to using reflection, which is what we would have done anyway if the
  582. // objects were not of the exact same class.
  583. //
  584. // dynamic_cast_if_available() implements this logic. If RTTI is
  585. // enabled, it does a dynamic_cast. If RTTI is disabled, it just returns
  586. // NULL.
  587. template<typename To, typename From>
  588. inline To dynamic_cast_if_available(From from) {
  589. #ifdef GOOGLE_PROTOBUF_NO_RTTI
  590. // Avoid the compiler warning about unused variables.
  591. (void)from;
  592. return NULL;
  593. #else
  594. return dynamic_cast<To>(from);
  595. #endif
  596. }
  597. // Tries to downcast this message to a generated message type.
  598. // Returns NULL if this class is not an instance of T.
  599. //
  600. // This is like dynamic_cast_if_available, except it works even when
  601. // dynamic_cast is not available by using Reflection. However it only works
  602. // with Message objects.
  603. //
  604. // TODO(haberman): can we remove dynamic_cast_if_available in favor of this?
  605. template <typename T>
  606. T* DynamicCastToGenerated(const Message* from) {
  607. // Compile-time assert that T is a generated type that has a
  608. // default_instance() accessor, but avoid actually calling it.
  609. const T&(*get_default_instance)() = &T::default_instance;
  610. (void)get_default_instance;
  611. // Compile-time assert that T is a subclass of google::protobuf::Message.
  612. const Message* unused = static_cast<T*>(NULL);
  613. (void)unused;
  614. #ifdef GOOGLE_PROTOBUF_NO_RTTI
  615. bool ok = &T::default_instance() ==
  616. from->GetReflection()->GetMessageFactory()->GetPrototype(
  617. from->GetDescriptor());
  618. return ok ? down_cast<T*>(from) : NULL;
  619. #else
  620. return dynamic_cast<T*>(from);
  621. #endif
  622. }
  623. template <typename T>
  624. T* DynamicCastToGenerated(Message* from) {
  625. const Message* message_const = from;
  626. return const_cast<T*>(DynamicCastToGenerated<const T>(message_const));
  627. }
  628. LIBPROTOBUF_EXPORT void AssignDescriptors(
  629. const string& filename, const MigrationSchema* schemas,
  630. const Message* const* default_instances_, const uint32* offsets,
  631. MessageFactory* factory,
  632. // update the following descriptor arrays.
  633. Metadata* file_level_metadata,
  634. const EnumDescriptor** file_level_enum_descriptors,
  635. const ServiceDescriptor** file_level_service_descriptors);
  636. LIBPROTOBUF_EXPORT void RegisterAllTypes(const Metadata* file_level_metadata, int size);
  637. // These cannot be in lite so we put them in the reflection.
  638. LIBPROTOBUF_EXPORT void UnknownFieldSetSerializer(const uint8* base, uint32 offset, uint32 tag,
  639. uint32 has_offset,
  640. ::google::protobuf::io::CodedOutputStream* output);
  641. } // namespace internal
  642. } // namespace protobuf
  643. } // namespace google
  644. #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__