generated_message_reflection.h 33 KB

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