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