descriptor.h 77 KB

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