PageRenderTime 47ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/tomahawk-player/tomahawk
C++ Header | 1367 lines | 622 code | 212 blank | 533 comment | 19 complexity | 2f3567eae83e2315a31508f45c628ab8 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. // This file contains classes which describe a type of protocol message.
  35. // You can use a message's descriptor to learn at runtime what fields
  36. // it contains and what the types of those fields are. The Message
  37. // interface also allows you to dynamically access and modify individual
  38. // fields by passing the FieldDescriptor of the field you are interested
  39. // in.
  40. //
  41. // Most users will not care about descriptors, because they will write
  42. // code specific to certain protocol types and will simply use the classes
  43. // generated by the protocol compiler directly. Advanced users who want
  44. // to operate on arbitrary types (not known at compile time) may want to
  45. // read descriptors in order to learn about the contents of a message.
  46. // A very small number of users will want to construct their own
  47. // Descriptors, either because they are implementing Message manually or
  48. // because they are writing something like the protocol compiler.
  49. //
  50. // For an example of how you might use descriptors, see the code example
  51. // at the top of message.h.
  52. #ifndef GOOGLE_PROTOBUF_DESCRIPTOR_H__
  53. #define GOOGLE_PROTOBUF_DESCRIPTOR_H__
  54. #include <string>
  55. #include <vector>
  56. #include <google/protobuf/stubs/common.h>
  57. namespace google {
  58. namespace protobuf {
  59. // Defined in this file.
  60. class Descriptor;
  61. class FieldDescriptor;
  62. class EnumDescriptor;
  63. class EnumValueDescriptor;
  64. class ServiceDescriptor;
  65. class MethodDescriptor;
  66. class FileDescriptor;
  67. class DescriptorDatabase;
  68. class DescriptorPool;
  69. // Defined in descriptor.proto
  70. class DescriptorProto;
  71. class FieldDescriptorProto;
  72. class EnumDescriptorProto;
  73. class EnumValueDescriptorProto;
  74. class ServiceDescriptorProto;
  75. class MethodDescriptorProto;
  76. class FileDescriptorProto;
  77. class MessageOptions;
  78. class FieldOptions;
  79. class EnumOptions;
  80. class EnumValueOptions;
  81. class ServiceOptions;
  82. class MethodOptions;
  83. class FileOptions;
  84. class UninterpretedOption;
  85. // Defined in message.h
  86. class Message;
  87. // Defined in descriptor.cc
  88. class DescriptorBuilder;
  89. class FileDescriptorTables;
  90. // Defined in unknown_field_set.h.
  91. class UnknownField;
  92. // Describes a type of protocol message, or a particular group within a
  93. // message. To obtain the Descriptor for a given message object, call
  94. // Message::GetDescriptor(). Generated message classes also have a
  95. // static method called descriptor() which returns the type's descriptor.
  96. // Use DescriptorPool to construct your own descriptors.
  97. class LIBPROTOBUF_EXPORT Descriptor {
  98. public:
  99. // The name of the message type, not including its scope.
  100. const string& name() const;
  101. // The fully-qualified name of the message type, scope delimited by
  102. // periods. For example, message type "Foo" which is declared in package
  103. // "bar" has full name "bar.Foo". If a type "Baz" is nested within
  104. // Foo, Baz's full_name is "bar.Foo.Baz". To get only the part that
  105. // comes after the last '.', use name().
  106. const string& full_name() const;
  107. // Index of this descriptor within the file or containing type's message
  108. // type array.
  109. int index() const;
  110. // The .proto file in which this message type was defined. Never NULL.
  111. const FileDescriptor* file() const;
  112. // If this Descriptor describes a nested type, this returns the type
  113. // in which it is nested. Otherwise, returns NULL.
  114. const Descriptor* containing_type() const;
  115. // Get options for this message type. These are specified in the .proto file
  116. // by placing lines like "option foo = 1234;" in the message definition.
  117. // Allowed options are defined by MessageOptions in
  118. // google/protobuf/descriptor.proto, and any available extensions of that
  119. // message.
  120. const MessageOptions& options() const;
  121. // Write the contents of this Descriptor into the given DescriptorProto.
  122. // The target DescriptorProto must be clear before calling this; if it
  123. // isn't, the result may be garbage.
  124. void CopyTo(DescriptorProto* proto) const;
  125. // Write the contents of this decriptor in a human-readable form. Output
  126. // will be suitable for re-parsing.
  127. string DebugString() const;
  128. // Field stuff -----------------------------------------------------
  129. // The number of fields in this message type.
  130. int field_count() const;
  131. // Gets a field by index, where 0 <= index < field_count().
  132. // These are returned in the order they were defined in the .proto file.
  133. const FieldDescriptor* field(int index) const;
  134. // Looks up a field by declared tag number. Returns NULL if no such field
  135. // exists.
  136. const FieldDescriptor* FindFieldByNumber(int number) const;
  137. // Looks up a field by name. Returns NULL if no such field exists.
  138. const FieldDescriptor* FindFieldByName(const string& name) const;
  139. // Looks up a field by lowercased name (as returned by lowercase_name()).
  140. // This lookup may be ambiguous if multiple field names differ only by case,
  141. // in which case the field returned is chosen arbitrarily from the matches.
  142. const FieldDescriptor* FindFieldByLowercaseName(
  143. const string& lowercase_name) const;
  144. // Looks up a field by camel-case name (as returned by camelcase_name()).
  145. // This lookup may be ambiguous if multiple field names differ in a way that
  146. // leads them to have identical camel-case names, in which case the field
  147. // returned is chosen arbitrarily from the matches.
  148. const FieldDescriptor* FindFieldByCamelcaseName(
  149. const string& camelcase_name) const;
  150. // Nested type stuff -----------------------------------------------
  151. // The number of nested types in this message type.
  152. int nested_type_count() const;
  153. // Gets a nested type by index, where 0 <= index < nested_type_count().
  154. // These are returned in the order they were defined in the .proto file.
  155. const Descriptor* nested_type(int index) const;
  156. // Looks up a nested type by name. Returns NULL if no such nested type
  157. // exists.
  158. const Descriptor* FindNestedTypeByName(const string& name) const;
  159. // Enum stuff ------------------------------------------------------
  160. // The number of enum types in this message type.
  161. int enum_type_count() const;
  162. // Gets an enum type by index, where 0 <= index < enum_type_count().
  163. // These are returned in the order they were defined in the .proto file.
  164. const EnumDescriptor* enum_type(int index) const;
  165. // Looks up an enum type by name. Returns NULL if no such enum type exists.
  166. const EnumDescriptor* FindEnumTypeByName(const string& name) const;
  167. // Looks up an enum value by name, among all enum types in this message.
  168. // Returns NULL if no such value exists.
  169. const EnumValueDescriptor* FindEnumValueByName(const string& name) const;
  170. // Extensions ------------------------------------------------------
  171. // A range of field numbers which are designated for third-party
  172. // extensions.
  173. struct ExtensionRange {
  174. int start; // inclusive
  175. int end; // exclusive
  176. };
  177. // The number of extension ranges in this message type.
  178. int extension_range_count() const;
  179. // Gets an extension range by index, where 0 <= index <
  180. // extension_range_count(). These are returned in the order they were defined
  181. // in the .proto file.
  182. const ExtensionRange* extension_range(int index) const;
  183. // Returns true if the number is in one of the extension ranges.
  184. bool IsExtensionNumber(int number) const;
  185. // The number of extensions -- extending *other* messages -- that were
  186. // defined nested within this message type's scope.
  187. int extension_count() const;
  188. // Get an extension by index, where 0 <= index < extension_count().
  189. // These are returned in the order they were defined in the .proto file.
  190. const FieldDescriptor* extension(int index) const;
  191. // Looks up a named extension (which extends some *other* message type)
  192. // defined within this message type's scope.
  193. const FieldDescriptor* FindExtensionByName(const string& name) const;
  194. // Similar to FindFieldByLowercaseName(), but finds extensions defined within
  195. // this message type's scope.
  196. const FieldDescriptor* FindExtensionByLowercaseName(const string& name) const;
  197. // Similar to FindFieldByCamelcaseName(), but finds extensions defined within
  198. // this message type's scope.
  199. const FieldDescriptor* FindExtensionByCamelcaseName(const string& name) const;
  200. private:
  201. typedef MessageOptions OptionsType;
  202. // Internal version of DebugString; controls the level of indenting for
  203. // correct depth
  204. void DebugString(int depth, string *contents) const;
  205. const string* name_;
  206. const string* full_name_;
  207. const FileDescriptor* file_;
  208. const Descriptor* containing_type_;
  209. const MessageOptions* options_;
  210. // True if this is a placeholder for an unknown type.
  211. bool is_placeholder_;
  212. // True if this is a placeholder and the type name wasn't fully-qualified.
  213. bool is_unqualified_placeholder_;
  214. int field_count_;
  215. FieldDescriptor* fields_;
  216. int nested_type_count_;
  217. Descriptor* nested_types_;
  218. int enum_type_count_;
  219. EnumDescriptor* enum_types_;
  220. int extension_range_count_;
  221. ExtensionRange* extension_ranges_;
  222. int extension_count_;
  223. FieldDescriptor* extensions_;
  224. // IMPORTANT: If you add a new field, make sure to search for all instances
  225. // of Allocate<Descriptor>() and AllocateArray<Descriptor>() in descriptor.cc
  226. // and update them to initialize the field.
  227. // Must be constructed using DescriptorPool.
  228. Descriptor() {}
  229. friend class DescriptorBuilder;
  230. friend class EnumDescriptor;
  231. friend class FieldDescriptor;
  232. friend class MethodDescriptor;
  233. friend class FileDescriptor;
  234. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Descriptor);
  235. };
  236. // Describes a single field of a message. To get the descriptor for a given
  237. // field, first get the Descriptor for the message in which it is defined,
  238. // then call Descriptor::FindFieldByName(). To get a FieldDescriptor for
  239. // an extension, do one of the following:
  240. // - Get the Descriptor or FileDescriptor for its containing scope, then
  241. // call Descriptor::FindExtensionByName() or
  242. // FileDescriptor::FindExtensionByName().
  243. // - Given a DescriptorPool, call DescriptorPool::FindExtensionByNumber().
  244. // - Given a Reflection for a message object, call
  245. // Reflection::FindKnownExtensionByName() or
  246. // Reflection::FindKnownExtensionByNumber().
  247. // Use DescriptorPool to construct your own descriptors.
  248. class LIBPROTOBUF_EXPORT FieldDescriptor {
  249. public:
  250. // Identifies a field type. 0 is reserved for errors. The order is weird
  251. // for historical reasons. Types 12 and up are new in proto2.
  252. enum Type {
  253. TYPE_DOUBLE = 1, // double, exactly eight bytes on the wire.
  254. TYPE_FLOAT = 2, // float, exactly four bytes on the wire.
  255. TYPE_INT64 = 3, // int64, varint on the wire. Negative numbers
  256. // take 10 bytes. Use TYPE_SINT64 if negative
  257. // values are likely.
  258. TYPE_UINT64 = 4, // uint64, varint on the wire.
  259. TYPE_INT32 = 5, // int32, varint on the wire. Negative numbers
  260. // take 10 bytes. Use TYPE_SINT32 if negative
  261. // values are likely.
  262. TYPE_FIXED64 = 6, // uint64, exactly eight bytes on the wire.
  263. TYPE_FIXED32 = 7, // uint32, exactly four bytes on the wire.
  264. TYPE_BOOL = 8, // bool, varint on the wire.
  265. TYPE_STRING = 9, // UTF-8 text.
  266. TYPE_GROUP = 10, // Tag-delimited message. Deprecated.
  267. TYPE_MESSAGE = 11, // Length-delimited message.
  268. TYPE_BYTES = 12, // Arbitrary byte array.
  269. TYPE_UINT32 = 13, // uint32, varint on the wire
  270. TYPE_ENUM = 14, // Enum, varint on the wire
  271. TYPE_SFIXED32 = 15, // int32, exactly four bytes on the wire
  272. TYPE_SFIXED64 = 16, // int64, exactly eight bytes on the wire
  273. TYPE_SINT32 = 17, // int32, ZigZag-encoded varint on the wire
  274. TYPE_SINT64 = 18, // int64, ZigZag-encoded varint on the wire
  275. MAX_TYPE = 18, // Constant useful for defining lookup tables
  276. // indexed by Type.
  277. };
  278. // Specifies the C++ data type used to represent the field. There is a
  279. // fixed mapping from Type to CppType where each Type maps to exactly one
  280. // CppType. 0 is reserved for errors.
  281. enum CppType {
  282. CPPTYPE_INT32 = 1, // TYPE_INT32, TYPE_SINT32, TYPE_SFIXED32
  283. CPPTYPE_INT64 = 2, // TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64
  284. CPPTYPE_UINT32 = 3, // TYPE_UINT32, TYPE_FIXED32
  285. CPPTYPE_UINT64 = 4, // TYPE_UINT64, TYPE_FIXED64
  286. CPPTYPE_DOUBLE = 5, // TYPE_DOUBLE
  287. CPPTYPE_FLOAT = 6, // TYPE_FLOAT
  288. CPPTYPE_BOOL = 7, // TYPE_BOOL
  289. CPPTYPE_ENUM = 8, // TYPE_ENUM
  290. CPPTYPE_STRING = 9, // TYPE_STRING, TYPE_BYTES
  291. CPPTYPE_MESSAGE = 10, // TYPE_MESSAGE, TYPE_GROUP
  292. MAX_CPPTYPE = 10, // Constant useful for defining lookup tables
  293. // indexed by CppType.
  294. };
  295. // Identifies whether the field is optional, required, or repeated. 0 is
  296. // reserved for errors.
  297. enum Label {
  298. LABEL_OPTIONAL = 1, // optional
  299. LABEL_REQUIRED = 2, // required
  300. LABEL_REPEATED = 3, // repeated
  301. MAX_LABEL = 3, // Constant useful for defining lookup tables
  302. // indexed by Label.
  303. };
  304. // Valid field numbers are positive integers up to kMaxNumber.
  305. static const int kMaxNumber = (1 << 29) - 1;
  306. // First field number reserved for the protocol buffer library implementation.
  307. // Users may not declare fields that use reserved numbers.
  308. static const int kFirstReservedNumber = 19000;
  309. // Last field number reserved for the protocol buffer library implementation.
  310. // Users may not declare fields that use reserved numbers.
  311. static const int kLastReservedNumber = 19999;
  312. const string& name() const; // Name of this field within the message.
  313. const string& full_name() const; // Fully-qualified name of the field.
  314. const FileDescriptor* file() const;// File in which this field was defined.
  315. bool is_extension() const; // Is this an extension field?
  316. int number() const; // Declared tag number.
  317. // Same as name() except converted to lower-case. This (and especially the
  318. // FindFieldByLowercaseName() method) can be useful when parsing formats
  319. // which prefer to use lowercase naming style. (Although, technically
  320. // field names should be lowercased anyway according to the protobuf style
  321. // guide, so this only makes a difference when dealing with old .proto files
  322. // which do not follow the guide.)
  323. const string& lowercase_name() const;
  324. // Same as name() except converted to camel-case. In this conversion, any
  325. // time an underscore appears in the name, it is removed and the next
  326. // letter is capitalized. Furthermore, the first letter of the name is
  327. // lower-cased. Examples:
  328. // FooBar -> fooBar
  329. // foo_bar -> fooBar
  330. // fooBar -> fooBar
  331. // This (and especially the FindFieldByCamelcaseName() method) can be useful
  332. // when parsing formats which prefer to use camel-case naming style.
  333. const string& camelcase_name() const;
  334. Type type() const; // Declared type of this field.
  335. CppType cpp_type() const; // C++ type of this field.
  336. Label label() const; // optional/required/repeated
  337. bool is_required() const; // shorthand for label() == LABEL_REQUIRED
  338. bool is_optional() const; // shorthand for label() == LABEL_OPTIONAL
  339. bool is_repeated() const; // shorthand for label() == LABEL_REPEATED
  340. bool is_packable() const; // shorthand for is_repeated() &&
  341. // IsTypePackable(type())
  342. // Index of this field within the message's field array, or the file or
  343. // extension scope's extensions array.
  344. int index() const;
  345. // Does this field have an explicitly-declared default value?
  346. bool has_default_value() const;
  347. // Get the field default value if cpp_type() == CPPTYPE_INT32. If no
  348. // explicit default was defined, the default is 0.
  349. int32 default_value_int32() const;
  350. // Get the field default value if cpp_type() == CPPTYPE_INT64. If no
  351. // explicit default was defined, the default is 0.
  352. int64 default_value_int64() const;
  353. // Get the field default value if cpp_type() == CPPTYPE_UINT32. If no
  354. // explicit default was defined, the default is 0.
  355. uint32 default_value_uint32() const;
  356. // Get the field default value if cpp_type() == CPPTYPE_UINT64. If no
  357. // explicit default was defined, the default is 0.
  358. uint64 default_value_uint64() const;
  359. // Get the field default value if cpp_type() == CPPTYPE_FLOAT. If no
  360. // explicit default was defined, the default is 0.0.
  361. float default_value_float() const;
  362. // Get the field default value if cpp_type() == CPPTYPE_DOUBLE. If no
  363. // explicit default was defined, the default is 0.0.
  364. double default_value_double() const;
  365. // Get the field default value if cpp_type() == CPPTYPE_BOOL. If no
  366. // explicit default was defined, the default is false.
  367. bool default_value_bool() const;
  368. // Get the field default value if cpp_type() == CPPTYPE_ENUM. If no
  369. // explicit default was defined, the default is the first value defined
  370. // in the enum type (all enum types are required to have at least one value).
  371. // This never returns NULL.
  372. const EnumValueDescriptor* default_value_enum() const;
  373. // Get the field default value if cpp_type() == CPPTYPE_STRING. If no
  374. // explicit default was defined, the default is the empty string.
  375. const string& default_value_string() const;
  376. // The Descriptor for the message of which this is a field. For extensions,
  377. // this is the extended type. Never NULL.
  378. const Descriptor* containing_type() const;
  379. // An extension may be declared within the scope of another message. If this
  380. // field is an extension (is_extension() is true), then extension_scope()
  381. // returns that message, or NULL if the extension was declared at global
  382. // scope. If this is not an extension, extension_scope() is undefined (may
  383. // assert-fail).
  384. const Descriptor* extension_scope() const;
  385. // If type is TYPE_MESSAGE or TYPE_GROUP, returns a descriptor for the
  386. // message or the group type. Otherwise, undefined.
  387. const Descriptor* message_type() const;
  388. // If type is TYPE_ENUM, returns a descriptor for the enum. Otherwise,
  389. // undefined.
  390. const EnumDescriptor* enum_type() const;
  391. // EXPERIMENTAL; DO NOT USE.
  392. // If this field is a map field, experimental_map_key() is the field
  393. // that is the key for this map.
  394. // experimental_map_key()->containing_type() is the same as message_type().
  395. const FieldDescriptor* experimental_map_key() const;
  396. // Get the FieldOptions for this field. This includes things listed in
  397. // square brackets after the field definition. E.g., the field:
  398. // optional string text = 1 [ctype=CORD];
  399. // has the "ctype" option set. Allowed options are defined by FieldOptions
  400. // in google/protobuf/descriptor.proto, and any available extensions of that
  401. // message.
  402. const FieldOptions& options() const;
  403. // See Descriptor::CopyTo().
  404. void CopyTo(FieldDescriptorProto* proto) const;
  405. // See Descriptor::DebugString().
  406. string DebugString() const;
  407. // Helper method to get the CppType for a particular Type.
  408. static CppType TypeToCppType(Type type);
  409. // Return true iff [packed = true] is valid for fields of this type.
  410. static inline bool IsTypePackable(Type field_type);
  411. private:
  412. typedef FieldOptions OptionsType;
  413. // See Descriptor::DebugString().
  414. void DebugString(int depth, string *contents) const;
  415. // formats the default value appropriately and returns it as a string.
  416. // Must have a default value to call this. If quote_string_type is true, then
  417. // types of CPPTYPE_STRING whill be surrounded by quotes and CEscaped.
  418. string DefaultValueAsString(bool quote_string_type) const;
  419. const string* name_;
  420. const string* full_name_;
  421. const string* lowercase_name_;
  422. const string* camelcase_name_;
  423. const FileDescriptor* file_;
  424. int number_;
  425. Type type_;
  426. Label label_;
  427. bool is_extension_;
  428. const Descriptor* containing_type_;
  429. const Descriptor* extension_scope_;
  430. const Descriptor* message_type_;
  431. const EnumDescriptor* enum_type_;
  432. const FieldDescriptor* experimental_map_key_;
  433. const FieldOptions* options_;
  434. // IMPORTANT: If you add a new field, make sure to search for all instances
  435. // of Allocate<FieldDescriptor>() and AllocateArray<FieldDescriptor>() in
  436. // descriptor.cc and update them to initialize the field.
  437. bool has_default_value_;
  438. union {
  439. int32 default_value_int32_;
  440. int64 default_value_int64_;
  441. uint32 default_value_uint32_;
  442. uint64 default_value_uint64_;
  443. float default_value_float_;
  444. double default_value_double_;
  445. bool default_value_bool_;
  446. const EnumValueDescriptor* default_value_enum_;
  447. const string* default_value_string_;
  448. };
  449. static const CppType kTypeToCppTypeMap[MAX_TYPE + 1];
  450. static const char * const kTypeToName[MAX_TYPE + 1];
  451. static const char * const kLabelToName[MAX_LABEL + 1];
  452. // Must be constructed using DescriptorPool.
  453. FieldDescriptor() {}
  454. friend class DescriptorBuilder;
  455. friend class FileDescriptor;
  456. friend class Descriptor;
  457. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldDescriptor);
  458. };
  459. // Describes an enum type defined in a .proto file. To get the EnumDescriptor
  460. // for a generated enum type, call TypeName_descriptor(). Use DescriptorPool
  461. // to construct your own descriptors.
  462. class LIBPROTOBUF_EXPORT EnumDescriptor {
  463. public:
  464. // The name of this enum type in the containing scope.
  465. const string& name() const;
  466. // The fully-qualified name of the enum type, scope delimited by periods.
  467. const string& full_name() const;
  468. // Index of this enum within the file or containing message's enum array.
  469. int index() const;
  470. // The .proto file in which this enum type was defined. Never NULL.
  471. const FileDescriptor* file() const;
  472. // The number of values for this EnumDescriptor. Guaranteed to be greater
  473. // than zero.
  474. int value_count() const;
  475. // Gets a value by index, where 0 <= index < value_count().
  476. // These are returned in the order they were defined in the .proto file.
  477. const EnumValueDescriptor* value(int index) const;
  478. // Looks up a value by name. Returns NULL if no such value exists.
  479. const EnumValueDescriptor* FindValueByName(const string& name) const;
  480. // Looks up a value by number. Returns NULL if no such value exists. If
  481. // multiple values have this number, the first one defined is returned.
  482. const EnumValueDescriptor* FindValueByNumber(int number) const;
  483. // If this enum type is nested in a message type, this is that message type.
  484. // Otherwise, NULL.
  485. const Descriptor* containing_type() const;
  486. // Get options for this enum type. These are specified in the .proto file by
  487. // placing lines like "option foo = 1234;" in the enum definition. Allowed
  488. // options are defined by EnumOptions in google/protobuf/descriptor.proto,
  489. // and any available extensions of that message.
  490. const EnumOptions& options() const;
  491. // See Descriptor::CopyTo().
  492. void CopyTo(EnumDescriptorProto* proto) const;
  493. // See Descriptor::DebugString().
  494. string DebugString() const;
  495. private:
  496. typedef EnumOptions OptionsType;
  497. // See Descriptor::DebugString().
  498. void DebugString(int depth, string *contents) const;
  499. const string* name_;
  500. const string* full_name_;
  501. const FileDescriptor* file_;
  502. const Descriptor* containing_type_;
  503. const EnumOptions* options_;
  504. // True if this is a placeholder for an unknown type.
  505. bool is_placeholder_;
  506. // True if this is a placeholder and the type name wasn't fully-qualified.
  507. bool is_unqualified_placeholder_;
  508. int value_count_;
  509. EnumValueDescriptor* values_;
  510. // IMPORTANT: If you add a new field, make sure to search for all instances
  511. // of Allocate<EnumDescriptor>() and AllocateArray<EnumDescriptor>() in
  512. // descriptor.cc and update them to initialize the field.
  513. // Must be constructed using DescriptorPool.
  514. EnumDescriptor() {}
  515. friend class DescriptorBuilder;
  516. friend class Descriptor;
  517. friend class FieldDescriptor;
  518. friend class EnumValueDescriptor;
  519. friend class FileDescriptor;
  520. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumDescriptor);
  521. };
  522. // Describes an individual enum constant of a particular type. To get the
  523. // EnumValueDescriptor for a given enum value, first get the EnumDescriptor
  524. // for its type, then use EnumDescriptor::FindValueByName() or
  525. // EnumDescriptor::FindValueByNumber(). Use DescriptorPool to construct
  526. // your own descriptors.
  527. class LIBPROTOBUF_EXPORT EnumValueDescriptor {
  528. public:
  529. const string& name() const; // Name of this enum constant.
  530. int index() const; // Index within the enums's Descriptor.
  531. int number() const; // Numeric value of this enum constant.
  532. // The full_name of an enum value is a sibling symbol of the enum type.
  533. // e.g. the full name of FieldDescriptorProto::TYPE_INT32 is actually
  534. // "google.protobuf.FieldDescriptorProto.TYPE_INT32", NOT
  535. // "google.protobuf.FieldDescriptorProto.Type.TYPE_INT32". This is to conform
  536. // with C++ scoping rules for enums.
  537. const string& full_name() const;
  538. // The type of this value. Never NULL.
  539. const EnumDescriptor* type() const;
  540. // Get options for this enum value. These are specified in the .proto file
  541. // by adding text like "[foo = 1234]" after an enum value definition.
  542. // Allowed options are defined by EnumValueOptions in
  543. // google/protobuf/descriptor.proto, and any available extensions of that
  544. // message.
  545. const EnumValueOptions& options() const;
  546. // See Descriptor::CopyTo().
  547. void CopyTo(EnumValueDescriptorProto* proto) const;
  548. // See Descriptor::DebugString().
  549. string DebugString() const;
  550. private:
  551. typedef EnumValueOptions OptionsType;
  552. // See Descriptor::DebugString().
  553. void DebugString(int depth, string *contents) const;
  554. const string* name_;
  555. const string* full_name_;
  556. int number_;
  557. const EnumDescriptor* type_;
  558. const EnumValueOptions* options_;
  559. // IMPORTANT: If you add a new field, make sure to search for all instances
  560. // of Allocate<EnumValueDescriptor>() and AllocateArray<EnumValueDescriptor>()
  561. // in descriptor.cc and update them to initialize the field.
  562. // Must be constructed using DescriptorPool.
  563. EnumValueDescriptor() {}
  564. friend class DescriptorBuilder;
  565. friend class EnumDescriptor;
  566. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumValueDescriptor);
  567. };
  568. // Describes an RPC service. To get the ServiceDescriptor for a service,
  569. // call Service::GetDescriptor(). Generated service classes also have a
  570. // static method called descriptor() which returns the type's
  571. // ServiceDescriptor. Use DescriptorPool to construct your own descriptors.
  572. class LIBPROTOBUF_EXPORT ServiceDescriptor {
  573. public:
  574. // The name of the service, not including its containing scope.
  575. const string& name() const;
  576. // The fully-qualified name of the service, scope delimited by periods.
  577. const string& full_name() const;
  578. // Index of this service within the file's services array.
  579. int index() const;
  580. // The .proto file in which this service was defined. Never NULL.
  581. const FileDescriptor* file() const;
  582. // Get options for this service type. These are specified in the .proto file
  583. // by placing lines like "option foo = 1234;" in the service definition.
  584. // Allowed options are defined by ServiceOptions in
  585. // google/protobuf/descriptor.proto, and any available extensions of that
  586. // message.
  587. const ServiceOptions& options() const;
  588. // The number of methods this service defines.
  589. int method_count() const;
  590. // Gets a MethodDescriptor by index, where 0 <= index < method_count().
  591. // These are returned in the order they were defined in the .proto file.
  592. const MethodDescriptor* method(int index) const;
  593. // Look up a MethodDescriptor by name.
  594. const MethodDescriptor* FindMethodByName(const string& name) const;
  595. // See Descriptor::CopyTo().
  596. void CopyTo(ServiceDescriptorProto* proto) const;
  597. // See Descriptor::DebugString().
  598. string DebugString() const;
  599. private:
  600. typedef ServiceOptions OptionsType;
  601. // See Descriptor::DebugString().
  602. void DebugString(string *contents) const;
  603. const string* name_;
  604. const string* full_name_;
  605. const FileDescriptor* file_;
  606. const ServiceOptions* options_;
  607. int method_count_;
  608. MethodDescriptor* methods_;
  609. // IMPORTANT: If you add a new field, make sure to search for all instances
  610. // of Allocate<ServiceDescriptor>() and AllocateArray<ServiceDescriptor>() in
  611. // descriptor.cc and update them to initialize the field.
  612. // Must be constructed using DescriptorPool.
  613. ServiceDescriptor() {}
  614. friend class DescriptorBuilder;
  615. friend class FileDescriptor;
  616. friend class MethodDescriptor;
  617. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ServiceDescriptor);
  618. };
  619. // Describes an individual service method. To obtain a MethodDescriptor given
  620. // a service, first get its ServiceDescriptor, then call
  621. // ServiceDescriptor::FindMethodByName(). Use DescriptorPool to construct your
  622. // own descriptors.
  623. class LIBPROTOBUF_EXPORT MethodDescriptor {
  624. public:
  625. // Name of this method, not including containing scope.
  626. const string& name() const;
  627. // The fully-qualified name of the method, scope delimited by periods.
  628. const string& full_name() const;
  629. // Index within the service's Descriptor.
  630. int index() const;
  631. // Gets the service to which this method belongs. Never NULL.
  632. const ServiceDescriptor* service() const;
  633. // Gets the type of protocol message which this method accepts as input.
  634. const Descriptor* input_type() const;
  635. // Gets the type of protocol message which this message produces as output.
  636. const Descriptor* output_type() const;
  637. // Get options for this method. These are specified in the .proto file by
  638. // placing lines like "option foo = 1234;" in curly-braces after a method
  639. // declaration. Allowed options are defined by MethodOptions in
  640. // google/protobuf/descriptor.proto, and any available extensions of that
  641. // message.
  642. const MethodOptions& options() const;
  643. // See Descriptor::CopyTo().
  644. void CopyTo(MethodDescriptorProto* proto) const;
  645. // See Descriptor::DebugString().
  646. string DebugString() const;
  647. private:
  648. typedef MethodOptions OptionsType;
  649. // See Descriptor::DebugString().
  650. void DebugString(int depth, string *contents) const;
  651. const string* name_;
  652. const string* full_name_;
  653. const ServiceDescriptor* service_;
  654. const Descriptor* input_type_;
  655. const Descriptor* output_type_;
  656. const MethodOptions* options_;
  657. // IMPORTANT: If you add a new field, make sure to search for all instances
  658. // of Allocate<MethodDescriptor>() and AllocateArray<MethodDescriptor>() in
  659. // descriptor.cc and update them to initialize the field.
  660. // Must be constructed using DescriptorPool.
  661. MethodDescriptor() {}
  662. friend class DescriptorBuilder;
  663. friend class ServiceDescriptor;
  664. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MethodDescriptor);
  665. };
  666. // Describes a whole .proto file. To get the FileDescriptor for a compiled-in
  667. // file, get the descriptor for something defined in that file and call
  668. // descriptor->file(). Use DescriptorPool to construct your own descriptors.
  669. class LIBPROTOBUF_EXPORT FileDescriptor {
  670. public:
  671. // The filename, relative to the source tree.
  672. // e.g. "google/protobuf/descriptor.proto"
  673. const string& name() const;
  674. // The package, e.g. "google.protobuf.compiler".
  675. const string& package() const;
  676. // The DescriptorPool in which this FileDescriptor and all its contents were
  677. // allocated. Never NULL.
  678. const DescriptorPool* pool() const;
  679. // The number of files imported by this one.
  680. int dependency_count() const;
  681. // Gets an imported file by index, where 0 <= index < dependency_count().
  682. // These are returned in the order they were defined in the .proto file.
  683. const FileDescriptor* dependency(int index) const;
  684. // Number of top-level message types defined in this file. (This does not
  685. // include nested types.)
  686. int message_type_count() const;
  687. // Gets a top-level message type, where 0 <= index < message_type_count().
  688. // These are returned in the order they were defined in the .proto file.
  689. const Descriptor* message_type(int index) const;
  690. // Number of top-level enum types defined in this file. (This does not
  691. // include nested types.)
  692. int enum_type_count() const;
  693. // Gets a top-level enum type, where 0 <= index < enum_type_count().
  694. // These are returned in the order they were defined in the .proto file.
  695. const EnumDescriptor* enum_type(int index) const;
  696. // Number of services defined in this file.
  697. int service_count() const;
  698. // Gets a service, where 0 <= index < service_count().
  699. // These are returned in the order they were defined in the .proto file.
  700. const ServiceDescriptor* service(int index) const;
  701. // Number of extensions defined at file scope. (This does not include
  702. // extensions nested within message types.)
  703. int extension_count() const;
  704. // Gets an extension's descriptor, where 0 <= index < extension_count().
  705. // These are returned in the order they were defined in the .proto file.
  706. const FieldDescriptor* extension(int index) const;
  707. // Get options for this file. These are specified in the .proto file by
  708. // placing lines like "option foo = 1234;" at the top level, outside of any
  709. // other definitions. Allowed options are defined by FileOptions in
  710. // google/protobuf/descriptor.proto, and any available extensions of that
  711. // message.
  712. const FileOptions& options() const;
  713. // Find a top-level message type by name. Returns NULL if not found.
  714. const Descriptor* FindMessageTypeByName(const string& name) const;
  715. // Find a top-level enum type by name. Returns NULL if not found.
  716. const EnumDescriptor* FindEnumTypeByName(const string& name) const;
  717. // Find an enum value defined in any top-level enum by name. Returns NULL if
  718. // not found.
  719. const EnumValueDescriptor* FindEnumValueByName(const string& name) const;
  720. // Find a service definition by name. Returns NULL if not found.
  721. const ServiceDescriptor* FindServiceByName(const string& name) const;
  722. // Find a top-level extension definition by name. Returns NULL if not found.
  723. const FieldDescriptor* FindExtensionByName(const string& name) const;
  724. // Similar to FindExtensionByName(), but searches by lowercased-name. See
  725. // Descriptor::FindFieldByLowercaseName().
  726. const FieldDescriptor* FindExtensionByLowercaseName(const string& name) const;
  727. // Similar to FindExtensionByName(), but searches by camelcased-name. See
  728. // Descriptor::FindFieldByCamelcaseName().
  729. const FieldDescriptor* FindExtensionByCamelcaseName(const string& name) const;
  730. // See Descriptor::CopyTo().
  731. void CopyTo(FileDescriptorProto* proto) const;
  732. // See Descriptor::DebugString().
  733. string DebugString() const;
  734. private:
  735. typedef FileOptions OptionsType;
  736. const string* name_;
  737. const string* package_;
  738. const DescriptorPool* pool_;
  739. int dependency_count_;
  740. const FileDescriptor** dependencies_;
  741. int message_type_count_;
  742. Descriptor* message_types_;
  743. int enum_type_count_;
  744. EnumDescriptor* enum_types_;
  745. int service_count_;
  746. ServiceDescriptor* services_;
  747. int extension_count_;
  748. FieldDescriptor* extensions_;
  749. const FileOptions* options_;
  750. const FileDescriptorTables* tables_;
  751. // IMPORTANT: If you add a new field, make sure to search for all instances
  752. // of Allocate<FileDescriptor>() and AllocateArray<FileDescriptor>() in
  753. // descriptor.cc and update them to initialize the field.
  754. FileDescriptor() {}
  755. friend class DescriptorBuilder;
  756. friend class Descriptor;
  757. friend class FieldDescriptor;
  758. friend class EnumDescriptor;
  759. friend class ServiceDescriptor;
  760. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileDescriptor);
  761. };
  762. // ===================================================================
  763. // Used to construct descriptors.
  764. //
  765. // Normally you won't want to build your own descriptors. Message classes
  766. // constructed by the protocol compiler will provide them for you. However,
  767. // if you are implementing Message on your own, or if you are writing a
  768. // program which can operate on totally arbitrary types and needs to load
  769. // them from some sort of database, you might need to.
  770. //
  771. // Since Descriptors are composed of a whole lot of cross-linked bits of
  772. // data that would be a pain to put together manually, the
  773. // DescriptorPool class is provided to make the process easier. It can
  774. // take a FileDescriptorProto (defined in descriptor.proto), validate it,
  775. // and convert it to a set of nicely cross-linked Descriptors.
  776. //
  777. // DescriptorPool also helps with memory management. Descriptors are
  778. // composed of many objects containing static data and pointers to each
  779. // other. In all likelihood, when it comes time to delete this data,
  780. // you'll want to delete it all at once. In fact, it is not uncommon to
  781. // have a whole pool of descriptors all cross-linked with each other which
  782. // you wish to delete all at once. This class represents such a pool, and
  783. // handles the memory management for you.
  784. //
  785. // You can also search for descriptors within a DescriptorPool by name, and
  786. // extensions by number.
  787. class LIBPROTOBUF_EXPORT DescriptorPool {
  788. public:
  789. // Create a normal, empty DescriptorPool.
  790. DescriptorPool();
  791. // Constructs a DescriptorPool that, when it can't find something among the
  792. // descriptors already in the pool, looks for it in the given
  793. // DescriptorDatabase.
  794. // Notes:
  795. // - If a DescriptorPool is constructed this way, its BuildFile*() methods
  796. // must not be called (they will assert-fail). The only way to populate
  797. // the pool with descriptors is to call the Find*By*() methods.
  798. // - The Find*By*() methods may block the calling thread if the
  799. // DescriptorDatabase blocks. This in turn means that parsing messages
  800. // may block if they need to look up extensions.
  801. // - The Find*By*() methods will use mutexes for thread-safety, thus making
  802. // them slower even when they don't have to fall back to the database.
  803. // In fact, even the Find*By*() methods of descriptor objects owned by
  804. // this pool will be slower, since they will have to obtain locks too.
  805. // - An ErrorCollector may optionally be given to collect validation errors
  806. // in files loaded from the database. If not given, errors will be printed
  807. // to GOOGLE_LOG(ERROR). Remember that files are built on-demand, so this
  808. // ErrorCollector may be called from any thread that calls one of the
  809. // Find*By*() methods.
  810. class ErrorCollector;
  811. explicit DescriptorPool(DescriptorDatabase* fallback_database,
  812. ErrorCollector* error_collector = NULL);
  813. ~DescriptorPool();
  814. // Get a pointer to the generated pool. Generated protocol message classes
  815. // which are compiled into the binary will allocate their descriptors in
  816. // this pool. Do not add your own descriptors to this pool.
  817. static const DescriptorPool* generated_pool();
  818. // Find a FileDescriptor in the pool by file name. Returns NULL if not
  819. // found.
  820. const FileDescriptor* FindFileByName(const string& name) const;
  821. // Find the FileDescriptor in the pool which defines the given symbol.
  822. // If any of the Find*ByName() methods below would succeed, then this is
  823. // equivalent to calling that method and calling the result's file() method.
  824. // Otherwise this returns NULL.
  825. const FileDescriptor* FindFileContainingSymbol(
  826. const string& symbol_name) const;
  827. // Looking up descriptors ------------------------------------------
  828. // These find descriptors by fully-qualified name. These will find both
  829. // top-level descriptors and nested descriptors. They return NULL if not
  830. // found.
  831. const Descriptor* FindMessageTypeByName(const string& name) const;
  832. const FieldDescriptor* FindFieldByName(const string& name) const;
  833. const FieldDescriptor* FindExtensionByName(const string& name) const;
  834. const EnumDescriptor* FindEnumTypeByName(const string& name) const;
  835. const EnumValueDescriptor* FindEnumValueByName(const string& name) const;
  836. const ServiceDescriptor* FindServiceByName(const string& name) const;
  837. const MethodDescriptor* FindMethodByName(const string& name) const;
  838. // Finds an extension of the given type by number. The extendee must be
  839. // a member of this DescriptorPool or one of its underlays.
  840. const FieldDescriptor* FindExtensionByNumber(const Descriptor* extendee,
  841. int number) const;
  842. // Finds extensions of extendee. The extensions will be appended to
  843. // out in an undefined order. Only extensions defined directly in
  844. // this DescriptorPool or one of its underlays are guaranteed to be
  845. // found: extensions defined in the fallback database might not be found
  846. // depending on the database implementation.
  847. void FindAllExtensions(const Descriptor* extendee,
  848. vector<const FieldDescriptor*>* out) const;
  849. // Building descriptors --------------------------------------------
  850. // When converting a FileDescriptorProto to a FileDescriptor, various
  851. // errors might be detected in the input. The caller may handle these
  852. // programmatically by implementing an ErrorCollector.
  853. class LIBPROTOBUF_EXPORT ErrorCollector {
  854. public:
  855. inline ErrorCollector() {}
  856. virtual ~ErrorCollector();
  857. // These constants specify what exact part of the construct is broken.
  858. // This is useful e.g. for mapping the error back to an exact location
  859. // in a .proto file.
  860. enum ErrorLocation {
  861. NAME, // the symbol name, or the package name for files
  862. NUMBER, // field or extension range number
  863. TYPE, // field type
  864. EXTENDEE, // field extendee
  865. DEFAULT_VALUE, // field default value
  866. INPUT_TYPE, // method input type
  867. OUTPUT_TYPE, // method output type
  868. OPTION_NAME, // name in assignment
  869. OPTION_VALUE, // value in option assignment
  870. OTHER // some other problem
  871. };
  872. // Reports an error in the FileDescriptorProto.
  873. virtual void AddError(
  874. const string& filename, // File name in which the error occurred.
  875. const string& element_name, // Full name of the erroneous element.
  876. const Message* descriptor, // Descriptor of the erroneous element.
  877. ErrorLocation location, // One of the location constants, above.
  878. const string& message // Human-readable error message.
  879. ) = 0;
  880. private:
  881. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ErrorCollector);
  882. };
  883. // Convert the FileDescriptorProto to real descriptors and place them in
  884. // this DescriptorPool. All dependencies of the file must already be in
  885. // the pool. Returns the resulting FileDescriptor, or NULL if there were
  886. // problems with the input (e.g. the message was invalid, or dependencies
  887. // were missing). Details about the errors are written to GOOGLE_LOG(ERROR).
  888. const FileDescriptor* BuildFile(const FileDescriptorProto& proto);
  889. // Same as BuildFile() except errors are sent to the given ErrorCollector.
  890. const FileDescriptor* BuildFileCollectingErrors(
  891. const FileDescriptorProto& proto,
  892. ErrorCollector* error_collector);
  893. // By default, it is an error if a FileDescriptorProto contains references
  894. // to types or other files that are not found in the DescriptorPool (or its
  895. // backing DescriptorDatabase, if any). If you call
  896. // AllowUnknownDependencies(), however, then unknown types and files
  897. // will be replaced by placeholder descriptors. This can allow you to
  898. // perform some useful operations with a .proto file even if you do not
  899. // have access to other .proto files on which it depends. However, some
  900. // heuristics must be used to fill in the gaps in information, and these
  901. // can lead to descriptors which are inaccurate. For example, the
  902. // DescriptorPool may be forced to guess whether an unknown type is a message
  903. // or an enum, as well as what package it resides in. Furthermore,
  904. // placeholder types will not be discoverable via FindMessageTypeByName()
  905. // and similar methods, which could confuse some descriptor-based algorithms.
  906. // Generally, the results of this option should only be relied upon for
  907. // debugging purposes.
  908. void AllowUnknownDependencies() { allow_unknown_ = true; }
  909. // Internal stuff --------------------------------------------------
  910. // These methods MUST NOT be called from outside the proto2 library.
  911. // These methods may contain hidden pitfalls and may be removed in a
  912. // future library version.
  913. // Create a DescriptorPool which is overlaid on top of some other pool.
  914. // If you search for a descriptor in the overlay and it is not found, the
  915. // underlay will be searched as a backup. If the underlay has its own
  916. // underlay, that will be searched next, and so on. This also means that
  917. // files built in the overlay will be cross-linked with the underlay's
  918. // descriptors if necessary. The underlay remains property of the caller;
  919. // it must remain valid for the lifetime of the newly-constructed pool.
  920. //
  921. // Example: Say you want to parse a .proto file at runtime in order to use
  922. // its type with a DynamicMessage. Say this .proto file has dependencies,
  923. // but you know that all the dependencies will be things that are already
  924. // compiled into the binary. For ease of use, you'd like to load the types
  925. // right out of generated_pool() rather than have to parse redundant copies
  926. // of all these .protos and runtime. But, you don't want to add the parsed
  927. // types directly into generated_pool(): this is not allowed, and would be
  928. // bad design anyway. So, instead, you could use generated_pool() as an
  929. // underlay for a new DescriptorPool in which you add only the new file.
  930. //
  931. // WARNING: Use of underlays can lead to many subtle gotchas. Instead,
  932. // try to formulate what you want to do in terms of DescriptorDatabases.
  933. explicit DescriptorPool(const DescriptorPool* underlay);
  934. // Called by generated classes at init time to add their descriptors to
  935. // generated_pool. Do NOT call this in your own code! filename must be a
  936. // permanent string (e.g. a string literal).
  937. static void InternalAddGeneratedFile(
  938. const void* encoded_file_descriptor, int size);
  939. // For internal use only: Gets a non-const pointer to the generated pool.
  940. // This is called at static-initialization time only, so thread-safety is
  941. // not a concern. If both an underlay and a fallback database are present,
  942. // the fallback database takes precedence.
  943. static DescriptorPool* internal_generated_pool();
  944. // For internal use only: Changes the behavior of BuildFile() such that it
  945. // allows the file to make reference to message types declared in other files
  946. // which it did not officially declare as dependencies.
  947. void InternalDontEnforceDependencies();
  948. // For internal use only.
  949. void internal_set_underlay(const DescriptorPool* underlay) {
  950. underlay_ = underlay;
  951. }
  952. // For internal (unit test) use only: Returns true if a FileDescriptor has
  953. // been constructed for the given file, false otherwise. Useful for testing
  954. // lazy descriptor initialization behavior.
  955. bool InternalIsFileLoaded(const string& filename) const;
  956. private:
  957. friend class Descriptor;
  958. friend class FieldDescriptor;
  959. friend class EnumDescriptor;
  960. friend class ServiceDescriptor;
  961. friend class FileDescriptor;
  962. friend class DescriptorBuilder;
  963. // Tries to find something in the fallback database and link in the
  964. // corresponding proto file. Returns true if successful, in which case
  965. // the caller should search for the thing again. These are declared
  966. // const because they are called by (semantically) const methods.
  967. bool TryFindFileInFallbackDatabase(const string& name) const;
  968. bool TryFindSymbolInFallbackDatabase(const string& name) const;
  969. bool TryFindExtensionInFallbackDatabase(const Descriptor* containing_type,
  970. int field_number) const;
  971. // Like BuildFile() but called internally when the file has been loaded from
  972. // fallback_database_. Declared const because it is called by (semantically)
  973. // const methods.
  974. const FileDescriptor* BuildFileFromDatabase(
  975. const FileDescriptorProto& proto) const;
  976. // If fallback_database_ is NULL, this is NULL. Otherwise, this is a mutex
  977. // which must be locked while accessing tables_.
  978. Mutex* mutex_;
  979. // See constructor.
  980. DescriptorDatabase* fallback_database_;
  981. ErrorCollector* default_error_collector_;
  982. const DescriptorPool* underlay_;
  983. // This class contains a lot of hash maps with complicated types that
  984. // we'd like to keep out of the header.
  985. class Tables;
  986. scoped_ptr<Tables> tables_;
  987. bool enforce_dependencies_;
  988. bool allow_unknown_;
  989. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorPool);
  990. };
  991. // inline methods ====================================================
  992. // These macros makes this repetitive code more readable.
  993. #define PROTOBUF_DEFINE_ACCESSOR(CLASS, FIELD, TYPE) \
  994. inline TYPE CLASS::FIELD() const { return FIELD##_; }
  995. // Strings fields are stored as pointers but returned as const references.
  996. #define PROTOBUF_DEFINE_STRING_ACCESSOR(CLASS, FIELD) \
  997. inline const string& CLASS::FIELD() const { return *FIELD##_; }
  998. // Arrays take an index parameter, obviously.
  999. #define PROTOBUF_DEFINE_ARRAY_ACCESSOR(CLASS, FIELD, TYPE) \
  1000. inline TYPE CLASS::FIELD(int index) const { return FIELD##s_ + index; }
  1001. #define PROTOBUF_DEFINE_OPTIONS_ACCESSOR(CLASS, TYPE) \
  1002. inline const TYPE& CLASS::options() const { return *options_; }
  1003. PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, name)
  1004. PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, full_name)
  1005. PROTOBUF_DEFINE_ACCESSOR(Descriptor, file, const FileDescriptor*)
  1006. PROTOBUF_DEFINE_ACCESSOR(Descriptor, containing_type, const Descriptor*)
  1007. PROTOBUF_DEFINE_ACCESSOR(Descriptor, field_count, int)
  1008. PROTOBUF_DEFINE_ACCESSOR(Descriptor, nested_type_count, int)
  1009. PROTOBUF_DEFINE_ACCESSOR(Descriptor, enum_type_count, int)
  1010. PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, field, const FieldDescriptor*)
  1011. PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, nested_type, const Descriptor*)
  1012. PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, enum_type, const EnumDescriptor*)
  1013. PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_range_count, int)
  1014. PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_count, int)
  1015. PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension_range,
  1016. const Descriptor::ExtensionRange*)
  1017. PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension,
  1018. const FieldDescriptor*)
  1019. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(Descriptor, MessageOptions);
  1020. PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, name)
  1021. PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, full_name)
  1022. PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, lowercase_name)
  1023. PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, camelcase_name)
  1024. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, file, const FileDescriptor*)
  1025. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, number, int)
  1026. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, is_extension, bool)
  1027. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, type, FieldDescriptor::Type)
  1028. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, label, FieldDescriptor::Label)
  1029. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, containing_type, const Descriptor*)
  1030. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, extension_scope, const Descriptor*)
  1031. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, message_type, const Descriptor*)
  1032. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, enum_type, const EnumDescriptor*)
  1033. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, experimental_map_key,
  1034. const FieldDescriptor*)
  1035. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FieldDescriptor, FieldOptions);
  1036. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_default_value, bool)
  1037. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int32 , int32 )
  1038. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int64 , int64 )
  1039. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint32, uint32)
  1040. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint64, uint64)
  1041. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_float , float )
  1042. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_double, double)
  1043. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_bool , bool )
  1044. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_enum,
  1045. const EnumValueDescriptor*)
  1046. PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, default_value_string)
  1047. PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, name)
  1048. PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, full_name)
  1049. PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, file, const FileDescriptor*)
  1050. PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, containing_type, const Descriptor*)
  1051. PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, value_count, int)
  1052. PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, value,
  1053. const EnumValueDescriptor*)
  1054. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumDescriptor, EnumOptions);
  1055. PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, name)
  1056. PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, full_name)
  1057. PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, number, int)
  1058. PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, type, const EnumDescriptor*)
  1059. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumValueDescriptor, EnumValueOptions);
  1060. PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, name)
  1061. PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, full_name)
  1062. PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, file, const FileDescriptor*)
  1063. PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, method_count, int)
  1064. PROTOBUF_DEFINE_ARRAY_ACCESSOR(ServiceDescriptor, method,
  1065. const MethodDescriptor*)
  1066. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(ServiceDescriptor, ServiceOptions);
  1067. PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, name)
  1068. PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, full_name)
  1069. PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, service, const ServiceDescriptor*)
  1070. PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, input_type, const Descriptor*)
  1071. PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, output_type, const Descriptor*)
  1072. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(MethodDescriptor, MethodOptions);
  1073. PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, name)
  1074. PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, package)
  1075. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, pool, const DescriptorPool*)
  1076. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, dependency_count, int)
  1077. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, message_type_count, int)
  1078. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, enum_type_count, int)
  1079. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, service_count, int)
  1080. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, extension_count, int)
  1081. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FileDescriptor, FileOptions);
  1082. PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, message_type, const Descriptor*)
  1083. PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, enum_type, const EnumDescriptor*)
  1084. PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, service,
  1085. const ServiceDescriptor*)
  1086. PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, extension,
  1087. const FieldDescriptor*)
  1088. #undef PROTOBUF_DEFINE_ACCESSOR
  1089. #undef PROTOBUF_DEFINE_STRING_ACCESSOR
  1090. #undef PROTOBUF_DEFINE_ARRAY_ACCESSOR
  1091. // A few accessors differ from the macros...
  1092. inline bool FieldDescriptor::is_required() const {
  1093. return label() == LABEL_REQUIRED;
  1094. }
  1095. inline bool FieldDescriptor::is_optional() const {
  1096. return label() == LABEL_OPTIONAL;
  1097. }
  1098. inline bool FieldDescriptor::is_repeated() const {
  1099. return label() == LABEL_REPEATED;
  1100. }
  1101. inline bool FieldDescriptor::is_packable() const {
  1102. return is_repeated() && IsTypePackable(type());
  1103. }
  1104. // To save space, index() is computed by looking at the descriptor's position
  1105. // in the parent's array of children.
  1106. inline int FieldDescriptor::index() const {
  1107. if (!is_extension_) {
  1108. return this - containing_type_->fields_;
  1109. } else if (extension_scope_ != NULL) {
  1110. return this - extension_scope_->extensions_;
  1111. } else {
  1112. return this - file_->extensions_;
  1113. }
  1114. }
  1115. inline int Descriptor::index() const {
  1116. if (containing_type_ == NULL) {
  1117. return this - file_->message_types_;
  1118. } else {
  1119. return this - containing_type_->nested_types_;
  1120. }
  1121. }
  1122. inline int EnumDescriptor::index() const {
  1123. if (containing_type_ == NULL) {
  1124. return this - file_->enum_types_;
  1125. } else {
  1126. return this - containing_type_->enum_types_;
  1127. }
  1128. }
  1129. inline int EnumValueDescriptor::index() const {
  1130. return this - type_->values_;
  1131. }
  1132. inline int ServiceDescriptor::index() const {
  1133. return this - file_->services_;
  1134. }
  1135. inline int MethodDescriptor::index() const {
  1136. return this - service_->methods_;
  1137. }
  1138. inline FieldDescriptor::CppType FieldDescriptor::cpp_type() const {
  1139. return kTypeToCppTypeMap[type_];
  1140. }
  1141. inline FieldDescriptor::CppType FieldDescriptor::TypeToCppType(Type type) {
  1142. return kTypeToCppTypeMap[type];
  1143. }
  1144. inline bool FieldDescriptor::IsTypePackable(Type field_type) {
  1145. return (field_type != FieldDescriptor::TYPE_STRING &&
  1146. field_type != FieldDescriptor::TYPE_GROUP &&
  1147. field_type != FieldDescriptor::TYPE_MESSAGE &&
  1148. field_type != FieldDescriptor::TYPE_BYTES);
  1149. }
  1150. inline const FileDescriptor* FileDescriptor::dependency(int index) const {
  1151. return dependencies_[index];
  1152. }
  1153. } // namespace protobuf
  1154. } // namespace google
  1155. #endif // GOOGLE_PROTOBUF_DESCRIPTOR_H__