PageRenderTime 95ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/tomahawk-player/tomahawk
C++ Header | 692 lines | 255 code | 78 blank | 359 comment | 0 complexity | 5282982d871a979fd1d8f4f24259644b MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  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. // Defines Message, the abstract interface implemented by non-lite
  35. // protocol message objects. Although it's possible to implement this
  36. // interface manually, most users will use the protocol compiler to
  37. // generate implementations.
  38. //
  39. // Example usage:
  40. //
  41. // Say you have a message defined as:
  42. //
  43. // message Foo {
  44. // optional string text = 1;
  45. // repeated int32 numbers = 2;
  46. // }
  47. //
  48. // Then, if you used the protocol compiler to generate a class from the above
  49. // definition, you could use it like so:
  50. //
  51. // string data; // Will store a serialized version of the message.
  52. //
  53. // {
  54. // // Create a message and serialize it.
  55. // Foo foo;
  56. // foo.set_text("Hello World!");
  57. // foo.add_numbers(1);
  58. // foo.add_numbers(5);
  59. // foo.add_numbers(42);
  60. //
  61. // foo.SerializeToString(&data);
  62. // }
  63. //
  64. // {
  65. // // Parse the serialized message and check that it contains the
  66. // // correct data.
  67. // Foo foo;
  68. // foo.ParseFromString(data);
  69. //
  70. // assert(foo.text() == "Hello World!");
  71. // assert(foo.numbers_size() == 3);
  72. // assert(foo.numbers(0) == 1);
  73. // assert(foo.numbers(1) == 5);
  74. // assert(foo.numbers(2) == 42);
  75. // }
  76. //
  77. // {
  78. // // Same as the last block, but do it dynamically via the Message
  79. // // reflection interface.
  80. // Message* foo = new Foo;
  81. // Descriptor* descriptor = foo->GetDescriptor();
  82. //
  83. // // Get the descriptors for the fields we're interested in and verify
  84. // // their types.
  85. // FieldDescriptor* text_field = descriptor->FindFieldByName("text");
  86. // assert(text_field != NULL);
  87. // assert(text_field->type() == FieldDescriptor::TYPE_STRING);
  88. // assert(text_field->label() == FieldDescriptor::TYPE_OPTIONAL);
  89. // FieldDescriptor* numbers_field = descriptor->FindFieldByName("numbers");
  90. // assert(numbers_field != NULL);
  91. // assert(numbers_field->type() == FieldDescriptor::TYPE_INT32);
  92. // assert(numbers_field->label() == FieldDescriptor::TYPE_REPEATED);
  93. //
  94. // // Parse the message.
  95. // foo->ParseFromString(data);
  96. //
  97. // // Use the reflection interface to examine the contents.
  98. // const Reflection* reflection = foo->GetReflection();
  99. // assert(reflection->GetString(foo, text_field) == "Hello World!");
  100. // assert(reflection->FieldSize(foo, numbers_field) == 3);
  101. // assert(reflection->GetRepeatedInt32(foo, numbers_field, 0) == 1);
  102. // assert(reflection->GetRepeatedInt32(foo, numbers_field, 1) == 5);
  103. // assert(reflection->GetRepeatedInt32(foo, numbers_field, 2) == 42);
  104. //
  105. // delete foo;
  106. // }
  107. #ifndef GOOGLE_PROTOBUF_MESSAGE_H__
  108. #define GOOGLE_PROTOBUF_MESSAGE_H__
  109. #include <vector>
  110. #include <string>
  111. #ifdef __DECCXX
  112. // HP C++'s iosfwd doesn't work.
  113. #include <iostream>
  114. #else
  115. #include <iosfwd>
  116. #endif
  117. #include <google/protobuf/message_lite.h>
  118. #include <google/protobuf/stubs/common.h>
  119. namespace google {
  120. namespace protobuf {
  121. // Defined in this file.
  122. class Message;
  123. class Reflection;
  124. class MessageFactory;
  125. // Defined in other files.
  126. class Descriptor; // descriptor.h
  127. class FieldDescriptor; // descriptor.h
  128. class EnumDescriptor; // descriptor.h
  129. class EnumValueDescriptor; // descriptor.h
  130. namespace io {
  131. class ZeroCopyInputStream; // zero_copy_stream.h
  132. class ZeroCopyOutputStream; // zero_copy_stream.h
  133. class CodedInputStream; // coded_stream.h
  134. class CodedOutputStream; // coded_stream.h
  135. }
  136. class UnknownFieldSet; // unknown_field_set.h
  137. // A container to hold message metadata.
  138. struct Metadata {
  139. const Descriptor* descriptor;
  140. const Reflection* reflection;
  141. };
  142. // Returns the EnumDescriptor for enum type E, which must be a
  143. // proto-declared enum type. Code generated by the protocol compiler
  144. // will include specializations of this template for each enum type declared.
  145. template <typename E>
  146. const EnumDescriptor* GetEnumDescriptor();
  147. // Abstract interface for protocol messages.
  148. //
  149. // See also MessageLite, which contains most every-day operations. Message
  150. // adds descriptors and reflection on top of that.
  151. //
  152. // The methods of this class that are virtual but not pure-virtual have
  153. // default implementations based on reflection. Message classes which are
  154. // optimized for speed will want to override these with faster implementations,
  155. // but classes optimized for code size may be happy with keeping them. See
  156. // the optimize_for option in descriptor.proto.
  157. class LIBPROTOBUF_EXPORT Message : public MessageLite {
  158. public:
  159. inline Message() {}
  160. virtual ~Message();
  161. // Basic Operations ------------------------------------------------
  162. // Construct a new instance of the same type. Ownership is passed to the
  163. // caller. (This is also defined in MessageLite, but is defined again here
  164. // for return-type covariance.)
  165. virtual Message* New() const = 0;
  166. // Make this message into a copy of the given message. The given message
  167. // must have the same descriptor, but need not necessarily be the same class.
  168. // By default this is just implemented as "Clear(); MergeFrom(from);".
  169. virtual void CopyFrom(const Message& from);
  170. // Merge the fields from the given message into this message. Singular
  171. // fields will be overwritten, except for embedded messages which will
  172. // be merged. Repeated fields will be concatenated. The given message
  173. // must be of the same type as this message (i.e. the exact same class).
  174. virtual void MergeFrom(const Message& from);
  175. // Verifies that IsInitialized() returns true. GOOGLE_CHECK-fails otherwise, with
  176. // a nice error message.
  177. void CheckInitialized() const;
  178. // Slowly build a list of all required fields that are not set.
  179. // This is much, much slower than IsInitialized() as it is implemented
  180. // purely via reflection. Generally, you should not call this unless you
  181. // have already determined that an error exists by calling IsInitialized().
  182. void FindInitializationErrors(vector<string>* errors) const;
  183. // Like FindInitializationErrors, but joins all the strings, delimited by
  184. // commas, and returns them.
  185. string InitializationErrorString() const;
  186. // Clears all unknown fields from this message and all embedded messages.
  187. // Normally, if unknown tag numbers are encountered when parsing a message,
  188. // the tag and value are stored in the message's UnknownFieldSet and
  189. // then written back out when the message is serialized. This allows servers
  190. // which simply route messages to other servers to pass through messages
  191. // that have new field definitions which they don't yet know about. However,
  192. // this behavior can have security implications. To avoid it, call this
  193. // method after parsing.
  194. //
  195. // See Reflection::GetUnknownFields() for more on unknown fields.
  196. virtual void DiscardUnknownFields();
  197. // Computes (an estimate of) the total number of bytes currently used for
  198. // storing the message in memory. The default implementation calls the
  199. // Reflection object's SpaceUsed() method.
  200. virtual int SpaceUsed() const;
  201. // Debugging & Testing----------------------------------------------
  202. // Generates a human readable form of this message, useful for debugging
  203. // and other purposes.
  204. string DebugString() const;
  205. // Like DebugString(), but with less whitespace.
  206. string ShortDebugString() const;
  207. // Like DebugString(), but do not escape UTF-8 byte sequences.
  208. string Utf8DebugString() const;
  209. // Convenience function useful in GDB. Prints DebugString() to stdout.
  210. void PrintDebugString() const;
  211. // Heavy I/O -------------------------------------------------------
  212. // Additional parsing and serialization methods not implemented by
  213. // MessageLite because they are not supported by the lite library.
  214. // Parse a protocol buffer from a file descriptor. If successful, the entire
  215. // input will be consumed.
  216. bool ParseFromFileDescriptor(int file_descriptor);
  217. // Like ParseFromFileDescriptor(), but accepts messages that are missing
  218. // required fields.
  219. bool ParsePartialFromFileDescriptor(int file_descriptor);
  220. // Parse a protocol buffer from a C++ istream. If successful, the entire
  221. // input will be consumed.
  222. bool ParseFromIstream(istream* input);
  223. // Like ParseFromIstream(), but accepts messages that are missing
  224. // required fields.
  225. bool ParsePartialFromIstream(istream* input);
  226. // Serialize the message and write it to the given file descriptor. All
  227. // required fields must be set.
  228. bool SerializeToFileDescriptor(int file_descriptor) const;
  229. // Like SerializeToFileDescriptor(), but allows missing required fields.
  230. bool SerializePartialToFileDescriptor(int file_descriptor) const;
  231. // Serialize the message and write it to the given C++ ostream. All
  232. // required fields must be set.
  233. bool SerializeToOstream(ostream* output) const;
  234. // Like SerializeToOstream(), but allows missing required fields.
  235. bool SerializePartialToOstream(ostream* output) const;
  236. // Reflection-based methods ----------------------------------------
  237. // These methods are pure-virtual in MessageLite, but Message provides
  238. // reflection-based default implementations.
  239. virtual string GetTypeName() const;
  240. virtual void Clear();
  241. virtual bool IsInitialized() const;
  242. virtual void CheckTypeAndMergeFrom(const MessageLite& other);
  243. virtual bool MergePartialFromCodedStream(io::CodedInputStream* input);
  244. virtual int ByteSize() const;
  245. virtual void SerializeWithCachedSizes(io::CodedOutputStream* output) const;
  246. private:
  247. // This is called only by the default implementation of ByteSize(), to
  248. // update the cached size. If you override ByteSize(), you do not need
  249. // to override this. If you do not override ByteSize(), you MUST override
  250. // this; the default implementation will crash.
  251. //
  252. // The method is private because subclasses should never call it; only
  253. // override it. Yes, C++ lets you do that. Crazy, huh?
  254. virtual void SetCachedSize(int size) const;
  255. public:
  256. // Introspection ---------------------------------------------------
  257. // Typedef for backwards-compatibility.
  258. typedef google::protobuf::Reflection Reflection;
  259. // Get a Descriptor for this message's type. This describes what
  260. // fields the message contains, the types of those fields, etc.
  261. const Descriptor* GetDescriptor() const { return GetMetadata().descriptor; }
  262. // Get the Reflection interface for this Message, which can be used to
  263. // read and modify the fields of the Message dynamically (in other words,
  264. // without knowing the message type at compile time). This object remains
  265. // property of the Message.
  266. //
  267. // This method remains virtual in case a subclass does not implement
  268. // reflection and wants to override the default behavior.
  269. virtual const Reflection* GetReflection() const {
  270. return GetMetadata().reflection;
  271. }
  272. protected:
  273. // Get a struct containing the metadata for the Message. Most subclasses only
  274. // need to implement this method, rather than the GetDescriptor() and
  275. // GetReflection() wrappers.
  276. virtual Metadata GetMetadata() const = 0;
  277. private:
  278. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Message);
  279. };
  280. // This interface contains methods that can be used to dynamically access
  281. // and modify the fields of a protocol message. Their semantics are
  282. // similar to the accessors the protocol compiler generates.
  283. //
  284. // To get the Reflection for a given Message, call Message::GetReflection().
  285. //
  286. // This interface is separate from Message only for efficiency reasons;
  287. // the vast majority of implementations of Message will share the same
  288. // implementation of Reflection (GeneratedMessageReflection,
  289. // defined in generated_message.h), and all Messages of a particular class
  290. // should share the same Reflection object (though you should not rely on
  291. // the latter fact).
  292. //
  293. // There are several ways that these methods can be used incorrectly. For
  294. // example, any of the following conditions will lead to undefined
  295. // results (probably assertion failures):
  296. // - The FieldDescriptor is not a field of this message type.
  297. // - The method called is not appropriate for the field's type. For
  298. // each field type in FieldDescriptor::TYPE_*, there is only one
  299. // Get*() method, one Set*() method, and one Add*() method that is
  300. // valid for that type. It should be obvious which (except maybe
  301. // for TYPE_BYTES, which are represented using strings in C++).
  302. // - A Get*() or Set*() method for singular fields is called on a repeated
  303. // field.
  304. // - GetRepeated*(), SetRepeated*(), or Add*() is called on a non-repeated
  305. // field.
  306. // - The Message object passed to any method is not of the right type for
  307. // this Reflection object (i.e. message.GetReflection() != reflection).
  308. //
  309. // You might wonder why there is not any abstract representation for a field
  310. // of arbitrary type. E.g., why isn't there just a "GetField()" method that
  311. // returns "const Field&", where "Field" is some class with accessors like
  312. // "GetInt32Value()". The problem is that someone would have to deal with
  313. // allocating these Field objects. For generated message classes, having to
  314. // allocate space for an additional object to wrap every field would at least
  315. // double the message's memory footprint, probably worse. Allocating the
  316. // objects on-demand, on the other hand, would be expensive and prone to
  317. // memory leaks. So, instead we ended up with this flat interface.
  318. //
  319. // TODO(kenton): Create a utility class which callers can use to read and
  320. // write fields from a Reflection without paying attention to the type.
  321. class LIBPROTOBUF_EXPORT Reflection {
  322. public:
  323. // TODO(kenton): Remove parameter.
  324. inline Reflection() {}
  325. virtual ~Reflection();
  326. // Get the UnknownFieldSet for the message. This contains fields which
  327. // were seen when the Message was parsed but were not recognized according
  328. // to the Message's definition.
  329. virtual const UnknownFieldSet& GetUnknownFields(
  330. const Message& message) const = 0;
  331. // Get a mutable pointer to the UnknownFieldSet for the message. This
  332. // contains fields which were seen when the Message was parsed but were not
  333. // recognized according to the Message's definition.
  334. virtual UnknownFieldSet* MutableUnknownFields(Message* message) const = 0;
  335. // Estimate the amount of memory used by the message object.
  336. virtual int SpaceUsed(const Message& message) const = 0;
  337. // Check if the given non-repeated field is set.
  338. virtual bool HasField(const Message& message,
  339. const FieldDescriptor* field) const = 0;
  340. // Get the number of elements of a repeated field.
  341. virtual int FieldSize(const Message& message,
  342. const FieldDescriptor* field) const = 0;
  343. // Clear the value of a field, so that HasField() returns false or
  344. // FieldSize() returns zero.
  345. virtual void ClearField(Message* message,
  346. const FieldDescriptor* field) const = 0;
  347. // Remove the last element of a repeated field.
  348. // We don't provide a way to remove any element other than the last
  349. // because it invites inefficient use, such as O(n^2) filtering loops
  350. // that should have been O(n). If you want to remove an element other
  351. // than the last, the best way to do it is to re-arrange the elements
  352. // (using Swap()) so that the one you want removed is at the end, then
  353. // call RemoveLast().
  354. virtual void RemoveLast(Message* message,
  355. const FieldDescriptor* field) const = 0;
  356. // Swap the complete contents of two messages.
  357. virtual void Swap(Message* message1, Message* message2) const = 0;
  358. // Swap two elements of a repeated field.
  359. virtual void SwapElements(Message* message,
  360. const FieldDescriptor* field,
  361. int index1,
  362. int index2) const = 0;
  363. // List all fields of the message which are currently set. This includes
  364. // extensions. Singular fields will only be listed if HasField(field) would
  365. // return true and repeated fields will only be listed if FieldSize(field)
  366. // would return non-zero. Fields (both normal fields and extension fields)
  367. // will be listed ordered by field number.
  368. virtual void ListFields(const Message& message,
  369. vector<const FieldDescriptor*>* output) const = 0;
  370. // Singular field getters ------------------------------------------
  371. // These get the value of a non-repeated field. They return the default
  372. // value for fields that aren't set.
  373. virtual int32 GetInt32 (const Message& message,
  374. const FieldDescriptor* field) const = 0;
  375. virtual int64 GetInt64 (const Message& message,
  376. const FieldDescriptor* field) const = 0;
  377. virtual uint32 GetUInt32(const Message& message,
  378. const FieldDescriptor* field) const = 0;
  379. virtual uint64 GetUInt64(const Message& message,
  380. const FieldDescriptor* field) const = 0;
  381. virtual float GetFloat (const Message& message,
  382. const FieldDescriptor* field) const = 0;
  383. virtual double GetDouble(const Message& message,
  384. const FieldDescriptor* field) const = 0;
  385. virtual bool GetBool (const Message& message,
  386. const FieldDescriptor* field) const = 0;
  387. virtual string GetString(const Message& message,
  388. const FieldDescriptor* field) const = 0;
  389. virtual const EnumValueDescriptor* GetEnum(
  390. const Message& message, const FieldDescriptor* field) const = 0;
  391. // See MutableMessage() for the meaning of the "factory" parameter.
  392. virtual const Message& GetMessage(const Message& message,
  393. const FieldDescriptor* field,
  394. MessageFactory* factory = NULL) const = 0;
  395. // Get a string value without copying, if possible.
  396. //
  397. // GetString() necessarily returns a copy of the string. This can be
  398. // inefficient when the string is already stored in a string object in the
  399. // underlying message. GetStringReference() will return a reference to the
  400. // underlying string in this case. Otherwise, it will copy the string into
  401. // *scratch and return that.
  402. //
  403. // Note: It is perfectly reasonable and useful to write code like:
  404. // str = reflection->GetStringReference(field, &str);
  405. // This line would ensure that only one copy of the string is made
  406. // regardless of the field's underlying representation. When initializing
  407. // a newly-constructed string, though, it's just as fast and more readable
  408. // to use code like:
  409. // string str = reflection->GetString(field);
  410. virtual const string& GetStringReference(const Message& message,
  411. const FieldDescriptor* field,
  412. string* scratch) const = 0;
  413. // Singular field mutators -----------------------------------------
  414. // These mutate the value of a non-repeated field.
  415. virtual void SetInt32 (Message* message,
  416. const FieldDescriptor* field, int32 value) const = 0;
  417. virtual void SetInt64 (Message* message,
  418. const FieldDescriptor* field, int64 value) const = 0;
  419. virtual void SetUInt32(Message* message,
  420. const FieldDescriptor* field, uint32 value) const = 0;
  421. virtual void SetUInt64(Message* message,
  422. const FieldDescriptor* field, uint64 value) const = 0;
  423. virtual void SetFloat (Message* message,
  424. const FieldDescriptor* field, float value) const = 0;
  425. virtual void SetDouble(Message* message,
  426. const FieldDescriptor* field, double value) const = 0;
  427. virtual void SetBool (Message* message,
  428. const FieldDescriptor* field, bool value) const = 0;
  429. virtual void SetString(Message* message,
  430. const FieldDescriptor* field,
  431. const string& value) const = 0;
  432. virtual void SetEnum (Message* message,
  433. const FieldDescriptor* field,
  434. const EnumValueDescriptor* value) const = 0;
  435. // Get a mutable pointer to a field with a message type. If a MessageFactory
  436. // is provided, it will be used to construct instances of the sub-message;
  437. // otherwise, the default factory is used. If the field is an extension that
  438. // does not live in the same pool as the containing message's descriptor (e.g.
  439. // it lives in an overlay pool), then a MessageFactory must be provided.
  440. // If you have no idea what that meant, then you probably don't need to worry
  441. // about it (don't provide a MessageFactory). WARNING: If the
  442. // FieldDescriptor is for a compiled-in extension, then
  443. // factory->GetPrototype(field->message_type() MUST return an instance of the
  444. // compiled-in class for this type, NOT DynamicMessage.
  445. virtual Message* MutableMessage(Message* message,
  446. const FieldDescriptor* field,
  447. MessageFactory* factory = NULL) const = 0;
  448. // Repeated field getters ------------------------------------------
  449. // These get the value of one element of a repeated field.
  450. virtual int32 GetRepeatedInt32 (const Message& message,
  451. const FieldDescriptor* field,
  452. int index) const = 0;
  453. virtual int64 GetRepeatedInt64 (const Message& message,
  454. const FieldDescriptor* field,
  455. int index) const = 0;
  456. virtual uint32 GetRepeatedUInt32(const Message& message,
  457. const FieldDescriptor* field,
  458. int index) const = 0;
  459. virtual uint64 GetRepeatedUInt64(const Message& message,
  460. const FieldDescriptor* field,
  461. int index) const = 0;
  462. virtual float GetRepeatedFloat (const Message& message,
  463. const FieldDescriptor* field,
  464. int index) const = 0;
  465. virtual double GetRepeatedDouble(const Message& message,
  466. const FieldDescriptor* field,
  467. int index) const = 0;
  468. virtual bool GetRepeatedBool (const Message& message,
  469. const FieldDescriptor* field,
  470. int index) const = 0;
  471. virtual string GetRepeatedString(const Message& message,
  472. const FieldDescriptor* field,
  473. int index) const = 0;
  474. virtual const EnumValueDescriptor* GetRepeatedEnum(
  475. const Message& message,
  476. const FieldDescriptor* field, int index) const = 0;
  477. virtual const Message& GetRepeatedMessage(
  478. const Message& message,
  479. const FieldDescriptor* field, int index) const = 0;
  480. // See GetStringReference(), above.
  481. virtual const string& GetRepeatedStringReference(
  482. const Message& message, const FieldDescriptor* field,
  483. int index, string* scratch) const = 0;
  484. // Repeated field mutators -----------------------------------------
  485. // These mutate the value of one element of a repeated field.
  486. virtual void SetRepeatedInt32 (Message* message,
  487. const FieldDescriptor* field,
  488. int index, int32 value) const = 0;
  489. virtual void SetRepeatedInt64 (Message* message,
  490. const FieldDescriptor* field,
  491. int index, int64 value) const = 0;
  492. virtual void SetRepeatedUInt32(Message* message,
  493. const FieldDescriptor* field,
  494. int index, uint32 value) const = 0;
  495. virtual void SetRepeatedUInt64(Message* message,
  496. const FieldDescriptor* field,
  497. int index, uint64 value) const = 0;
  498. virtual void SetRepeatedFloat (Message* message,
  499. const FieldDescriptor* field,
  500. int index, float value) const = 0;
  501. virtual void SetRepeatedDouble(Message* message,
  502. const FieldDescriptor* field,
  503. int index, double value) const = 0;
  504. virtual void SetRepeatedBool (Message* message,
  505. const FieldDescriptor* field,
  506. int index, bool value) const = 0;
  507. virtual void SetRepeatedString(Message* message,
  508. const FieldDescriptor* field,
  509. int index, const string& value) const = 0;
  510. virtual void SetRepeatedEnum(Message* message,
  511. const FieldDescriptor* field, int index,
  512. const EnumValueDescriptor* value) const = 0;
  513. // Get a mutable pointer to an element of a repeated field with a message
  514. // type.
  515. virtual Message* MutableRepeatedMessage(
  516. Message* message, const FieldDescriptor* field, int index) const = 0;
  517. // Repeated field adders -------------------------------------------
  518. // These add an element to a repeated field.
  519. virtual void AddInt32 (Message* message,
  520. const FieldDescriptor* field, int32 value) const = 0;
  521. virtual void AddInt64 (Message* message,
  522. const FieldDescriptor* field, int64 value) const = 0;
  523. virtual void AddUInt32(Message* message,
  524. const FieldDescriptor* field, uint32 value) const = 0;
  525. virtual void AddUInt64(Message* message,
  526. const FieldDescriptor* field, uint64 value) const = 0;
  527. virtual void AddFloat (Message* message,
  528. const FieldDescriptor* field, float value) const = 0;
  529. virtual void AddDouble(Message* message,
  530. const FieldDescriptor* field, double value) const = 0;
  531. virtual void AddBool (Message* message,
  532. const FieldDescriptor* field, bool value) const = 0;
  533. virtual void AddString(Message* message,
  534. const FieldDescriptor* field,
  535. const string& value) const = 0;
  536. virtual void AddEnum (Message* message,
  537. const FieldDescriptor* field,
  538. const EnumValueDescriptor* value) const = 0;
  539. // See MutableMessage() for comments on the "factory" parameter.
  540. virtual Message* AddMessage(Message* message,
  541. const FieldDescriptor* field,
  542. MessageFactory* factory = NULL) const = 0;
  543. // Extensions ------------------------------------------------------
  544. // Try to find an extension of this message type by fully-qualified field
  545. // name. Returns NULL if no extension is known for this name or number.
  546. virtual const FieldDescriptor* FindKnownExtensionByName(
  547. const string& name) const = 0;
  548. // Try to find an extension of this message type by field number.
  549. // Returns NULL if no extension is known for this name or number.
  550. virtual const FieldDescriptor* FindKnownExtensionByNumber(
  551. int number) const = 0;
  552. private:
  553. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Reflection);
  554. };
  555. // Abstract interface for a factory for message objects.
  556. class LIBPROTOBUF_EXPORT MessageFactory {
  557. public:
  558. inline MessageFactory() {}
  559. virtual ~MessageFactory();
  560. // Given a Descriptor, gets or constructs the default (prototype) Message
  561. // of that type. You can then call that message's New() method to construct
  562. // a mutable message of that type.
  563. //
  564. // Calling this method twice with the same Descriptor returns the same
  565. // object. The returned object remains property of the factory. Also, any
  566. // objects created by calling the prototype's New() method share some data
  567. // with the prototype, so these must be destoyed before the MessageFactory
  568. // is destroyed.
  569. //
  570. // The given descriptor must outlive the returned message, and hence must
  571. // outlive the MessageFactory.
  572. //
  573. // Some implementations do not support all types. GetPrototype() will
  574. // return NULL if the descriptor passed in is not supported.
  575. //
  576. // This method may or may not be thread-safe depending on the implementation.
  577. // Each implementation should document its own degree thread-safety.
  578. virtual const Message* GetPrototype(const Descriptor* type) = 0;
  579. // Gets a MessageFactory which supports all generated, compiled-in messages.
  580. // In other words, for any compiled-in type FooMessage, the following is true:
  581. // MessageFactory::generated_factory()->GetPrototype(
  582. // FooMessage::descriptor()) == FooMessage::default_instance()
  583. // This factory supports all types which are found in
  584. // DescriptorPool::generated_pool(). If given a descriptor from any other
  585. // pool, GetPrototype() will return NULL. (You can also check if a
  586. // descriptor is for a generated message by checking if
  587. // descriptor->file()->pool() == DescriptorPool::generated_pool().)
  588. //
  589. // This factory is 100% thread-safe; calling GetPrototype() does not modify
  590. // any shared data.
  591. //
  592. // This factory is a singleton. The caller must not delete the object.
  593. static MessageFactory* generated_factory();
  594. // For internal use only: Registers a .proto file at static initialization
  595. // time, to be placed in generated_factory. The first time GetPrototype()
  596. // is called with a descriptor from this file, |register_messages| will be
  597. // called, with the file name as the parameter. It must call
  598. // InternalRegisterGeneratedMessage() (below) to register each message type
  599. // in the file. This strange mechanism is necessary because descriptors are
  600. // built lazily, so we can't register types by their descriptor until we
  601. // know that the descriptor exists. |filename| must be a permanent string.
  602. static void InternalRegisterGeneratedFile(
  603. const char* filename, void (*register_messages)(const string&));
  604. // For internal use only: Registers a message type. Called only by the
  605. // functions which are registered with InternalRegisterGeneratedFile(),
  606. // above.
  607. static void InternalRegisterGeneratedMessage(const Descriptor* descriptor,
  608. const Message* prototype);
  609. private:
  610. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageFactory);
  611. };
  612. } // namespace protobuf
  613. } // namespace google
  614. #endif // GOOGLE_PROTOBUF_MESSAGE_H__