generated_message_reflection.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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/common.h>
  41. // TODO(jasonh): Remove this once the compiler change to directly include this
  42. // is released to components.
  43. #include <google/protobuf/generated_enum_reflection.h>
  44. #include <google/protobuf/message.h>
  45. #include <google/protobuf/unknown_field_set.h>
  46. namespace google {
  47. namespace upb {
  48. namespace google_opensource {
  49. class GMR_Handlers;
  50. } // namespace google_opensource
  51. } // namespace upb
  52. namespace protobuf {
  53. class DescriptorPool;
  54. }
  55. namespace protobuf {
  56. namespace internal {
  57. class DefaultEmptyOneof;
  58. // Defined in this file.
  59. class GeneratedMessageReflection;
  60. // Defined in other files.
  61. class ExtensionSet; // extension_set.h
  62. // THIS CLASS IS NOT INTENDED FOR DIRECT USE. It is intended for use
  63. // by generated code. This class is just a big hack that reduces code
  64. // size.
  65. //
  66. // A GeneratedMessageReflection is an implementation of Reflection
  67. // which expects all fields to be backed by simple variables located in
  68. // memory. The locations are given using a base pointer and a set of
  69. // offsets.
  70. //
  71. // It is required that the user represents fields of each type in a standard
  72. // way, so that GeneratedMessageReflection can cast the void* pointer to
  73. // the appropriate type. For primitive fields and string fields, each field
  74. // should be represented using the obvious C++ primitive type. Enums and
  75. // Messages are different:
  76. // - Singular Message fields are stored as a pointer to a Message. These
  77. // should start out NULL, except for in the default instance where they
  78. // should start out pointing to other default instances.
  79. // - Enum fields are stored as an int. This int must always contain
  80. // a valid value, such that EnumDescriptor::FindValueByNumber() would
  81. // not return NULL.
  82. // - Repeated fields are stored as RepeatedFields or RepeatedPtrFields
  83. // of whatever type the individual field would be. Strings and
  84. // Messages use RepeatedPtrFields while everything else uses
  85. // RepeatedFields.
  86. class LIBPROTOBUF_EXPORT GeneratedMessageReflection : public Reflection {
  87. public:
  88. // Constructs a GeneratedMessageReflection.
  89. // Parameters:
  90. // descriptor: The descriptor for the message type being implemented.
  91. // default_instance: The default instance of the message. This is only
  92. // used to obtain pointers to default instances of embedded
  93. // messages, which GetMessage() will return if the particular
  94. // sub-message has not been initialized yet. (Thus, all
  95. // embedded message fields *must* have non-NULL pointers
  96. // in the default instance.)
  97. // offsets: An array of ints giving the byte offsets, relative to
  98. // the start of the message object, of each field. These can
  99. // be computed at compile time using the
  100. // GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET() macro, defined
  101. // below.
  102. // has_bits_offset: Offset in the message of an array of uint32s of size
  103. // descriptor->field_count()/32, rounded up. This is a
  104. // bitfield where each bit indicates whether or not the
  105. // corresponding field of the message has been initialized.
  106. // The bit for field index i is obtained by the expression:
  107. // has_bits[i / 32] & (1 << (i % 32))
  108. // unknown_fields_offset: Offset in the message of the UnknownFieldSet for
  109. // the message.
  110. // extensions_offset: Offset in the message of the ExtensionSet for the
  111. // message, or -1 if the message type has no extension
  112. // ranges.
  113. // pool: DescriptorPool to search for extension definitions. Only
  114. // used by FindKnownExtensionByName() and
  115. // FindKnownExtensionByNumber().
  116. // factory: MessageFactory to use to construct extension messages.
  117. // object_size: The size of a message object of this type, as measured
  118. // by sizeof().
  119. GeneratedMessageReflection(const Descriptor* descriptor,
  120. const Message* default_instance,
  121. const int offsets[],
  122. int has_bits_offset,
  123. int unknown_fields_offset,
  124. int extensions_offset,
  125. const DescriptorPool* pool,
  126. MessageFactory* factory,
  127. int object_size);
  128. // Similar with the construction above. Call this construction if the
  129. // message has oneof definition.
  130. // Parameters:
  131. // offsets: An array of ints giving the byte offsets.
  132. // For each oneof field, the offset is relative to the
  133. // default_oneof_instance. These can be computed at compile
  134. // time using the
  135. // PROTO2_GENERATED_DEFAULT_ONEOF_FIELD_OFFSET() macro.
  136. // For each none oneof field, the offset is related to
  137. // the start of the message object. These can be computed
  138. // at compile time using the
  139. // GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET() macro.
  140. // Besides offsets for all fields, this array also contains
  141. // offsets for oneof unions. The offset of the i-th oneof
  142. // union is offsets[descriptor->field_count() + i].
  143. // default_oneof_instance: The default instance of the oneofs. It is a
  144. // struct holding the default value of all oneof fields
  145. // for this message. It is only used to obtain pointers
  146. // to default instances of oneof fields, which Get
  147. // methods will return if the field is not set.
  148. // oneof_case_offset: Offset in the message of an array of uint32s of
  149. // size descriptor->oneof_decl_count(). Each uint32
  150. // indicates what field is set for each oneof.
  151. // other parameters are the same with the construction above.
  152. GeneratedMessageReflection(const Descriptor* descriptor,
  153. const Message* default_instance,
  154. const int offsets[],
  155. int has_bits_offset,
  156. int unknown_fields_offset,
  157. int extensions_offset,
  158. const void* default_oneof_instance,
  159. int oneof_case_offset,
  160. const DescriptorPool* pool,
  161. MessageFactory* factory,
  162. int object_size);
  163. ~GeneratedMessageReflection();
  164. // implements Reflection -------------------------------------------
  165. const UnknownFieldSet& GetUnknownFields(const Message& message) const;
  166. UnknownFieldSet* MutableUnknownFields(Message* message) const;
  167. int SpaceUsed(const Message& message) const;
  168. bool HasField(const Message& message, const FieldDescriptor* field) const;
  169. int FieldSize(const Message& message, const FieldDescriptor* field) const;
  170. void ClearField(Message* message, const FieldDescriptor* field) const;
  171. bool HasOneof(const Message& message,
  172. const OneofDescriptor* oneof_descriptor) const;
  173. void ClearOneof(Message* message, const OneofDescriptor* field) const;
  174. void RemoveLast(Message* message, const FieldDescriptor* field) const;
  175. Message* ReleaseLast(Message* message, const FieldDescriptor* field) const;
  176. void Swap(Message* message1, Message* message2) const;
  177. void SwapFields(Message* message1, Message* message2,
  178. const vector<const FieldDescriptor*>& fields) const;
  179. void SwapElements(Message* message, const FieldDescriptor* field,
  180. int index1, int index2) const;
  181. void ListFields(const Message& message,
  182. vector<const FieldDescriptor*>* output) const;
  183. int32 GetInt32 (const Message& message,
  184. const FieldDescriptor* field) const;
  185. int64 GetInt64 (const Message& message,
  186. const FieldDescriptor* field) const;
  187. uint32 GetUInt32(const Message& message,
  188. const FieldDescriptor* field) const;
  189. uint64 GetUInt64(const Message& message,
  190. const FieldDescriptor* field) const;
  191. float GetFloat (const Message& message,
  192. const FieldDescriptor* field) const;
  193. double GetDouble(const Message& message,
  194. const FieldDescriptor* field) const;
  195. bool GetBool (const Message& message,
  196. const FieldDescriptor* field) const;
  197. string GetString(const Message& message,
  198. const FieldDescriptor* field) const;
  199. const string& GetStringReference(const Message& message,
  200. const FieldDescriptor* field,
  201. string* scratch) const;
  202. const EnumValueDescriptor* GetEnum(const Message& message,
  203. const FieldDescriptor* field) const;
  204. const Message& GetMessage(const Message& message,
  205. const FieldDescriptor* field,
  206. MessageFactory* factory = NULL) const;
  207. const FieldDescriptor* GetOneofFieldDescriptor(
  208. const Message& message,
  209. const OneofDescriptor* oneof_descriptor) const;
  210. public:
  211. void SetInt32 (Message* message,
  212. const FieldDescriptor* field, int32 value) const;
  213. void SetInt64 (Message* message,
  214. const FieldDescriptor* field, int64 value) const;
  215. void SetUInt32(Message* message,
  216. const FieldDescriptor* field, uint32 value) const;
  217. void SetUInt64(Message* message,
  218. const FieldDescriptor* field, uint64 value) const;
  219. void SetFloat (Message* message,
  220. const FieldDescriptor* field, float value) const;
  221. void SetDouble(Message* message,
  222. const FieldDescriptor* field, double value) const;
  223. void SetBool (Message* message,
  224. const FieldDescriptor* field, bool value) const;
  225. void SetString(Message* message,
  226. const FieldDescriptor* field,
  227. const string& value) const;
  228. void SetEnum (Message* message, const FieldDescriptor* field,
  229. const EnumValueDescriptor* value) const;
  230. Message* MutableMessage(Message* message, const FieldDescriptor* field,
  231. MessageFactory* factory = NULL) const;
  232. void SetAllocatedMessage(Message* message,
  233. Message* sub_message,
  234. const FieldDescriptor* field) const;
  235. Message* ReleaseMessage(Message* message, const FieldDescriptor* field,
  236. MessageFactory* factory = NULL) const;
  237. int32 GetRepeatedInt32 (const Message& message,
  238. const FieldDescriptor* field, int index) const;
  239. int64 GetRepeatedInt64 (const Message& message,
  240. const FieldDescriptor* field, int index) const;
  241. uint32 GetRepeatedUInt32(const Message& message,
  242. const FieldDescriptor* field, int index) const;
  243. uint64 GetRepeatedUInt64(const Message& message,
  244. const FieldDescriptor* field, int index) const;
  245. float GetRepeatedFloat (const Message& message,
  246. const FieldDescriptor* field, int index) const;
  247. double GetRepeatedDouble(const Message& message,
  248. const FieldDescriptor* field, int index) const;
  249. bool GetRepeatedBool (const Message& message,
  250. const FieldDescriptor* field, int index) const;
  251. string GetRepeatedString(const Message& message,
  252. const FieldDescriptor* field, int index) const;
  253. const string& GetRepeatedStringReference(const Message& message,
  254. const FieldDescriptor* field,
  255. int index, string* scratch) const;
  256. const EnumValueDescriptor* GetRepeatedEnum(const Message& message,
  257. const FieldDescriptor* field,
  258. int index) const;
  259. const Message& GetRepeatedMessage(const Message& message,
  260. const FieldDescriptor* field,
  261. int index) const;
  262. // Set the value of a field.
  263. void SetRepeatedInt32 (Message* message,
  264. const FieldDescriptor* field, int index, int32 value) const;
  265. void SetRepeatedInt64 (Message* message,
  266. const FieldDescriptor* field, int index, int64 value) const;
  267. void SetRepeatedUInt32(Message* message,
  268. const FieldDescriptor* field, int index, uint32 value) const;
  269. void SetRepeatedUInt64(Message* message,
  270. const FieldDescriptor* field, int index, uint64 value) const;
  271. void SetRepeatedFloat (Message* message,
  272. const FieldDescriptor* field, int index, float value) const;
  273. void SetRepeatedDouble(Message* message,
  274. const FieldDescriptor* field, int index, double value) const;
  275. void SetRepeatedBool (Message* message,
  276. const FieldDescriptor* field, int index, bool value) const;
  277. void SetRepeatedString(Message* message,
  278. const FieldDescriptor* field, int index,
  279. const string& value) const;
  280. void SetRepeatedEnum(Message* message, const FieldDescriptor* field,
  281. int index, const EnumValueDescriptor* value) const;
  282. // Get a mutable pointer to a field with a message type.
  283. Message* MutableRepeatedMessage(Message* message,
  284. const FieldDescriptor* field,
  285. int index) const;
  286. void AddInt32 (Message* message,
  287. const FieldDescriptor* field, int32 value) const;
  288. void AddInt64 (Message* message,
  289. const FieldDescriptor* field, int64 value) const;
  290. void AddUInt32(Message* message,
  291. const FieldDescriptor* field, uint32 value) const;
  292. void AddUInt64(Message* message,
  293. const FieldDescriptor* field, uint64 value) const;
  294. void AddFloat (Message* message,
  295. const FieldDescriptor* field, float value) const;
  296. void AddDouble(Message* message,
  297. const FieldDescriptor* field, double value) const;
  298. void AddBool (Message* message,
  299. const FieldDescriptor* field, bool value) const;
  300. void AddString(Message* message,
  301. const FieldDescriptor* field, const string& value) const;
  302. void AddEnum(Message* message,
  303. const FieldDescriptor* field,
  304. const EnumValueDescriptor* value) const;
  305. Message* AddMessage(Message* message, const FieldDescriptor* field,
  306. MessageFactory* factory = NULL) const;
  307. const FieldDescriptor* FindKnownExtensionByName(const string& name) const;
  308. const FieldDescriptor* FindKnownExtensionByNumber(int number) const;
  309. protected:
  310. virtual void* MutableRawRepeatedField(
  311. Message* message, const FieldDescriptor* field, FieldDescriptor::CppType,
  312. int ctype, const Descriptor* desc) const;
  313. private:
  314. friend class GeneratedMessage;
  315. // To parse directly into a proto2 generated class, the class GMR_Handlers
  316. // needs access to member offsets and hasbits.
  317. friend class LIBPROTOBUF_EXPORT upb::google_opensource::GMR_Handlers;
  318. const Descriptor* descriptor_;
  319. const Message* default_instance_;
  320. const void* default_oneof_instance_;
  321. const int* offsets_;
  322. int has_bits_offset_;
  323. int oneof_case_offset_;
  324. int unknown_fields_offset_;
  325. int extensions_offset_;
  326. int object_size_;
  327. const DescriptorPool* descriptor_pool_;
  328. MessageFactory* message_factory_;
  329. template <typename Type>
  330. inline const Type& GetRaw(const Message& message,
  331. const FieldDescriptor* field) const;
  332. template <typename Type>
  333. inline Type* MutableRaw(Message* message,
  334. const FieldDescriptor* field) const;
  335. template <typename Type>
  336. inline const Type& DefaultRaw(const FieldDescriptor* field) const;
  337. template <typename Type>
  338. inline const Type& DefaultOneofRaw(const FieldDescriptor* field) const;
  339. inline const uint32* GetHasBits(const Message& message) const;
  340. inline uint32* MutableHasBits(Message* message) const;
  341. inline uint32 GetOneofCase(
  342. const Message& message,
  343. const OneofDescriptor* oneof_descriptor) const;
  344. inline uint32* MutableOneofCase(
  345. Message* message,
  346. const OneofDescriptor* oneof_descriptor) const;
  347. inline const ExtensionSet& GetExtensionSet(const Message& message) const;
  348. inline ExtensionSet* MutableExtensionSet(Message* message) const;
  349. inline bool HasBit(const Message& message,
  350. const FieldDescriptor* field) const;
  351. inline void SetBit(Message* message,
  352. const FieldDescriptor* field) const;
  353. inline void ClearBit(Message* message,
  354. const FieldDescriptor* field) const;
  355. inline void SwapBit(Message* message1,
  356. Message* message2,
  357. const FieldDescriptor* field) const;
  358. // This function only swaps the field. Should swap corresponding has_bit
  359. // before or after using this function.
  360. void SwapField(Message* message1,
  361. Message* message2,
  362. const FieldDescriptor* field) const;
  363. void SwapOneofField(Message* message1,
  364. Message* message2,
  365. const OneofDescriptor* oneof_descriptor) const;
  366. inline bool HasOneofField(const Message& message,
  367. const FieldDescriptor* field) const;
  368. inline void SetOneofCase(Message* message,
  369. const FieldDescriptor* field) const;
  370. inline void ClearOneofField(Message* message,
  371. const FieldDescriptor* field) const;
  372. template <typename Type>
  373. inline const Type& GetField(const Message& message,
  374. const FieldDescriptor* field) const;
  375. template <typename Type>
  376. inline void SetField(Message* message,
  377. const FieldDescriptor* field, const Type& value) const;
  378. template <typename Type>
  379. inline Type* MutableField(Message* message,
  380. const FieldDescriptor* field) const;
  381. template <typename Type>
  382. inline const Type& GetRepeatedField(const Message& message,
  383. const FieldDescriptor* field,
  384. int index) const;
  385. template <typename Type>
  386. inline const Type& GetRepeatedPtrField(const Message& message,
  387. const FieldDescriptor* field,
  388. int index) const;
  389. template <typename Type>
  390. inline void SetRepeatedField(Message* message,
  391. const FieldDescriptor* field, int index,
  392. Type value) const;
  393. template <typename Type>
  394. inline Type* MutableRepeatedField(Message* message,
  395. const FieldDescriptor* field,
  396. int index) const;
  397. template <typename Type>
  398. inline void AddField(Message* message,
  399. const FieldDescriptor* field, const Type& value) const;
  400. template <typename Type>
  401. inline Type* AddField(Message* message,
  402. const FieldDescriptor* field) const;
  403. int GetExtensionNumberOrDie(const Descriptor* type) const;
  404. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GeneratedMessageReflection);
  405. };
  406. // Returns the offset of the given field within the given aggregate type.
  407. // This is equivalent to the ANSI C offsetof() macro. However, according
  408. // to the C++ standard, offsetof() only works on POD types, and GCC
  409. // enforces this requirement with a warning. In practice, this rule is
  410. // unnecessarily strict; there is probably no compiler or platform on
  411. // which the offsets of the direct fields of a class are non-constant.
  412. // Fields inherited from superclasses *can* have non-constant offsets,
  413. // but that's not what this macro will be used for.
  414. //
  415. // Note that we calculate relative to the pointer value 16 here since if we
  416. // just use zero, GCC complains about dereferencing a NULL pointer. We
  417. // choose 16 rather than some other number just in case the compiler would
  418. // be confused by an unaligned pointer.
  419. #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(TYPE, FIELD) \
  420. static_cast<int>( \
  421. reinterpret_cast<const char*>( \
  422. &reinterpret_cast<const TYPE*>(16)->FIELD) - \
  423. reinterpret_cast<const char*>(16))
  424. #define PROTO2_GENERATED_DEFAULT_ONEOF_FIELD_OFFSET(ONEOF, FIELD) \
  425. static_cast<int>( \
  426. reinterpret_cast<const char*>(&(ONEOF->FIELD)) \
  427. - reinterpret_cast<const char*>(ONEOF))
  428. // There are some places in proto2 where dynamic_cast would be useful as an
  429. // optimization. For example, take Message::MergeFrom(const Message& other).
  430. // For a given generated message FooMessage, we generate these two methods:
  431. // void MergeFrom(const FooMessage& other);
  432. // void MergeFrom(const Message& other);
  433. // The former method can be implemented directly in terms of FooMessage's
  434. // inline accessors, but the latter method must work with the reflection
  435. // interface. However, if the parameter to the latter method is actually of
  436. // type FooMessage, then we'd like to be able to just call the other method
  437. // as an optimization. So, we use dynamic_cast to check this.
  438. //
  439. // That said, dynamic_cast requires RTTI, which many people like to disable
  440. // for performance and code size reasons. When RTTI is not available, we
  441. // still need to produce correct results. So, in this case we have to fall
  442. // back to using reflection, which is what we would have done anyway if the
  443. // objects were not of the exact same class.
  444. //
  445. // dynamic_cast_if_available() implements this logic. If RTTI is
  446. // enabled, it does a dynamic_cast. If RTTI is disabled, it just returns
  447. // NULL.
  448. //
  449. // If you need to compile without RTTI, simply #define GOOGLE_PROTOBUF_NO_RTTI.
  450. // On MSVC, this should be detected automatically.
  451. template<typename To, typename From>
  452. inline To dynamic_cast_if_available(From from) {
  453. #if defined(GOOGLE_PROTOBUF_NO_RTTI) || (defined(_MSC_VER)&&!defined(_CPPRTTI))
  454. return NULL;
  455. #else
  456. return dynamic_cast<To>(from);
  457. #endif
  458. }
  459. } // namespace internal
  460. } // namespace protobuf
  461. } // namespace google
  462. #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__