/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/generated_message_reflection.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 424 lines · 249 code · 36 blank · 139 comment · 2 complexity · cca90e0b10589b2cb4e1c312559cc41f MD5 · raw file

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