descriptor.h 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // This 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 <set>
  55. #include <string>
  56. #include <vector>
  57. #include <google/protobuf/stubs/common.h>
  58. namespace google {
  59. namespace protobuf {
  60. // Defined in this file.
  61. class Descriptor;
  62. class FieldDescriptor;
  63. class OneofDescriptor;
  64. class EnumDescriptor;
  65. class EnumValueDescriptor;
  66. class ServiceDescriptor;
  67. class MethodDescriptor;
  68. class FileDescriptor;
  69. class DescriptorDatabase;
  70. class DescriptorPool;
  71. // Defined in descriptor.proto
  72. class DescriptorProto;
  73. class FieldDescriptorProto;
  74. class OneofDescriptorProto;
  75. class EnumDescriptorProto;
  76. class EnumValueDescriptorProto;
  77. class ServiceDescriptorProto;
  78. class MethodDescriptorProto;
  79. class FileDescriptorProto;
  80. class MessageOptions;
  81. class FieldOptions;
  82. class EnumOptions;
  83. class EnumValueOptions;
  84. class ServiceOptions;
  85. class MethodOptions;
  86. class FileOptions;
  87. class UninterpretedOption;
  88. class SourceCodeInfo;
  89. // Defined in message.h
  90. class Message;
  91. // Defined in descriptor.cc
  92. class DescriptorBuilder;
  93. class FileDescriptorTables;
  94. // Defined in unknown_field_set.h.
  95. class UnknownField;
  96. // NB, all indices are zero-based.
  97. struct SourceLocation {
  98. int start_line;
  99. int end_line;
  100. int start_column;
  101. int end_column;
  102. // Doc comments found at the source location.
  103. // TODO(kenton): Maybe this struct should have been named SourceInfo or
  104. // something instead. Oh well.
  105. string leading_comments;
  106. string trailing_comments;
  107. };
  108. // Describes a type of protocol message, or a particular group within a
  109. // message. To obtain the Descriptor for a given message object, call
  110. // Message::GetDescriptor(). Generated message classes also have a
  111. // static method called descriptor() which returns the type's descriptor.
  112. // Use DescriptorPool to construct your own descriptors.
  113. class LIBPROTOBUF_EXPORT Descriptor {
  114. public:
  115. // The name of the message type, not including its scope.
  116. const string& name() const;
  117. // The fully-qualified name of the message type, scope delimited by
  118. // periods. For example, message type "Foo" which is declared in package
  119. // "bar" has full name "bar.Foo". If a type "Baz" is nested within
  120. // Foo, Baz's full_name is "bar.Foo.Baz". To get only the part that
  121. // comes after the last '.', use name().
  122. const string& full_name() const;
  123. // Index of this descriptor within the file or containing type's message
  124. // type array.
  125. int index() const;
  126. // The .proto file in which this message type was defined. Never NULL.
  127. const FileDescriptor* file() const;
  128. // If this Descriptor describes a nested type, this returns the type
  129. // in which it is nested. Otherwise, returns NULL.
  130. const Descriptor* containing_type() const;
  131. // Get options for this message type. These are specified in the .proto file
  132. // by placing lines like "option foo = 1234;" in the message definition.
  133. // Allowed options are defined by MessageOptions in
  134. // google/protobuf/descriptor.proto, and any available extensions of that
  135. // message.
  136. const MessageOptions& options() const;
  137. // Write the contents of this Descriptor into the given DescriptorProto.
  138. // The target DescriptorProto must be clear before calling this; if it
  139. // isn't, the result may be garbage.
  140. void CopyTo(DescriptorProto* proto) const;
  141. // Write the contents of this decriptor in a human-readable form. Output
  142. // will be suitable for re-parsing.
  143. string DebugString() const;
  144. // Returns true if this is a placeholder for an unknown type. This will
  145. // only be the case if this descriptor comes from a DescriptorPool
  146. // with AllowUnknownDependencies() set.
  147. bool is_placeholder() const;
  148. // Field stuff -----------------------------------------------------
  149. // The number of fields in this message type.
  150. int field_count() const;
  151. // Gets a field by index, where 0 <= index < field_count().
  152. // These are returned in the order they were defined in the .proto file.
  153. const FieldDescriptor* field(int index) const;
  154. // Looks up a field by declared tag number. Returns NULL if no such field
  155. // exists.
  156. const FieldDescriptor* FindFieldByNumber(int number) const;
  157. // Looks up a field by name. Returns NULL if no such field exists.
  158. const FieldDescriptor* FindFieldByName(const string& name) const;
  159. // Looks up a field by lowercased name (as returned by lowercase_name()).
  160. // This lookup may be ambiguous if multiple field names differ only by case,
  161. // in which case the field returned is chosen arbitrarily from the matches.
  162. const FieldDescriptor* FindFieldByLowercaseName(
  163. const string& lowercase_name) const;
  164. // Looks up a field by camel-case name (as returned by camelcase_name()).
  165. // This lookup may be ambiguous if multiple field names differ in a way that
  166. // leads them to have identical camel-case names, in which case the field
  167. // returned is chosen arbitrarily from the matches.
  168. const FieldDescriptor* FindFieldByCamelcaseName(
  169. const string& camelcase_name) const;
  170. // The number of oneofs in this message type.
  171. int oneof_decl_count() const;
  172. // Get a oneof by index, where 0 <= index < oneof_decl_count().
  173. // These are returned in the order they were defined in the .proto file.
  174. const OneofDescriptor* oneof_decl(int index) const;
  175. // Looks up a oneof by name. Returns NULL if no such oneof exists.
  176. const OneofDescriptor* FindOneofByName(const string& name) const;
  177. // Nested type stuff -----------------------------------------------
  178. // The number of nested types in this message type.
  179. int nested_type_count() const;
  180. // Gets a nested type by index, where 0 <= index < nested_type_count().
  181. // These are returned in the order they were defined in the .proto file.
  182. const Descriptor* nested_type(int index) const;
  183. // Looks up a nested type by name. Returns NULL if no such nested type
  184. // exists.
  185. const Descriptor* FindNestedTypeByName(const string& name) const;
  186. // Enum stuff ------------------------------------------------------
  187. // The number of enum types in this message type.
  188. int enum_type_count() const;
  189. // Gets an enum type by index, where 0 <= index < enum_type_count().
  190. // These are returned in the order they were defined in the .proto file.
  191. const EnumDescriptor* enum_type(int index) const;
  192. // Looks up an enum type by name. Returns NULL if no such enum type exists.
  193. const EnumDescriptor* FindEnumTypeByName(const string& name) const;
  194. // Looks up an enum value by name, among all enum types in this message.
  195. // Returns NULL if no such value exists.
  196. const EnumValueDescriptor* FindEnumValueByName(const string& name) const;
  197. // Extensions ------------------------------------------------------
  198. // A range of field numbers which are designated for third-party
  199. // extensions.
  200. struct ExtensionRange {
  201. int start; // inclusive
  202. int end; // exclusive
  203. };
  204. // The number of extension ranges in this message type.
  205. int extension_range_count() const;
  206. // Gets an extension range by index, where 0 <= index <
  207. // extension_range_count(). These are returned in the order they were defined
  208. // in the .proto file.
  209. const ExtensionRange* extension_range(int index) const;
  210. // Returns true if the number is in one of the extension ranges.
  211. bool IsExtensionNumber(int number) const;
  212. // Returns NULL if no extension range contains the given number.
  213. const ExtensionRange* FindExtensionRangeContainingNumber(int number) const;
  214. // The number of extensions -- extending *other* messages -- that were
  215. // defined nested within this message type's scope.
  216. int extension_count() const;
  217. // Get an extension by index, where 0 <= index < extension_count().
  218. // These are returned in the order they were defined in the .proto file.
  219. const FieldDescriptor* extension(int index) const;
  220. // Looks up a named extension (which extends some *other* message type)
  221. // defined within this message type's scope.
  222. const FieldDescriptor* FindExtensionByName(const string& name) const;
  223. // Similar to FindFieldByLowercaseName(), but finds extensions defined within
  224. // this message type's scope.
  225. const FieldDescriptor* FindExtensionByLowercaseName(const string& name) const;
  226. // Similar to FindFieldByCamelcaseName(), but finds extensions defined within
  227. // this message type's scope.
  228. const FieldDescriptor* FindExtensionByCamelcaseName(const string& name) const;
  229. // Source Location ---------------------------------------------------
  230. // Updates |*out_location| to the source location of the complete
  231. // extent of this message declaration. Returns false and leaves
  232. // |*out_location| unchanged iff location information was not available.
  233. bool GetSourceLocation(SourceLocation* out_location) const;
  234. private:
  235. typedef MessageOptions OptionsType;
  236. // Internal version of DebugString; controls the level of indenting for
  237. // correct depth
  238. void DebugString(int depth, string *contents) const;
  239. // Walks up the descriptor tree to generate the source location path
  240. // to this descriptor from the file root.
  241. void GetLocationPath(vector<int>* output) const;
  242. const string* name_;
  243. const string* full_name_;
  244. const FileDescriptor* file_;
  245. const Descriptor* containing_type_;
  246. const MessageOptions* options_;
  247. // True if this is a placeholder for an unknown type.
  248. bool is_placeholder_;
  249. // True if this is a placeholder and the type name wasn't fully-qualified.
  250. bool is_unqualified_placeholder_;
  251. int field_count_;
  252. FieldDescriptor* fields_;
  253. int oneof_decl_count_;
  254. OneofDescriptor* oneof_decls_;
  255. int nested_type_count_;
  256. Descriptor* nested_types_;
  257. int enum_type_count_;
  258. EnumDescriptor* enum_types_;
  259. int extension_range_count_;
  260. ExtensionRange* extension_ranges_;
  261. int extension_count_;
  262. FieldDescriptor* extensions_;
  263. // IMPORTANT: If you add a new field, make sure to search for all instances
  264. // of Allocate<Descriptor>() and AllocateArray<Descriptor>() in descriptor.cc
  265. // and update them to initialize the field.
  266. // Must be constructed using DescriptorPool.
  267. Descriptor() {}
  268. friend class DescriptorBuilder;
  269. friend class EnumDescriptor;
  270. friend class FieldDescriptor;
  271. friend class OneofDescriptor;
  272. friend class MethodDescriptor;
  273. friend class FileDescriptor;
  274. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Descriptor);
  275. };
  276. // Describes a single field of a message. To get the descriptor for a given
  277. // field, first get the Descriptor for the message in which it is defined,
  278. // then call Descriptor::FindFieldByName(). To get a FieldDescriptor for
  279. // an extension, do one of the following:
  280. // - Get the Descriptor or FileDescriptor for its containing scope, then
  281. // call Descriptor::FindExtensionByName() or
  282. // FileDescriptor::FindExtensionByName().
  283. // - Given a DescriptorPool, call DescriptorPool::FindExtensionByNumber().
  284. // - Given a Reflection for a message object, call
  285. // Reflection::FindKnownExtensionByName() or
  286. // Reflection::FindKnownExtensionByNumber().
  287. // Use DescriptorPool to construct your own descriptors.
  288. class LIBPROTOBUF_EXPORT FieldDescriptor {
  289. public:
  290. // Identifies a field type. 0 is reserved for errors. The order is weird
  291. // for historical reasons. Types 12 and up are new in proto2.
  292. enum Type {
  293. TYPE_DOUBLE = 1, // double, exactly eight bytes on the wire.
  294. TYPE_FLOAT = 2, // float, exactly four bytes on the wire.
  295. TYPE_INT64 = 3, // int64, varint on the wire. Negative numbers
  296. // take 10 bytes. Use TYPE_SINT64 if negative
  297. // values are likely.
  298. TYPE_UINT64 = 4, // uint64, varint on the wire.
  299. TYPE_INT32 = 5, // int32, varint on the wire. Negative numbers
  300. // take 10 bytes. Use TYPE_SINT32 if negative
  301. // values are likely.
  302. TYPE_FIXED64 = 6, // uint64, exactly eight bytes on the wire.
  303. TYPE_FIXED32 = 7, // uint32, exactly four bytes on the wire.
  304. TYPE_BOOL = 8, // bool, varint on the wire.
  305. TYPE_STRING = 9, // UTF-8 text.
  306. TYPE_GROUP = 10, // Tag-delimited message. Deprecated.
  307. TYPE_MESSAGE = 11, // Length-delimited message.
  308. TYPE_BYTES = 12, // Arbitrary byte array.
  309. TYPE_UINT32 = 13, // uint32, varint on the wire
  310. TYPE_ENUM = 14, // Enum, varint on the wire
  311. TYPE_SFIXED32 = 15, // int32, exactly four bytes on the wire
  312. TYPE_SFIXED64 = 16, // int64, exactly eight bytes on the wire
  313. TYPE_SINT32 = 17, // int32, ZigZag-encoded varint on the wire
  314. TYPE_SINT64 = 18, // int64, ZigZag-encoded varint on the wire
  315. MAX_TYPE = 18, // Constant useful for defining lookup tables
  316. // indexed by Type.
  317. };
  318. // Specifies the C++ data type used to represent the field. There is a
  319. // fixed mapping from Type to CppType where each Type maps to exactly one
  320. // CppType. 0 is reserved for errors.
  321. enum CppType {
  322. CPPTYPE_INT32 = 1, // TYPE_INT32, TYPE_SINT32, TYPE_SFIXED32
  323. CPPTYPE_INT64 = 2, // TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64
  324. CPPTYPE_UINT32 = 3, // TYPE_UINT32, TYPE_FIXED32
  325. CPPTYPE_UINT64 = 4, // TYPE_UINT64, TYPE_FIXED64
  326. CPPTYPE_DOUBLE = 5, // TYPE_DOUBLE
  327. CPPTYPE_FLOAT = 6, // TYPE_FLOAT
  328. CPPTYPE_BOOL = 7, // TYPE_BOOL
  329. CPPTYPE_ENUM = 8, // TYPE_ENUM
  330. CPPTYPE_STRING = 9, // TYPE_STRING, TYPE_BYTES
  331. CPPTYPE_MESSAGE = 10, // TYPE_MESSAGE, TYPE_GROUP
  332. MAX_CPPTYPE = 10, // Constant useful for defining lookup tables
  333. // indexed by CppType.
  334. };
  335. // Identifies whether the field is optional, required, or repeated. 0 is
  336. // reserved for errors.
  337. enum Label {
  338. LABEL_OPTIONAL = 1, // optional
  339. LABEL_REQUIRED = 2, // required
  340. LABEL_REPEATED = 3, // repeated
  341. MAX_LABEL = 3, // Constant useful for defining lookup tables
  342. // indexed by Label.
  343. };
  344. // Valid field numbers are positive integers up to kMaxNumber.
  345. static const int kMaxNumber = (1 << 29) - 1;
  346. // First field number reserved for the protocol buffer library implementation.
  347. // Users may not declare fields that use reserved numbers.
  348. static const int kFirstReservedNumber = 19000;
  349. // Last field number reserved for the protocol buffer library implementation.
  350. // Users may not declare fields that use reserved numbers.
  351. static const int kLastReservedNumber = 19999;
  352. const string& name() const; // Name of this field within the message.
  353. const string& full_name() const; // Fully-qualified name of the field.
  354. const FileDescriptor* file() const;// File in which this field was defined.
  355. bool is_extension() const; // Is this an extension field?
  356. int number() const; // Declared tag number.
  357. // Same as name() except converted to lower-case. This (and especially the
  358. // FindFieldByLowercaseName() method) can be useful when parsing formats
  359. // which prefer to use lowercase naming style. (Although, technically
  360. // field names should be lowercased anyway according to the protobuf style
  361. // guide, so this only makes a difference when dealing with old .proto files
  362. // which do not follow the guide.)
  363. const string& lowercase_name() const;
  364. // Same as name() except converted to camel-case. In this conversion, any
  365. // time an underscore appears in the name, it is removed and the next
  366. // letter is capitalized. Furthermore, the first letter of the name is
  367. // lower-cased. Examples:
  368. // FooBar -> fooBar
  369. // foo_bar -> fooBar
  370. // fooBar -> fooBar
  371. // This (and especially the FindFieldByCamelcaseName() method) can be useful
  372. // when parsing formats which prefer to use camel-case naming style.
  373. const string& camelcase_name() const;
  374. Type type() const; // Declared type of this field.
  375. const char* type_name() const; // Name of the declared type.
  376. CppType cpp_type() const; // C++ type of this field.
  377. const char* cpp_type_name() const; // Name of the C++ type.
  378. Label label() const; // optional/required/repeated
  379. bool is_required() const; // shorthand for label() == LABEL_REQUIRED
  380. bool is_optional() const; // shorthand for label() == LABEL_OPTIONAL
  381. bool is_repeated() const; // shorthand for label() == LABEL_REPEATED
  382. bool is_packable() const; // shorthand for is_repeated() &&
  383. // IsTypePackable(type())
  384. bool is_packed() const; // shorthand for is_packable() &&
  385. // options().packed()
  386. // Index of this field within the message's field array, or the file or
  387. // extension scope's extensions array.
  388. int index() const;
  389. // Does this field have an explicitly-declared default value?
  390. bool has_default_value() const;
  391. // Get the field default value if cpp_type() == CPPTYPE_INT32. If no
  392. // explicit default was defined, the default is 0.
  393. int32 default_value_int32() const;
  394. // Get the field default value if cpp_type() == CPPTYPE_INT64. If no
  395. // explicit default was defined, the default is 0.
  396. int64 default_value_int64() const;
  397. // Get the field default value if cpp_type() == CPPTYPE_UINT32. If no
  398. // explicit default was defined, the default is 0.
  399. uint32 default_value_uint32() const;
  400. // Get the field default value if cpp_type() == CPPTYPE_UINT64. If no
  401. // explicit default was defined, the default is 0.
  402. uint64 default_value_uint64() const;
  403. // Get the field default value if cpp_type() == CPPTYPE_FLOAT. If no
  404. // explicit default was defined, the default is 0.0.
  405. float default_value_float() const;
  406. // Get the field default value if cpp_type() == CPPTYPE_DOUBLE. If no
  407. // explicit default was defined, the default is 0.0.
  408. double default_value_double() const;
  409. // Get the field default value if cpp_type() == CPPTYPE_BOOL. If no
  410. // explicit default was defined, the default is false.
  411. bool default_value_bool() const;
  412. // Get the field default value if cpp_type() == CPPTYPE_ENUM. If no
  413. // explicit default was defined, the default is the first value defined
  414. // in the enum type (all enum types are required to have at least one value).
  415. // This never returns NULL.
  416. const EnumValueDescriptor* default_value_enum() const;
  417. // Get the field default value if cpp_type() == CPPTYPE_STRING. If no
  418. // explicit default was defined, the default is the empty string.
  419. const string& default_value_string() const;
  420. // The Descriptor for the message of which this is a field. For extensions,
  421. // this is the extended type. Never NULL.
  422. const Descriptor* containing_type() const;
  423. // If the field is a member of a oneof, this is the one, otherwise this is
  424. // NULL.
  425. const OneofDescriptor* containing_oneof() const;
  426. // If the field is a member of a oneof, returns the index in that oneof.
  427. int index_in_oneof() const;
  428. // An extension may be declared within the scope of another message. If this
  429. // field is an extension (is_extension() is true), then extension_scope()
  430. // returns that message, or NULL if the extension was declared at global
  431. // scope. If this is not an extension, extension_scope() is undefined (may
  432. // assert-fail).
  433. const Descriptor* extension_scope() const;
  434. // If type is TYPE_MESSAGE or TYPE_GROUP, returns a descriptor for the
  435. // message or the group type. Otherwise, returns null.
  436. const Descriptor* message_type() const;
  437. // If type is TYPE_ENUM, returns a descriptor for the enum. Otherwise,
  438. // returns null.
  439. const EnumDescriptor* enum_type() const;
  440. // EXPERIMENTAL; DO NOT USE.
  441. // If this field is a map field, experimental_map_key() is the field
  442. // that is the key for this map.
  443. // experimental_map_key()->containing_type() is the same as message_type().
  444. const FieldDescriptor* experimental_map_key() const;
  445. // Get the FieldOptions for this field. This includes things listed in
  446. // square brackets after the field definition. E.g., the field:
  447. // optional string text = 1 [ctype=CORD];
  448. // has the "ctype" option set. Allowed options are defined by FieldOptions
  449. // in google/protobuf/descriptor.proto, and any available extensions of that
  450. // message.
  451. const FieldOptions& options() const;
  452. // See Descriptor::CopyTo().
  453. void CopyTo(FieldDescriptorProto* proto) const;
  454. // See Descriptor::DebugString().
  455. string DebugString() const;
  456. // Helper method to get the CppType for a particular Type.
  457. static CppType TypeToCppType(Type type);
  458. // Helper method to get the name of a Type.
  459. static const char* TypeName(Type type);
  460. // Helper method to get the name of a CppType.
  461. static const char* CppTypeName(CppType cpp_type);
  462. // Return true iff [packed = true] is valid for fields of this type.
  463. static inline bool IsTypePackable(Type field_type);
  464. // Source Location ---------------------------------------------------
  465. // Updates |*out_location| to the source location of the complete
  466. // extent of this field declaration. Returns false and leaves
  467. // |*out_location| unchanged iff location information was not available.
  468. bool GetSourceLocation(SourceLocation* out_location) const;
  469. private:
  470. typedef FieldOptions OptionsType;
  471. // See Descriptor::DebugString().
  472. enum PrintLabelFlag { PRINT_LABEL, OMIT_LABEL };
  473. void DebugString(int depth, PrintLabelFlag print_label_flag,
  474. string* contents) const;
  475. // formats the default value appropriately and returns it as a string.
  476. // Must have a default value to call this. If quote_string_type is true, then
  477. // types of CPPTYPE_STRING whill be surrounded by quotes and CEscaped.
  478. string DefaultValueAsString(bool quote_string_type) const;
  479. // Walks up the descriptor tree to generate the source location path
  480. // to this descriptor from the file root.
  481. void GetLocationPath(vector<int>* output) const;
  482. const string* name_;
  483. const string* full_name_;
  484. const string* lowercase_name_;
  485. const string* camelcase_name_;
  486. const FileDescriptor* file_;
  487. int number_;
  488. Type type_;
  489. Label label_;
  490. bool is_extension_;
  491. int index_in_oneof_;
  492. const Descriptor* containing_type_;
  493. const OneofDescriptor* containing_oneof_;
  494. const Descriptor* extension_scope_;
  495. const Descriptor* message_type_;
  496. const EnumDescriptor* enum_type_;
  497. const FieldDescriptor* experimental_map_key_;
  498. const FieldOptions* options_;
  499. // IMPORTANT: If you add a new field, make sure to search for all instances
  500. // of Allocate<FieldDescriptor>() and AllocateArray<FieldDescriptor>() in
  501. // descriptor.cc and update them to initialize the field.
  502. bool has_default_value_;
  503. union {
  504. int32 default_value_int32_;
  505. int64 default_value_int64_;
  506. uint32 default_value_uint32_;
  507. uint64 default_value_uint64_;
  508. float default_value_float_;
  509. double default_value_double_;
  510. bool default_value_bool_;
  511. const EnumValueDescriptor* default_value_enum_;
  512. const string* default_value_string_;
  513. };
  514. static const CppType kTypeToCppTypeMap[MAX_TYPE + 1];
  515. static const char * const kTypeToName[MAX_TYPE + 1];
  516. static const char * const kCppTypeToName[MAX_CPPTYPE + 1];
  517. static const char * const kLabelToName[MAX_LABEL + 1];
  518. // Must be constructed using DescriptorPool.
  519. FieldDescriptor() {}
  520. friend class DescriptorBuilder;
  521. friend class FileDescriptor;
  522. friend class Descriptor;
  523. friend class OneofDescriptor;
  524. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldDescriptor);
  525. };
  526. // Describes a oneof defined in a message type.
  527. class LIBPROTOBUF_EXPORT OneofDescriptor {
  528. public:
  529. const string& name() const; // Name of this oneof.
  530. const string& full_name() const; // Fully-qualified name of the oneof.
  531. // Index of this oneof within the message's oneof array.
  532. int index() const;
  533. // The Descriptor for the message containing this oneof.
  534. const Descriptor* containing_type() const;
  535. // The number of (non-extension) fields which are members of this oneof.
  536. int field_count() const;
  537. // Get a member of this oneof, in the order in which they were declared in the
  538. // .proto file. Does not include extensions.
  539. const FieldDescriptor* field(int index) const;
  540. // See Descriptor::CopyTo().
  541. void CopyTo(OneofDescriptorProto* proto) const;
  542. // See Descriptor::DebugString().
  543. string DebugString() const;
  544. // Source Location ---------------------------------------------------
  545. // Updates |*out_location| to the source location of the complete
  546. // extent of this oneof declaration. Returns false and leaves
  547. // |*out_location| unchanged iff location information was not available.
  548. bool GetSourceLocation(SourceLocation* out_location) const;
  549. private:
  550. // See Descriptor::DebugString().
  551. void DebugString(int depth, string* contents) const;
  552. // Walks up the descriptor tree to generate the source location path
  553. // to this descriptor from the file root.
  554. void GetLocationPath(vector<int>* output) const;
  555. const string* name_;
  556. const string* full_name_;
  557. const Descriptor* containing_type_;
  558. bool is_extendable_;
  559. int field_count_;
  560. const FieldDescriptor** fields_;
  561. // IMPORTANT: If you add a new field, make sure to search for all instances
  562. // of Allocate<OneofDescriptor>() and AllocateArray<OneofDescriptor>()
  563. // in descriptor.cc and update them to initialize the field.
  564. // Must be constructed using DescriptorPool.
  565. OneofDescriptor() {}
  566. friend class DescriptorBuilder;
  567. friend class Descriptor;
  568. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(OneofDescriptor);
  569. };
  570. // Describes an enum type defined in a .proto file. To get the EnumDescriptor
  571. // for a generated enum type, call TypeName_descriptor(). Use DescriptorPool
  572. // to construct your own descriptors.
  573. class LIBPROTOBUF_EXPORT EnumDescriptor {
  574. public:
  575. // The name of this enum type in the containing scope.
  576. const string& name() const;
  577. // The fully-qualified name of the enum type, scope delimited by periods.
  578. const string& full_name() const;
  579. // Index of this enum within the file or containing message's enum array.
  580. int index() const;
  581. // The .proto file in which this enum type was defined. Never NULL.
  582. const FileDescriptor* file() const;
  583. // The number of values for this EnumDescriptor. Guaranteed to be greater
  584. // than zero.
  585. int value_count() const;
  586. // Gets a value by index, where 0 <= index < value_count().
  587. // These are returned in the order they were defined in the .proto file.
  588. const EnumValueDescriptor* value(int index) const;
  589. // Looks up a value by name. Returns NULL if no such value exists.
  590. const EnumValueDescriptor* FindValueByName(const string& name) const;
  591. // Looks up a value by number. Returns NULL if no such value exists. If
  592. // multiple values have this number, the first one defined is returned.
  593. const EnumValueDescriptor* FindValueByNumber(int number) const;
  594. // If this enum type is nested in a message type, this is that message type.
  595. // Otherwise, NULL.
  596. const Descriptor* containing_type() const;
  597. // Get options for this enum type. These are specified in the .proto file by
  598. // placing lines like "option foo = 1234;" in the enum definition. Allowed
  599. // options are defined by EnumOptions in google/protobuf/descriptor.proto,
  600. // and any available extensions of that message.
  601. const EnumOptions& options() const;
  602. // See Descriptor::CopyTo().
  603. void CopyTo(EnumDescriptorProto* proto) const;
  604. // See Descriptor::DebugString().
  605. string DebugString() const;
  606. // Returns true if this is a placeholder for an unknown enum. This will
  607. // only be the case if this descriptor comes from a DescriptorPool
  608. // with AllowUnknownDependencies() set.
  609. bool is_placeholder() const;
  610. // Source Location ---------------------------------------------------
  611. // Updates |*out_location| to the source location of the complete
  612. // extent of this enum declaration. Returns false and leaves
  613. // |*out_location| unchanged iff location information was not available.
  614. bool GetSourceLocation(SourceLocation* out_location) const;
  615. private:
  616. typedef EnumOptions OptionsType;
  617. // See Descriptor::DebugString().
  618. void DebugString(int depth, string *contents) const;
  619. // Walks up the descriptor tree to generate the source location path
  620. // to this descriptor from the file root.
  621. void GetLocationPath(vector<int>* output) const;
  622. const string* name_;
  623. const string* full_name_;
  624. const FileDescriptor* file_;
  625. const Descriptor* containing_type_;
  626. const EnumOptions* options_;
  627. // True if this is a placeholder for an unknown type.
  628. bool is_placeholder_;
  629. // True if this is a placeholder and the type name wasn't fully-qualified.
  630. bool is_unqualified_placeholder_;
  631. int value_count_;
  632. EnumValueDescriptor* values_;
  633. // IMPORTANT: If you add a new field, make sure to search for all instances
  634. // of Allocate<EnumDescriptor>() and AllocateArray<EnumDescriptor>() in
  635. // descriptor.cc and update them to initialize the field.
  636. // Must be constructed using DescriptorPool.
  637. EnumDescriptor() {}
  638. friend class DescriptorBuilder;
  639. friend class Descriptor;
  640. friend class FieldDescriptor;
  641. friend class EnumValueDescriptor;
  642. friend class FileDescriptor;
  643. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumDescriptor);
  644. };
  645. // Describes an individual enum constant of a particular type. To get the
  646. // EnumValueDescriptor for a given enum value, first get the EnumDescriptor
  647. // for its type, then use EnumDescriptor::FindValueByName() or
  648. // EnumDescriptor::FindValueByNumber(). Use DescriptorPool to construct
  649. // your own descriptors.
  650. class LIBPROTOBUF_EXPORT EnumValueDescriptor {
  651. public:
  652. const string& name() const; // Name of this enum constant.
  653. int index() const; // Index within the enums's Descriptor.
  654. int number() const; // Numeric value of this enum constant.
  655. // The full_name of an enum value is a sibling symbol of the enum type.
  656. // e.g. the full name of FieldDescriptorProto::TYPE_INT32 is actually
  657. // "google.protobuf.FieldDescriptorProto.TYPE_INT32", NOT
  658. // "google.protobuf.FieldDescriptorProto.Type.TYPE_INT32". This is to conform
  659. // with C++ scoping rules for enums.
  660. const string& full_name() const;
  661. // The type of this value. Never NULL.
  662. const EnumDescriptor* type() const;
  663. // Get options for this enum value. These are specified in the .proto file
  664. // by adding text like "[foo = 1234]" after an enum value definition.
  665. // Allowed options are defined by EnumValueOptions in
  666. // google/protobuf/descriptor.proto, and any available extensions of that
  667. // message.
  668. const EnumValueOptions& options() const;
  669. // See Descriptor::CopyTo().
  670. void CopyTo(EnumValueDescriptorProto* proto) const;
  671. // See Descriptor::DebugString().
  672. string DebugString() const;
  673. // Source Location ---------------------------------------------------
  674. // Updates |*out_location| to the source location of the complete
  675. // extent of this enum value declaration. Returns false and leaves
  676. // |*out_location| unchanged iff location information was not available.
  677. bool GetSourceLocation(SourceLocation* out_location) const;
  678. private:
  679. typedef EnumValueOptions OptionsType;
  680. // See Descriptor::DebugString().
  681. void DebugString(int depth, string *contents) const;
  682. // Walks up the descriptor tree to generate the source location path
  683. // to this descriptor from the file root.
  684. void GetLocationPath(vector<int>* output) const;
  685. const string* name_;
  686. const string* full_name_;
  687. int number_;
  688. const EnumDescriptor* type_;
  689. const EnumValueOptions* options_;
  690. // IMPORTANT: If you add a new field, make sure to search for all instances
  691. // of Allocate<EnumValueDescriptor>() and AllocateArray<EnumValueDescriptor>()
  692. // in descriptor.cc and update them to initialize the field.
  693. // Must be constructed using DescriptorPool.
  694. EnumValueDescriptor() {}
  695. friend class DescriptorBuilder;
  696. friend class EnumDescriptor;
  697. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumValueDescriptor);
  698. };
  699. // Describes an RPC service. To get the ServiceDescriptor for a service,
  700. // call Service::GetDescriptor(). Generated service classes also have a
  701. // static method called descriptor() which returns the type's
  702. // ServiceDescriptor. Use DescriptorPool to construct your own descriptors.
  703. class LIBPROTOBUF_EXPORT ServiceDescriptor {
  704. public:
  705. // The name of the service, not including its containing scope.
  706. const string& name() const;
  707. // The fully-qualified name of the service, scope delimited by periods.
  708. const string& full_name() const;
  709. // Index of this service within the file's services array.
  710. int index() const;
  711. // The .proto file in which this service was defined. Never NULL.
  712. const FileDescriptor* file() const;
  713. // Get options for this service type. These are specified in the .proto file
  714. // by placing lines like "option foo = 1234;" in the service definition.
  715. // Allowed options are defined by ServiceOptions in
  716. // google/protobuf/descriptor.proto, and any available extensions of that
  717. // message.
  718. const ServiceOptions& options() const;
  719. // The number of methods this service defines.
  720. int method_count() const;
  721. // Gets a MethodDescriptor by index, where 0 <= index < method_count().
  722. // These are returned in the order they were defined in the .proto file.
  723. const MethodDescriptor* method(int index) const;
  724. // Look up a MethodDescriptor by name.
  725. const MethodDescriptor* FindMethodByName(const string& name) const;
  726. // See Descriptor::CopyTo().
  727. void CopyTo(ServiceDescriptorProto* proto) const;
  728. // See Descriptor::DebugString().
  729. string DebugString() const;
  730. // Source Location ---------------------------------------------------
  731. // Updates |*out_location| to the source location of the complete
  732. // extent of this service declaration. Returns false and leaves
  733. // |*out_location| unchanged iff location information was not available.
  734. bool GetSourceLocation(SourceLocation* out_location) const;
  735. private:
  736. typedef ServiceOptions OptionsType;
  737. // See Descriptor::DebugString().
  738. void DebugString(string *contents) const;
  739. // Walks up the descriptor tree to generate the source location path
  740. // to this descriptor from the file root.
  741. void GetLocationPath(vector<int>* output) const;
  742. const string* name_;
  743. const string* full_name_;
  744. const FileDescriptor* file_;
  745. const ServiceOptions* options_;
  746. int method_count_;
  747. MethodDescriptor* methods_;
  748. // IMPORTANT: If you add a new field, make sure to search for all instances
  749. // of Allocate<ServiceDescriptor>() and AllocateArray<ServiceDescriptor>() in
  750. // descriptor.cc and update them to initialize the field.
  751. // Must be constructed using DescriptorPool.
  752. ServiceDescriptor() {}
  753. friend class DescriptorBuilder;
  754. friend class FileDescriptor;
  755. friend class MethodDescriptor;
  756. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ServiceDescriptor);
  757. };
  758. // Describes an individual service method. To obtain a MethodDescriptor given
  759. // a service, first get its ServiceDescriptor, then call
  760. // ServiceDescriptor::FindMethodByName(). Use DescriptorPool to construct your
  761. // own descriptors.
  762. class LIBPROTOBUF_EXPORT MethodDescriptor {
  763. public:
  764. // Name of this method, not including containing scope.
  765. const string& name() const;
  766. // The fully-qualified name of the method, scope delimited by periods.
  767. const string& full_name() const;
  768. // Index within the service's Descriptor.
  769. int index() const;
  770. // Gets the service to which this method belongs. Never NULL.
  771. const ServiceDescriptor* service() const;
  772. // Gets the type of protocol message which this method accepts as input.
  773. const Descriptor* input_type() const;
  774. // Gets the type of protocol message which this message produces as output.
  775. const Descriptor* output_type() const;
  776. // Get options for this method. These are specified in the .proto file by
  777. // placing lines like "option foo = 1234;" in curly-braces after a method
  778. // declaration. Allowed options are defined by MethodOptions in
  779. // google/protobuf/descriptor.proto, and any available extensions of that
  780. // message.
  781. const MethodOptions& options() const;
  782. // See Descriptor::CopyTo().
  783. void CopyTo(MethodDescriptorProto* proto) const;
  784. // See Descriptor::DebugString().
  785. string DebugString() const;
  786. // Source Location ---------------------------------------------------
  787. // Updates |*out_location| to the source location of the complete
  788. // extent of this method declaration. Returns false and leaves
  789. // |*out_location| unchanged iff location information was not available.
  790. bool GetSourceLocation(SourceLocation* out_location) const;
  791. private:
  792. typedef MethodOptions OptionsType;
  793. // See Descriptor::DebugString().
  794. void DebugString(int depth, string *contents) const;
  795. // Walks up the descriptor tree to generate the source location path
  796. // to this descriptor from the file root.
  797. void GetLocationPath(vector<int>* output) const;
  798. const string* name_;
  799. const string* full_name_;
  800. const ServiceDescriptor* service_;
  801. const Descriptor* input_type_;
  802. const Descriptor* output_type_;
  803. const MethodOptions* options_;
  804. // IMPORTANT: If you add a new field, make sure to search for all instances
  805. // of Allocate<MethodDescriptor>() and AllocateArray<MethodDescriptor>() in
  806. // descriptor.cc and update them to initialize the field.
  807. // Must be constructed using DescriptorPool.
  808. MethodDescriptor() {}
  809. friend class DescriptorBuilder;
  810. friend class ServiceDescriptor;
  811. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MethodDescriptor);
  812. };
  813. // Describes a whole .proto file. To get the FileDescriptor for a compiled-in
  814. // file, get the descriptor for something defined in that file and call
  815. // descriptor->file(). Use DescriptorPool to construct your own descriptors.
  816. class LIBPROTOBUF_EXPORT FileDescriptor {
  817. public:
  818. // The filename, relative to the source tree.
  819. // e.g. "google/protobuf/descriptor.proto"
  820. const string& name() const;
  821. // The package, e.g. "google.protobuf.compiler".
  822. const string& package() const;
  823. // The DescriptorPool in which this FileDescriptor and all its contents were
  824. // allocated. Never NULL.
  825. const DescriptorPool* pool() const;
  826. // The number of files imported by this one.
  827. int dependency_count() const;
  828. // Gets an imported file by index, where 0 <= index < dependency_count().
  829. // These are returned in the order they were defined in the .proto file.
  830. const FileDescriptor* dependency(int index) const;
  831. // The number of files public imported by this one.
  832. // The public dependency list is a subset of the dependency list.
  833. int public_dependency_count() const;
  834. // Gets a public imported file by index, where 0 <= index <
  835. // public_dependency_count().
  836. // These are returned in the order they were defined in the .proto file.
  837. const FileDescriptor* public_dependency(int index) const;
  838. // The number of files that are imported for weak fields.
  839. // The weak dependency list is a subset of the dependency list.
  840. int weak_dependency_count() const;
  841. // Gets a weak imported file by index, where 0 <= index <
  842. // weak_dependency_count().
  843. // These are returned in the order they were defined in the .proto file.
  844. const FileDescriptor* weak_dependency(int index) const;
  845. // Number of top-level message types defined in this file. (This does not
  846. // include nested types.)
  847. int message_type_count() const;
  848. // Gets a top-level message type, where 0 <= index < message_type_count().
  849. // These are returned in the order they were defined in the .proto file.
  850. const Descriptor* message_type(int index) const;
  851. // Number of top-level enum types defined in this file. (This does not
  852. // include nested types.)
  853. int enum_type_count() const;
  854. // Gets a top-level enum type, where 0 <= index < enum_type_count().
  855. // These are returned in the order they were defined in the .proto file.
  856. const EnumDescriptor* enum_type(int index) const;
  857. // Number of services defined in this file.
  858. int service_count() const;
  859. // Gets a service, where 0 <= index < service_count().
  860. // These are returned in the order they were defined in the .proto file.
  861. const ServiceDescriptor* service(int index) const;
  862. // Number of extensions defined at file scope. (This does not include
  863. // extensions nested within message types.)
  864. int extension_count() const;
  865. // Gets an extension's descriptor, where 0 <= index < extension_count().
  866. // These are returned in the order they were defined in the .proto file.
  867. const FieldDescriptor* extension(int index) const;
  868. // Get options for this file. These are specified in the .proto file by
  869. // placing lines like "option foo = 1234;" at the top level, outside of any
  870. // other definitions. Allowed options are defined by FileOptions in
  871. // google/protobuf/descriptor.proto, and any available extensions of that
  872. // message.
  873. const FileOptions& options() const;
  874. // Find a top-level message type by name. Returns NULL if not found.
  875. const Descriptor* FindMessageTypeByName(const string& name) const;
  876. // Find a top-level enum type by name. Returns NULL if not found.
  877. const EnumDescriptor* FindEnumTypeByName(const string& name) const;
  878. // Find an enum value defined in any top-level enum by name. Returns NULL if
  879. // not found.
  880. const EnumValueDescriptor* FindEnumValueByName(const string& name) const;
  881. // Find a service definition by name. Returns NULL if not found.
  882. const ServiceDescriptor* FindServiceByName(const string& name) const;
  883. // Find a top-level extension definition by name. Returns NULL if not found.
  884. const FieldDescriptor* FindExtensionByName(const string& name) const;
  885. // Similar to FindExtensionByName(), but searches by lowercased-name. See
  886. // Descriptor::FindFieldByLowercaseName().
  887. const FieldDescriptor* FindExtensionByLowercaseName(const string& name) const;
  888. // Similar to FindExtensionByName(), but searches by camelcased-name. See
  889. // Descriptor::FindFieldByCamelcaseName().
  890. const FieldDescriptor* FindExtensionByCamelcaseName(const string& name) const;
  891. // See Descriptor::CopyTo().
  892. // Notes:
  893. // - This method does NOT copy source code information since it is relatively
  894. // large and rarely needed. See CopySourceCodeInfoTo() below.
  895. void CopyTo(FileDescriptorProto* proto) const;
  896. // Write the source code information of this FileDescriptor into the given
  897. // FileDescriptorProto. See CopyTo() above.
  898. void CopySourceCodeInfoTo(FileDescriptorProto* proto) const;
  899. // See Descriptor::DebugString().
  900. string DebugString() const;
  901. // Returns true if this is a placeholder for an unknown file. This will
  902. // only be the case if this descriptor comes from a DescriptorPool
  903. // with AllowUnknownDependencies() set.
  904. bool is_placeholder() const;
  905. private:
  906. // Source Location ---------------------------------------------------
  907. // Updates |*out_location| to the source location of the complete
  908. // extent of the declaration or declaration-part denoted by |path|.
  909. // Returns false and leaves |*out_location| unchanged iff location
  910. // information was not available. (See SourceCodeInfo for
  911. // description of path encoding.)
  912. bool GetSourceLocation(const vector<int>& path,
  913. SourceLocation* out_location) const;
  914. typedef FileOptions OptionsType;
  915. const string* name_;
  916. const string* package_;
  917. const DescriptorPool* pool_;
  918. int dependency_count_;
  919. const FileDescriptor** dependencies_;
  920. int public_dependency_count_;
  921. int* public_dependencies_;
  922. int weak_dependency_count_;
  923. int* weak_dependencies_;
  924. int message_type_count_;
  925. Descriptor* message_types_;
  926. int enum_type_count_;
  927. EnumDescriptor* enum_types_;
  928. int service_count_;
  929. ServiceDescriptor* services_;
  930. int extension_count_;
  931. bool is_placeholder_;
  932. FieldDescriptor* extensions_;
  933. const FileOptions* options_;
  934. const FileDescriptorTables* tables_;
  935. const SourceCodeInfo* source_code_info_;
  936. // IMPORTANT: If you add a new field, make sure to search for all instances
  937. // of Allocate<FileDescriptor>() and AllocateArray<FileDescriptor>() in
  938. // descriptor.cc and update them to initialize the field.
  939. FileDescriptor() {}
  940. friend class DescriptorBuilder;
  941. friend class Descriptor;
  942. friend class FieldDescriptor;
  943. friend class OneofDescriptor;
  944. friend class EnumDescriptor;
  945. friend class EnumValueDescriptor;
  946. friend class MethodDescriptor;
  947. friend class ServiceDescriptor;
  948. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileDescriptor);
  949. };
  950. // ===================================================================
  951. // Used to construct descriptors.
  952. //
  953. // Normally you won't want to build your own descriptors. Message classes
  954. // constructed by the protocol compiler will provide them for you. However,
  955. // if you are implementing Message on your own, or if you are writing a
  956. // program which can operate on totally arbitrary types and needs to load
  957. // them from some sort of database, you might need to.
  958. //
  959. // Since Descriptors are composed of a whole lot of cross-linked bits of
  960. // data that would be a pain to put together manually, the
  961. // DescriptorPool class is provided to make the process easier. It can
  962. // take a FileDescriptorProto (defined in descriptor.proto), validate it,
  963. // and convert it to a set of nicely cross-linked Descriptors.
  964. //
  965. // DescriptorPool also helps with memory management. Descriptors are
  966. // composed of many objects containing static data and pointers to each
  967. // other. In all likelihood, when it comes time to delete this data,
  968. // you'll want to delete it all at once. In fact, it is not uncommon to
  969. // have a whole pool of descriptors all cross-linked with each other which
  970. // you wish to delete all at once. This class represents such a pool, and
  971. // handles the memory management for you.
  972. //
  973. // You can also search for descriptors within a DescriptorPool by name, and
  974. // extensions by number.
  975. class LIBPROTOBUF_EXPORT DescriptorPool {
  976. public:
  977. // Create a normal, empty DescriptorPool.
  978. DescriptorPool();
  979. // Constructs a DescriptorPool that, when it can't find something among the
  980. // descriptors already in the pool, looks for it in the given
  981. // DescriptorDatabase.
  982. // Notes:
  983. // - If a DescriptorPool is constructed this way, its BuildFile*() methods
  984. // must not be called (they will assert-fail). The only way to populate
  985. // the pool with descriptors is to call the Find*By*() methods.
  986. // - The Find*By*() methods may block the calling thread if the
  987. // DescriptorDatabase blocks. This in turn means that parsing messages
  988. // may block if they need to look up extensions.
  989. // - The Find*By*() methods will use mutexes for thread-safety, thus making
  990. // them slower even when they don't have to fall back to the database.
  991. // In fact, even the Find*By*() methods of descriptor objects owned by
  992. // this pool will be slower, since they will have to obtain locks too.
  993. // - An ErrorCollector may optionally be given to collect validation errors
  994. // in files loaded from the database. If not given, errors will be printed
  995. // to GOOGLE_LOG(ERROR). Remember that files are built on-demand, so this
  996. // ErrorCollector may be called from any thread that calls one of the
  997. // Find*By*() methods.
  998. // - The DescriptorDatabase must not be mutated during the lifetime of
  999. // the DescriptorPool. Even if the client takes care to avoid data races,
  1000. // changes to the content of the DescriptorDatabase may not be reflected
  1001. // in subsequent lookups in the DescriptorPool.
  1002. class ErrorCollector;
  1003. explicit DescriptorPool(DescriptorDatabase* fallback_database,
  1004. ErrorCollector* error_collector = NULL);
  1005. ~DescriptorPool();
  1006. // Get a pointer to the generated pool. Generated protocol message classes
  1007. // which are compiled into the binary will allocate their descriptors in
  1008. // this pool. Do not add your own descriptors to this pool.
  1009. static const DescriptorPool* generated_pool();
  1010. // Find a FileDescriptor in the pool by file name. Returns NULL if not
  1011. // found.
  1012. const FileDescriptor* FindFileByName(const string& name) const;
  1013. // Find the FileDescriptor in the pool which defines the given symbol.
  1014. // If any of the Find*ByName() methods below would succeed, then this is
  1015. // equivalent to calling that method and calling the result's file() method.
  1016. // Otherwise this returns NULL.
  1017. const FileDescriptor* FindFileContainingSymbol(
  1018. const string& symbol_name) const;
  1019. // Looking up descriptors ------------------------------------------
  1020. // These find descriptors by fully-qualified name. These will find both
  1021. // top-level descriptors and nested descriptors. They return NULL if not
  1022. // found.
  1023. const Descriptor* FindMessageTypeByName(const string& name) const;
  1024. const FieldDescriptor* FindFieldByName(const string& name) const;
  1025. const FieldDescriptor* FindExtensionByName(const string& name) const;
  1026. const OneofDescriptor* FindOneofByName(const string& name) const;
  1027. const EnumDescriptor* FindEnumTypeByName(const string& name) const;
  1028. const EnumValueDescriptor* FindEnumValueByName(const string& name) const;
  1029. const ServiceDescriptor* FindServiceByName(const string& name) const;
  1030. const MethodDescriptor* FindMethodByName(const string& name) const;
  1031. // Finds an extension of the given type by number. The extendee must be
  1032. // a member of this DescriptorPool or one of its underlays.
  1033. const FieldDescriptor* FindExtensionByNumber(const Descriptor* extendee,
  1034. int number) const;
  1035. // Finds extensions of extendee. The extensions will be appended to
  1036. // out in an undefined order. Only extensions defined directly in
  1037. // this DescriptorPool or one of its underlays are guaranteed to be
  1038. // found: extensions defined in the fallback database might not be found
  1039. // depending on the database implementation.
  1040. void FindAllExtensions(const Descriptor* extendee,
  1041. vector<const FieldDescriptor*>* out) const;
  1042. // Building descriptors --------------------------------------------
  1043. // When converting a FileDescriptorProto to a FileDescriptor, various
  1044. // errors might be detected in the input. The caller may handle these
  1045. // programmatically by implementing an ErrorCollector.
  1046. class LIBPROTOBUF_EXPORT ErrorCollector {
  1047. public:
  1048. inline ErrorCollector() {}
  1049. virtual ~ErrorCollector();
  1050. // These constants specify what exact part of the construct is broken.
  1051. // This is useful e.g. for mapping the error back to an exact location
  1052. // in a .proto file.
  1053. enum ErrorLocation {
  1054. NAME, // the symbol name, or the package name for files
  1055. NUMBER, // field or extension range number
  1056. TYPE, // field type
  1057. EXTENDEE, // field extendee
  1058. DEFAULT_VALUE, // field default value
  1059. INPUT_TYPE, // method input type
  1060. OUTPUT_TYPE, // method output type
  1061. OPTION_NAME, // name in assignment
  1062. OPTION_VALUE, // value in option assignment
  1063. OTHER // some other problem
  1064. };
  1065. // Reports an error in the FileDescriptorProto. Use this function if the
  1066. // problem occured should interrupt building the FileDescriptorProto.
  1067. virtual void AddError(
  1068. const string& filename, // File name in which the error occurred.
  1069. const string& element_name, // Full name of the erroneous element.
  1070. const Message* descriptor, // Descriptor of the erroneous element.
  1071. ErrorLocation location, // One of the location constants, above.
  1072. const string& message // Human-readable error message.
  1073. ) = 0;
  1074. // Reports a warning in the FileDescriptorProto. Use this function if the
  1075. // problem occured should NOT interrupt building the FileDescriptorProto.
  1076. virtual void AddWarning(
  1077. const string& filename, // File name in which the error occurred.
  1078. const string& element_name, // Full name of the erroneous element.
  1079. const Message* descriptor, // Descriptor of the erroneous element.
  1080. ErrorLocation location, // One of the location constants, above.
  1081. const string& message // Human-readable error message.
  1082. ) {}
  1083. private:
  1084. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ErrorCollector);
  1085. };
  1086. // Convert the FileDescriptorProto to real descriptors and place them in
  1087. // this DescriptorPool. All dependencies of the file must already be in
  1088. // the pool. Returns the resulting FileDescriptor, or NULL if there were
  1089. // problems with the input (e.g. the message was invalid, or dependencies
  1090. // were missing). Details about the errors are written to GOOGLE_LOG(ERROR).
  1091. const FileDescriptor* BuildFile(const FileDescriptorProto& proto);
  1092. // Same as BuildFile() except errors are sent to the given ErrorCollector.
  1093. const FileDescriptor* BuildFileCollectingErrors(
  1094. const FileDescriptorProto& proto,
  1095. ErrorCollector* error_collector);
  1096. // By default, it is an error if a FileDescriptorProto contains references
  1097. // to types or other files that are not found in the DescriptorPool (or its
  1098. // backing DescriptorDatabase, if any). If you call
  1099. // AllowUnknownDependencies(), however, then unknown types and files
  1100. // will be replaced by placeholder descriptors (which can be identified by
  1101. // the is_placeholder() method). This can allow you to
  1102. // perform some useful operations with a .proto file even if you do not
  1103. // have access to other .proto files on which it depends. However, some
  1104. // heuristics must be used to fill in the gaps in information, and these
  1105. // can lead to descriptors which are inaccurate. For example, the
  1106. // DescriptorPool may be forced to guess whether an unknown type is a message
  1107. // or an enum, as well as what package it resides in. Furthermore,
  1108. // placeholder types will not be discoverable via FindMessageTypeByName()
  1109. // and similar methods, which could confuse some descriptor-based algorithms.
  1110. // Generally, the results of this option should be handled with extreme care.
  1111. void AllowUnknownDependencies() { allow_unknown_ = true; }
  1112. // By default, weak imports are allowed to be missing, in which case we will
  1113. // use a placeholder for the dependency and convert the field to be an Empty
  1114. // message field. If you call EnforceWeakDependencies(true), however, the
  1115. // DescriptorPool will report a import not found error.
  1116. void EnforceWeakDependencies(bool enforce) { enforce_weak_ = enforce; }
  1117. // Internal stuff --------------------------------------------------
  1118. // These methods MUST NOT be called from outside the proto2 library.
  1119. // These methods may contain hidden pitfalls and may be removed in a
  1120. // future library version.
  1121. // Create a DescriptorPool which is overlaid on top of some other pool.
  1122. // If you search for a descriptor in the overlay and it is not found, the
  1123. // underlay will be searched as a backup. If the underlay has its own
  1124. // underlay, that will be searched next, and so on. This also means that
  1125. // files built in the overlay will be cross-linked with the underlay's
  1126. // descriptors if necessary. The underlay remains property of the caller;
  1127. // it must remain valid for the lifetime of the newly-constructed pool.
  1128. //
  1129. // Example: Say you want to parse a .proto file at runtime in order to use
  1130. // its type with a DynamicMessage. Say this .proto file has dependencies,
  1131. // but you know that all the dependencies will be things that are already
  1132. // compiled into the binary. For ease of use, you'd like to load the types
  1133. // right out of generated_pool() rather than have to parse redundant copies
  1134. // of all these .protos and runtime. But, you don't want to add the parsed
  1135. // types directly into generated_pool(): this is not allowed, and would be
  1136. // bad design anyway. So, instead, you could use generated_pool() as an
  1137. // underlay for a new DescriptorPool in which you add only the new file.
  1138. //
  1139. // WARNING: Use of underlays can lead to many subtle gotchas. Instead,
  1140. // try to formulate what you want to do in terms of DescriptorDatabases.
  1141. explicit DescriptorPool(const DescriptorPool* underlay);
  1142. // Called by generated classes at init time to add their descriptors to
  1143. // generated_pool. Do NOT call this in your own code! filename must be a
  1144. // permanent string (e.g. a string literal).
  1145. static void InternalAddGeneratedFile(
  1146. const void* encoded_file_descriptor, int size);
  1147. // For internal use only: Gets a non-const pointer to the generated pool.
  1148. // This is called at static-initialization time only, so thread-safety is
  1149. // not a concern. If both an underlay and a fallback database are present,
  1150. // the underlay takes precedence.
  1151. static DescriptorPool* internal_generated_pool();
  1152. // For internal use only: Changes the behavior of BuildFile() such that it
  1153. // allows the file to make reference to message types declared in other files
  1154. // which it did not officially declare as dependencies.
  1155. void InternalDontEnforceDependencies();
  1156. // For internal use only.
  1157. void internal_set_underlay(const DescriptorPool* underlay) {
  1158. underlay_ = underlay;
  1159. }
  1160. // For internal (unit test) use only: Returns true if a FileDescriptor has
  1161. // been constructed for the given file, false otherwise. Useful for testing
  1162. // lazy descriptor initialization behavior.
  1163. bool InternalIsFileLoaded(const string& filename) const;
  1164. // Add a file to unused_import_track_files_. DescriptorBuilder will log
  1165. // warnings for those files if there is any unused import.
  1166. void AddUnusedImportTrackFile(const string& file_name);
  1167. void ClearUnusedImportTrackFiles();
  1168. private:
  1169. friend class Descriptor;
  1170. friend class FieldDescriptor;
  1171. friend class EnumDescriptor;
  1172. friend class ServiceDescriptor;
  1173. friend class FileDescriptor;
  1174. friend class DescriptorBuilder;
  1175. // Return true if the given name is a sub-symbol of any non-package
  1176. // descriptor that already exists in the descriptor pool. (The full
  1177. // definition of such types is already known.)
  1178. bool IsSubSymbolOfBuiltType(const string& name) const;
  1179. // Tries to find something in the fallback database and link in the
  1180. // corresponding proto file. Returns true if successful, in which case
  1181. // the caller should search for the thing again. These are declared
  1182. // const because they are called by (semantically) const methods.
  1183. bool TryFindFileInFallbackDatabase(const string& name) const;
  1184. bool TryFindSymbolInFallbackDatabase(const string& name) const;
  1185. bool TryFindExtensionInFallbackDatabase(const Descriptor* containing_type,
  1186. int field_number) const;
  1187. // Like BuildFile() but called internally when the file has been loaded from
  1188. // fallback_database_. Declared const because it is called by (semantically)
  1189. // const methods.
  1190. const FileDescriptor* BuildFileFromDatabase(
  1191. const FileDescriptorProto& proto) const;
  1192. // If fallback_database_ is NULL, this is NULL. Otherwise, this is a mutex
  1193. // which must be locked while accessing tables_.
  1194. Mutex* mutex_;
  1195. // See constructor.
  1196. DescriptorDatabase* fallback_database_;
  1197. ErrorCollector* default_error_collector_;
  1198. const DescriptorPool* underlay_;
  1199. // This class contains a lot of hash maps with complicated types that
  1200. // we'd like to keep out of the header.
  1201. class Tables;
  1202. scoped_ptr<Tables> tables_;
  1203. bool enforce_dependencies_;
  1204. bool allow_unknown_;
  1205. bool enforce_weak_;
  1206. std::set<string> unused_import_track_files_;
  1207. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorPool);
  1208. };
  1209. // inline methods ====================================================
  1210. // These macros makes this repetitive code more readable.
  1211. #define PROTOBUF_DEFINE_ACCESSOR(CLASS, FIELD, TYPE) \
  1212. inline TYPE CLASS::FIELD() const { return FIELD##_; }
  1213. // Strings fields are stored as pointers but returned as const references.
  1214. #define PROTOBUF_DEFINE_STRING_ACCESSOR(CLASS, FIELD) \
  1215. inline const string& CLASS::FIELD() const { return *FIELD##_; }
  1216. // Arrays take an index parameter, obviously.
  1217. #define PROTOBUF_DEFINE_ARRAY_ACCESSOR(CLASS, FIELD, TYPE) \
  1218. inline TYPE CLASS::FIELD(int index) const { return FIELD##s_ + index; }
  1219. #define PROTOBUF_DEFINE_OPTIONS_ACCESSOR(CLASS, TYPE) \
  1220. inline const TYPE& CLASS::options() const { return *options_; }
  1221. PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, name)
  1222. PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, full_name)
  1223. PROTOBUF_DEFINE_ACCESSOR(Descriptor, file, const FileDescriptor*)
  1224. PROTOBUF_DEFINE_ACCESSOR(Descriptor, containing_type, const Descriptor*)
  1225. PROTOBUF_DEFINE_ACCESSOR(Descriptor, field_count, int)
  1226. PROTOBUF_DEFINE_ACCESSOR(Descriptor, oneof_decl_count, int)
  1227. PROTOBUF_DEFINE_ACCESSOR(Descriptor, nested_type_count, int)
  1228. PROTOBUF_DEFINE_ACCESSOR(Descriptor, enum_type_count, int)
  1229. PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, field, const FieldDescriptor*)
  1230. PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, oneof_decl, const OneofDescriptor*)
  1231. PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, nested_type, const Descriptor*)
  1232. PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, enum_type, const EnumDescriptor*)
  1233. PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_range_count, int)
  1234. PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_count, int)
  1235. PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension_range,
  1236. const Descriptor::ExtensionRange*)
  1237. PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension,
  1238. const FieldDescriptor*)
  1239. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(Descriptor, MessageOptions);
  1240. PROTOBUF_DEFINE_ACCESSOR(Descriptor, is_placeholder, bool)
  1241. PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, name)
  1242. PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, full_name)
  1243. PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, lowercase_name)
  1244. PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, camelcase_name)
  1245. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, file, const FileDescriptor*)
  1246. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, number, int)
  1247. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, is_extension, bool)
  1248. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, type, FieldDescriptor::Type)
  1249. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, label, FieldDescriptor::Label)
  1250. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, containing_type, const Descriptor*)
  1251. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, containing_oneof,
  1252. const OneofDescriptor*)
  1253. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, index_in_oneof, int)
  1254. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, extension_scope, const Descriptor*)
  1255. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, message_type, const Descriptor*)
  1256. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, enum_type, const EnumDescriptor*)
  1257. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, experimental_map_key,
  1258. const FieldDescriptor*)
  1259. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FieldDescriptor, FieldOptions)
  1260. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_default_value, bool)
  1261. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int32 , int32 )
  1262. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int64 , int64 )
  1263. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint32, uint32)
  1264. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint64, uint64)
  1265. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_float , float )
  1266. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_double, double)
  1267. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_bool , bool )
  1268. PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_enum,
  1269. const EnumValueDescriptor*)
  1270. PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, default_value_string)
  1271. PROTOBUF_DEFINE_STRING_ACCESSOR(OneofDescriptor, name)
  1272. PROTOBUF_DEFINE_STRING_ACCESSOR(OneofDescriptor, full_name)
  1273. PROTOBUF_DEFINE_ACCESSOR(OneofDescriptor, containing_type, const Descriptor*)
  1274. PROTOBUF_DEFINE_ACCESSOR(OneofDescriptor, field_count, int)
  1275. PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, name)
  1276. PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, full_name)
  1277. PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, file, const FileDescriptor*)
  1278. PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, containing_type, const Descriptor*)
  1279. PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, value_count, int)
  1280. PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, value,
  1281. const EnumValueDescriptor*)
  1282. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumDescriptor, EnumOptions);
  1283. PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, is_placeholder, bool)
  1284. PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, name)
  1285. PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, full_name)
  1286. PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, number, int)
  1287. PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, type, const EnumDescriptor*)
  1288. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumValueDescriptor, EnumValueOptions)
  1289. PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, name)
  1290. PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, full_name)
  1291. PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, file, const FileDescriptor*)
  1292. PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, method_count, int)
  1293. PROTOBUF_DEFINE_ARRAY_ACCESSOR(ServiceDescriptor, method,
  1294. const MethodDescriptor*)
  1295. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(ServiceDescriptor, ServiceOptions);
  1296. PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, name)
  1297. PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, full_name)
  1298. PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, service, const ServiceDescriptor*)
  1299. PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, input_type, const Descriptor*)
  1300. PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, output_type, const Descriptor*)
  1301. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(MethodDescriptor, MethodOptions);
  1302. PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, name)
  1303. PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, package)
  1304. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, pool, const DescriptorPool*)
  1305. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, dependency_count, int)
  1306. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, public_dependency_count, int)
  1307. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, weak_dependency_count, int)
  1308. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, message_type_count, int)
  1309. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, enum_type_count, int)
  1310. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, service_count, int)
  1311. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, extension_count, int)
  1312. PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FileDescriptor, FileOptions);
  1313. PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, is_placeholder, bool)
  1314. PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, message_type, const Descriptor*)
  1315. PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, enum_type, const EnumDescriptor*)
  1316. PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, service,
  1317. const ServiceDescriptor*)
  1318. PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, extension,
  1319. const FieldDescriptor*)
  1320. #undef PROTOBUF_DEFINE_ACCESSOR
  1321. #undef PROTOBUF_DEFINE_STRING_ACCESSOR
  1322. #undef PROTOBUF_DEFINE_ARRAY_ACCESSOR
  1323. // A few accessors differ from the macros...
  1324. inline bool Descriptor::IsExtensionNumber(int number) const {
  1325. return FindExtensionRangeContainingNumber(number) != NULL;
  1326. }
  1327. inline bool FieldDescriptor::is_required() const {
  1328. return label() == LABEL_REQUIRED;
  1329. }
  1330. inline bool FieldDescriptor::is_optional() const {
  1331. return label() == LABEL_OPTIONAL;
  1332. }
  1333. inline bool FieldDescriptor::is_repeated() const {
  1334. return label() == LABEL_REPEATED;
  1335. }
  1336. inline bool FieldDescriptor::is_packable() const {
  1337. return is_repeated() && IsTypePackable(type());
  1338. }
  1339. // To save space, index() is computed by looking at the descriptor's position
  1340. // in the parent's array of children.
  1341. inline int FieldDescriptor::index() const {
  1342. if (!is_extension_) {
  1343. return static_cast<int>(this - containing_type_->fields_);
  1344. } else if (extension_scope_ != NULL) {
  1345. return static_cast<int>(this - extension_scope_->extensions_);
  1346. } else {
  1347. return static_cast<int>(this - file_->extensions_);
  1348. }
  1349. }
  1350. inline int Descriptor::index() const {
  1351. if (containing_type_ == NULL) {
  1352. return static_cast<int>(this - file_->message_types_);
  1353. } else {
  1354. return static_cast<int>(this - containing_type_->nested_types_);
  1355. }
  1356. }
  1357. inline int OneofDescriptor::index() const {
  1358. return static_cast<int>(this - containing_type_->oneof_decls_);
  1359. }
  1360. inline int EnumDescriptor::index() const {
  1361. if (containing_type_ == NULL) {
  1362. return static_cast<int>(this - file_->enum_types_);
  1363. } else {
  1364. return static_cast<int>(this - containing_type_->enum_types_);
  1365. }
  1366. }
  1367. inline int EnumValueDescriptor::index() const {
  1368. return static_cast<int>(this - type_->values_);
  1369. }
  1370. inline int ServiceDescriptor::index() const {
  1371. return static_cast<int>(this - file_->services_);
  1372. }
  1373. inline int MethodDescriptor::index() const {
  1374. return static_cast<int>(this - service_->methods_);
  1375. }
  1376. inline const char* FieldDescriptor::type_name() const {
  1377. return kTypeToName[type_];
  1378. }
  1379. inline FieldDescriptor::CppType FieldDescriptor::cpp_type() const {
  1380. return kTypeToCppTypeMap[type_];
  1381. }
  1382. inline const char* FieldDescriptor::cpp_type_name() const {
  1383. return kCppTypeToName[kTypeToCppTypeMap[type_]];
  1384. }
  1385. inline FieldDescriptor::CppType FieldDescriptor::TypeToCppType(Type type) {
  1386. return kTypeToCppTypeMap[type];
  1387. }
  1388. inline const char* FieldDescriptor::TypeName(Type type) {
  1389. return kTypeToName[type];
  1390. }
  1391. inline const char* FieldDescriptor::CppTypeName(CppType cpp_type) {
  1392. return kCppTypeToName[cpp_type];
  1393. }
  1394. inline bool FieldDescriptor::IsTypePackable(Type field_type) {
  1395. return (field_type != FieldDescriptor::TYPE_STRING &&
  1396. field_type != FieldDescriptor::TYPE_GROUP &&
  1397. field_type != FieldDescriptor::TYPE_MESSAGE &&
  1398. field_type != FieldDescriptor::TYPE_BYTES);
  1399. }
  1400. inline const FileDescriptor* FileDescriptor::dependency(int index) const {
  1401. return dependencies_[index];
  1402. }
  1403. inline const FileDescriptor* FileDescriptor::public_dependency(
  1404. int index) const {
  1405. return dependencies_[public_dependencies_[index]];
  1406. }
  1407. inline const FileDescriptor* FileDescriptor::weak_dependency(
  1408. int index) const {
  1409. return dependencies_[weak_dependencies_[index]];
  1410. }
  1411. // Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because fields_ is actually an array
  1412. // of pointers rather than the usual array of objects.
  1413. inline const FieldDescriptor* OneofDescriptor::field(int index) const {
  1414. return fields_[index];
  1415. }
  1416. } // namespace protobuf
  1417. } // namespace google
  1418. #endif // GOOGLE_PROTOBUF_DESCRIPTOR_H__