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 (at) 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 62 namespace google { 63 namespace protobuf { 64 65 // Defined in this file. 66 class Descriptor; 67 class FieldDescriptor; 68 class EnumDescriptor; 69 class EnumValueDescriptor; 70 class ServiceDescriptor; 71 class MethodDescriptor; 72 class FileDescriptor; 73 class DescriptorDatabase; 74 class DescriptorPool; 75 76 // Defined in descriptor.proto 77 class DescriptorProto; 78 class FieldDescriptorProto; 79 class EnumDescriptorProto; 80 class EnumValueDescriptorProto; 81 class ServiceDescriptorProto; 82 class MethodDescriptorProto; 83 class FileDescriptorProto; 84 class MessageOptions; 85 class FieldOptions; 86 class EnumOptions; 87 class EnumValueOptions; 88 class ServiceOptions; 89 class MethodOptions; 90 class FileOptions; 91 class UninterpretedOption; 92 93 // Defined in message.h 94 class Message; 95 96 // Defined in descriptor.cc 97 class DescriptorBuilder; 98 class FileDescriptorTables; 99 100 // Defined in unknown_field_set.h. 101 class 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. 108 class 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. 293 class 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 bool is_packable() const; // shorthand for is_repeated() && 399 // IsTypePackable(type()) 400 401 // Index of this field within the message's field array, or the file or 402 // extension scope's extensions array. 403 int index() const; 404 405 // Does this field have an explicitly-declared default value? 406 bool has_default_value() const; 407 408 // Get the field default value if cpp_type() == CPPTYPE_INT32. If no 409 // explicit default was defined, the default is 0. 410 int32 default_value_int32() const; 411 // Get the field default value if cpp_type() == CPPTYPE_INT64. If no 412 // explicit default was defined, the default is 0. 413 int64 default_value_int64() const; 414 // Get the field default value if cpp_type() == CPPTYPE_UINT32. If no 415 // explicit default was defined, the default is 0. 416 uint32 default_value_uint32() const; 417 // Get the field default value if cpp_type() == CPPTYPE_UINT64. If no 418 // explicit default was defined, the default is 0. 419 uint64 default_value_uint64() const; 420 // Get the field default value if cpp_type() == CPPTYPE_FLOAT. If no 421 // explicit default was defined, the default is 0.0. 422 float default_value_float() const; 423 // Get the field default value if cpp_type() == CPPTYPE_DOUBLE. If no 424 // explicit default was defined, the default is 0.0. 425 double default_value_double() const; 426 // Get the field default value if cpp_type() == CPPTYPE_BOOL. If no 427 // explicit default was defined, the default is false. 428 bool default_value_bool() const; 429 // Get the field default value if cpp_type() == CPPTYPE_ENUM. If no 430 // explicit default was defined, the default is the first value defined 431 // in the enum type (all enum types are required to have at least one value). 432 // This never returns NULL. 433 const EnumValueDescriptor* default_value_enum() const; 434 // Get the field default value if cpp_type() == CPPTYPE_STRING. If no 435 // explicit default was defined, the default is the empty string. 436 const string& default_value_string() const; 437 438 // The Descriptor for the message of which this is a field. For extensions, 439 // this is the extended type. Never NULL. 440 const Descriptor* containing_type() const; 441 442 // An extension may be declared within the scope of another message. If this 443 // field is an extension (is_extension() is true), then extension_scope() 444 // returns that message, or NULL if the extension was declared at global 445 // scope. If this is not an extension, extension_scope() is undefined (may 446 // assert-fail). 447 const Descriptor* extension_scope() const; 448 449 // If type is TYPE_MESSAGE or TYPE_GROUP, returns a descriptor for the 450 // message or the group type. Otherwise, undefined. 451 const Descriptor* message_type() const; 452 // If type is TYPE_ENUM, returns a descriptor for the enum. Otherwise, 453 // undefined. 454 const EnumDescriptor* enum_type() const; 455 456 // EXPERIMENTAL; DO NOT USE. 457 // If this field is a map field, experimental_map_key() is the field 458 // that is the key for this map. 459 // experimental_map_key()->containing_type() is the same as message_type(). 460 const FieldDescriptor* experimental_map_key() const; 461 462 // Get the FieldOptions for this field. This includes things listed in 463 // square brackets after the field definition. E.g., the field: 464 // optional string text = 1 [ctype=CORD]; 465 // has the "ctype" option set. Allowed options are defined by FieldOptions 466 // in google/protobuf/descriptor.proto, and any available extensions of that 467 // message. 468 const FieldOptions& options() const; 469 470 // See Descriptor::CopyTo(). 471 void CopyTo(FieldDescriptorProto* proto) const; 472 473 // See Descriptor::DebugString(). 474 string DebugString() const; 475 476 // Helper method to get the CppType for a particular Type. 477 static CppType TypeToCppType(Type type); 478 479 // Return true iff [packed = true] is valid for fields of this type. 480 static inline bool IsTypePackable(Type field_type); 481 482 private: 483 typedef FieldOptions OptionsType; 484 485 // See Descriptor::DebugString(). 486 void DebugString(int depth, string *contents) const; 487 488 // formats the default value appropriately and returns it as a string. 489 // Must have a default value to call this. If quote_string_type is true, then 490 // types of CPPTYPE_STRING whill be surrounded by quotes and CEscaped. 491 string DefaultValueAsString(bool quote_string_type) const; 492 493 const string* name_; 494 const string* full_name_; 495 const string* lowercase_name_; 496 const string* camelcase_name_; 497 const FileDescriptor* file_; 498 int number_; 499 Type type_; 500 Label label_; 501 bool is_extension_; 502 const Descriptor* containing_type_; 503 const Descriptor* extension_scope_; 504 const Descriptor* message_type_; 505 const EnumDescriptor* enum_type_; 506 const FieldDescriptor* experimental_map_key_; 507 const FieldOptions* options_; 508 // IMPORTANT: If you add a new field, make sure to search for all instances 509 // of Allocate<FieldDescriptor>() and AllocateArray<FieldDescriptor>() in 510 // descriptor.cc and update them to initialize the field. 511 512 bool has_default_value_; 513 union { 514 int32 default_value_int32_; 515 int64 default_value_int64_; 516 uint32 default_value_uint32_; 517 uint64 default_value_uint64_; 518 float default_value_float_; 519 double default_value_double_; 520 bool default_value_bool_; 521 522 const EnumValueDescriptor* default_value_enum_; 523 const string* default_value_string_; 524 }; 525 526 static const CppType kTypeToCppTypeMap[MAX_TYPE + 1]; 527 528 static const char * const kTypeToName[MAX_TYPE + 1]; 529 530 static const char * const kLabelToName[MAX_LABEL + 1]; 531 532 // Must be constructed using DescriptorPool. 533 FieldDescriptor() {} 534 friend class DescriptorBuilder; 535 friend class FileDescriptor; 536 friend class Descriptor; 537 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldDescriptor); 538 }; 539 540 // Describes an enum type defined in a .proto file. To get the EnumDescriptor 541 // for a generated enum type, call TypeName_descriptor(). Use DescriptorPool 542 // to construct your own descriptors. 543 class LIBPROTOBUF_EXPORT EnumDescriptor { 544 public: 545 // The name of this enum type in the containing scope. 546 const string& name() const; 547 548 // The fully-qualified name of the enum type, scope delimited by periods. 549 const string& full_name() const; 550 551 // Index of this enum within the file or containing message's enum array. 552 int index() const; 553 554 // The .proto file in which this enum type was defined. Never NULL. 555 const FileDescriptor* file() const; 556 557 // The number of values for this EnumDescriptor. Guaranteed to be greater 558 // than zero. 559 int value_count() const; 560 // Gets a value by index, where 0 <= index < value_count(). 561 // These are returned in the order they were defined in the .proto file. 562 const EnumValueDescriptor* value(int index) const; 563 564 // Looks up a value by name. Returns NULL if no such value exists. 565 const EnumValueDescriptor* FindValueByName(const string& name) const; 566 // Looks up a value by number. Returns NULL if no such value exists. If 567 // multiple values have this number, the first one defined is returned. 568 const EnumValueDescriptor* FindValueByNumber(int number) const; 569 570 // If this enum type is nested in a message type, this is that message type. 571 // Otherwise, NULL. 572 const Descriptor* containing_type() const; 573 574 // Get options for this enum type. These are specified in the .proto file by 575 // placing lines like "option foo = 1234;" in the enum definition. Allowed 576 // options are defined by EnumOptions in google/protobuf/descriptor.proto, 577 // and any available extensions of that message. 578 const EnumOptions& options() const; 579 580 // See Descriptor::CopyTo(). 581 void CopyTo(EnumDescriptorProto* proto) const; 582 583 // See Descriptor::DebugString(). 584 string DebugString() const; 585 586 private: 587 typedef EnumOptions OptionsType; 588 589 // See Descriptor::DebugString(). 590 void DebugString(int depth, string *contents) const; 591 592 const string* name_; 593 const string* full_name_; 594 const FileDescriptor* file_; 595 const Descriptor* containing_type_; 596 const EnumOptions* options_; 597 598 // True if this is a placeholder for an unknown type. 599 bool is_placeholder_; 600 // True if this is a placeholder and the type name wasn't fully-qualified. 601 bool is_unqualified_placeholder_; 602 603 int value_count_; 604 EnumValueDescriptor* values_; 605 // IMPORTANT: If you add a new field, make sure to search for all instances 606 // of Allocate<EnumDescriptor>() and AllocateArray<EnumDescriptor>() in 607 // descriptor.cc and update them to initialize the field. 608 609 // Must be constructed using DescriptorPool. 610 EnumDescriptor() {} 611 friend class DescriptorBuilder; 612 friend class Descriptor; 613 friend class FieldDescriptor; 614 friend class EnumValueDescriptor; 615 friend class FileDescriptor; 616 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumDescriptor); 617 }; 618 619 // Describes an individual enum constant of a particular type. To get the 620 // EnumValueDescriptor for a given enum value, first get the EnumDescriptor 621 // for its type, then use EnumDescriptor::FindValueByName() or 622 // EnumDescriptor::FindValueByNumber(). Use DescriptorPool to construct 623 // your own descriptors. 624 class LIBPROTOBUF_EXPORT EnumValueDescriptor { 625 public: 626 const string& name() const; // Name of this enum constant. 627 int index() const; // Index within the enums's Descriptor. 628 int number() const; // Numeric value of this enum constant. 629 630 // The full_name of an enum value is a sibling symbol of the enum type. 631 // e.g. the full name of FieldDescriptorProto::TYPE_INT32 is actually 632 // "google.protobuf.FieldDescriptorProto.TYPE_INT32", NOT 633 // "google.protobuf.FieldDescriptorProto.Type.TYPE_INT32". This is to conform 634 // with C++ scoping rules for enums. 635 const string& full_name() const; 636 637 // The type of this value. Never NULL. 638 const EnumDescriptor* type() const; 639 640 // Get options for this enum value. These are specified in the .proto file 641 // by adding text like "[foo = 1234]" after an enum value definition. 642 // Allowed options are defined by EnumValueOptions in 643 // google/protobuf/descriptor.proto, and any available extensions of that 644 // message. 645 const EnumValueOptions& options() const; 646 647 // See Descriptor::CopyTo(). 648 void CopyTo(EnumValueDescriptorProto* proto) const; 649 650 // See Descriptor::DebugString(). 651 string DebugString() const; 652 653 private: 654 typedef EnumValueOptions OptionsType; 655 656 // See Descriptor::DebugString(). 657 void DebugString(int depth, string *contents) const; 658 659 const string* name_; 660 const string* full_name_; 661 int number_; 662 const EnumDescriptor* type_; 663 const EnumValueOptions* options_; 664 // IMPORTANT: If you add a new field, make sure to search for all instances 665 // of Allocate<EnumValueDescriptor>() and AllocateArray<EnumValueDescriptor>() 666 // in descriptor.cc and update them to initialize the field. 667 668 // Must be constructed using DescriptorPool. 669 EnumValueDescriptor() {} 670 friend class DescriptorBuilder; 671 friend class EnumDescriptor; 672 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumValueDescriptor); 673 }; 674 675 // Describes an RPC service. To get the ServiceDescriptor for a service, 676 // call Service::GetDescriptor(). Generated service classes also have a 677 // static method called descriptor() which returns the type's 678 // ServiceDescriptor. Use DescriptorPool to construct your own descriptors. 679 class LIBPROTOBUF_EXPORT ServiceDescriptor { 680 public: 681 // The name of the service, not including its containing scope. 682 const string& name() const; 683 // The fully-qualified name of the service, scope delimited by periods. 684 const string& full_name() const; 685 // Index of this service within the file's services array. 686 int index() const; 687 688 // The .proto file in which this service was defined. Never NULL. 689 const FileDescriptor* file() const; 690 691 // Get options for this service type. These are specified in the .proto file 692 // by placing lines like "option foo = 1234;" in the service definition. 693 // Allowed options are defined by ServiceOptions in 694 // google/protobuf/descriptor.proto, and any available extensions of that 695 // message. 696 const ServiceOptions& options() const; 697 698 // The number of methods this service defines. 699 int method_count() const; 700 // Gets a MethodDescriptor by index, where 0 <= index < method_count(). 701 // These are returned in the order they were defined in the .proto file. 702 const MethodDescriptor* method(int index) const; 703 704 // Look up a MethodDescriptor by name. 705 const MethodDescriptor* FindMethodByName(const string& name) const; 706 707 // See Descriptor::CopyTo(). 708 void CopyTo(ServiceDescriptorProto* proto) const; 709 710 // See Descriptor::DebugString(). 711 string DebugString() const; 712 713 private: 714 typedef ServiceOptions OptionsType; 715 716 // See Descriptor::DebugString(). 717 void DebugString(string *contents) const; 718 719 const string* name_; 720 const string* full_name_; 721 const FileDescriptor* file_; 722 const ServiceOptions* options_; 723 int method_count_; 724 MethodDescriptor* methods_; 725 // IMPORTANT: If you add a new field, make sure to search for all instances 726 // of Allocate<ServiceDescriptor>() and AllocateArray<ServiceDescriptor>() in 727 // descriptor.cc and update them to initialize the field. 728 729 // Must be constructed using DescriptorPool. 730 ServiceDescriptor() {} 731 friend class DescriptorBuilder; 732 friend class FileDescriptor; 733 friend class MethodDescriptor; 734 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ServiceDescriptor); 735 }; 736 737 // Describes an individual service method. To obtain a MethodDescriptor given 738 // a service, first get its ServiceDescriptor, then call 739 // ServiceDescriptor::FindMethodByName(). Use DescriptorPool to construct your 740 // own descriptors. 741 class LIBPROTOBUF_EXPORT MethodDescriptor { 742 public: 743 // Name of this method, not including containing scope. 744 const string& name() const; 745 // The fully-qualified name of the method, scope delimited by periods. 746 const string& full_name() const; 747 // Index within the service's Descriptor. 748 int index() const; 749 750 // Gets the service to which this method belongs. Never NULL. 751 const ServiceDescriptor* service() const; 752 753 // Gets the type of protocol message which this method accepts as input. 754 const Descriptor* input_type() const; 755 // Gets the type of protocol message which this message produces as output. 756 const Descriptor* output_type() const; 757 758 // Get options for this method. These are specified in the .proto file by 759 // placing lines like "option foo = 1234;" in curly-braces after a method 760 // declaration. Allowed options are defined by MethodOptions in 761 // google/protobuf/descriptor.proto, and any available extensions of that 762 // message. 763 const MethodOptions& options() const; 764 765 // See Descriptor::CopyTo(). 766 void CopyTo(MethodDescriptorProto* proto) const; 767 768 // See Descriptor::DebugString(). 769 string DebugString() const; 770 771 private: 772 typedef MethodOptions OptionsType; 773 774 // See Descriptor::DebugString(). 775 void DebugString(int depth, string *contents) const; 776 777 const string* name_; 778 const string* full_name_; 779 const ServiceDescriptor* service_; 780 const Descriptor* input_type_; 781 const Descriptor* output_type_; 782 const MethodOptions* options_; 783 // IMPORTANT: If you add a new field, make sure to search for all instances 784 // of Allocate<MethodDescriptor>() and AllocateArray<MethodDescriptor>() in 785 // descriptor.cc and update them to initialize the field. 786 787 // Must be constructed using DescriptorPool. 788 MethodDescriptor() {} 789 friend class DescriptorBuilder; 790 friend class ServiceDescriptor; 791 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MethodDescriptor); 792 }; 793 794 // Describes a whole .proto file. To get the FileDescriptor for a compiled-in 795 // file, get the descriptor for something defined in that file and call 796 // descriptor->file(). Use DescriptorPool to construct your own descriptors. 797 class LIBPROTOBUF_EXPORT FileDescriptor { 798 public: 799 // The filename, relative to the source tree. 800 // e.g. "google/protobuf/descriptor.proto" 801 const string& name() const; 802 803 // The package, e.g. "google.protobuf.compiler". 804 const string& package() const; 805 806 // The DescriptorPool in which this FileDescriptor and all its contents were 807 // allocated. Never NULL. 808 const DescriptorPool* pool() const; 809 810 // The number of files imported by this one. 811 int dependency_count() const; 812 // Gets an imported file by index, where 0 <= index < dependency_count(). 813 // These are returned in the order they were defined in the .proto file. 814 const FileDescriptor* dependency(int index) const; 815 816 // Number of top-level message types defined in this file. (This does not 817 // include nested types.) 818 int message_type_count() const; 819 // Gets a top-level message type, where 0 <= index < message_type_count(). 820 // These are returned in the order they were defined in the .proto file. 821 const Descriptor* message_type(int index) const; 822 823 // Number of top-level enum types defined in this file. (This does not 824 // include nested types.) 825 int enum_type_count() const; 826 // Gets a top-level enum type, where 0 <= index < enum_type_count(). 827 // These are returned in the order they were defined in the .proto file. 828 const EnumDescriptor* enum_type(int index) const; 829 830 // Number of services defined in this file. 831 int service_count() const; 832 // Gets a service, where 0 <= index < service_count(). 833 // These are returned in the order they were defined in the .proto file. 834 const ServiceDescriptor* service(int index) const; 835 836 // Number of extensions defined at file scope. (This does not include 837 // extensions nested within message types.) 838 int extension_count() const; 839 // Gets an extension's descriptor, where 0 <= index < extension_count(). 840 // These are returned in the order they were defined in the .proto file. 841 const FieldDescriptor* extension(int index) const; 842 843 // Get options for this file. These are specified in the .proto file by 844 // placing lines like "option foo = 1234;" at the top level, outside of any 845 // other definitions. Allowed options are defined by FileOptions in 846 // google/protobuf/descriptor.proto, and any available extensions of that 847 // message. 848 const FileOptions& options() const; 849 850 // Find a top-level message type by name. Returns NULL if not found. 851 const Descriptor* FindMessageTypeByName(const string& name) const; 852 // Find a top-level enum type by name. Returns NULL if not found. 853 const EnumDescriptor* FindEnumTypeByName(const string& name) const; 854 // Find an enum value defined in any top-level enum by name. Returns NULL if 855 // not found. 856 const EnumValueDescriptor* FindEnumValueByName(const string& name) const; 857 // Find a service definition by name. Returns NULL if not found. 858 const ServiceDescriptor* FindServiceByName(const string& name) const; 859 // Find a top-level extension definition by name. Returns NULL if not found. 860 const FieldDescriptor* FindExtensionByName(const string& name) const; 861 // Similar to FindExtensionByName(), but searches by lowercased-name. See 862 // Descriptor::FindFieldByLowercaseName(). 863 const FieldDescriptor* FindExtensionByLowercaseName(const string& name) const; 864 // Similar to FindExtensionByName(), but searches by camelcased-name. See 865 // Descriptor::FindFieldByCamelcaseName(). 866 const FieldDescriptor* FindExtensionByCamelcaseName(const string& name) const; 867 868 // See Descriptor::CopyTo(). 869 void CopyTo(FileDescriptorProto* proto) const; 870 871 // See Descriptor::DebugString(). 872 string DebugString() const; 873 874 private: 875 typedef FileOptions OptionsType; 876 877 const string* name_; 878 const string* package_; 879 const DescriptorPool* pool_; 880 int dependency_count_; 881 const FileDescriptor** dependencies_; 882 int message_type_count_; 883 Descriptor* message_types_; 884 int enum_type_count_; 885 EnumDescriptor* enum_types_; 886 int service_count_; 887 ServiceDescriptor* services_; 888 int extension_count_; 889 FieldDescriptor* extensions_; 890 const FileOptions* options_; 891 892 const FileDescriptorTables* tables_; 893 // IMPORTANT: If you add a new field, make sure to search for all instances 894 // of Allocate<FileDescriptor>() and AllocateArray<FileDescriptor>() in 895 // descriptor.cc and update them to initialize the field. 896 897 FileDescriptor() {} 898 friend class DescriptorBuilder; 899 friend class Descriptor; 900 friend class FieldDescriptor; 901 friend class EnumDescriptor; 902 friend class ServiceDescriptor; 903 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileDescriptor); 904 }; 905 906 // =================================================================== 907 908 // Used to construct descriptors. 909 // 910 // Normally you won't want to build your own descriptors. Message classes 911 // constructed by the protocol compiler will provide them for you. However, 912 // if you are implementing Message on your own, or if you are writing a 913 // program which can operate on totally arbitrary types and needs to load 914 // them from some sort of database, you might need to. 915 // 916 // Since Descriptors are composed of a whole lot of cross-linked bits of 917 // data that would be a pain to put together manually, the 918 // DescriptorPool class is provided to make the process easier. It can 919 // take a FileDescriptorProto (defined in descriptor.proto), validate it, 920 // and convert it to a set of nicely cross-linked Descriptors. 921 // 922 // DescriptorPool also helps with memory management. Descriptors are 923 // composed of many objects containing static data and pointers to each 924 // other. In all likelihood, when it comes time to delete this data, 925 // you'll want to delete it all at once. In fact, it is not uncommon to 926 // have a whole pool of descriptors all cross-linked with each other which 927 // you wish to delete all at once. This class represents such a pool, and 928 // handles the memory management for you. 929 // 930 // You can also search for descriptors within a DescriptorPool by name, and 931 // extensions by number. 932 class LIBPROTOBUF_EXPORT DescriptorPool { 933 public: 934 // Create a normal, empty DescriptorPool. 935 DescriptorPool(); 936 937 // Constructs a DescriptorPool that, when it can't find something among the 938 // descriptors already in the pool, looks for it in the given 939 // DescriptorDatabase. 940 // Notes: 941 // - If a DescriptorPool is constructed this way, its BuildFile*() methods 942 // must not be called (they will assert-fail). The only way to populate 943 // the pool with descriptors is to call the Find*By*() methods. 944 // - The Find*By*() methods may block the calling thread if the 945 // DescriptorDatabase blocks. This in turn means that parsing messages 946 // may block if they need to look up extensions. 947 // - The Find*By*() methods will use mutexes for thread-safety, thus making 948 // them slower even when they don't have to fall back to the database. 949 // In fact, even the Find*By*() methods of descriptor objects owned by 950 // this pool will be slower, since they will have to obtain locks too. 951 // - An ErrorCollector may optionally be given to collect validation errors 952 // in files loaded from the database. If not given, errors will be printed 953 // to GOOGLE_LOG(ERROR). Remember that files are built on-demand, so this 954 // ErrorCollector may be called from any thread that calls one of the 955 // Find*By*() methods. 956 class ErrorCollector; 957 explicit DescriptorPool(DescriptorDatabase* fallback_database, 958 ErrorCollector* error_collector = NULL); 959 960 ~DescriptorPool(); 961 962 // Get a pointer to the generated pool. Generated protocol message classes 963 // which are compiled into the binary will allocate their descriptors in 964 // this pool. Do not add your own descriptors to this pool. 965 static const DescriptorPool* generated_pool(); 966 967 // Find a FileDescriptor in the pool by file name. Returns NULL if not 968 // found. 969 const FileDescriptor* FindFileByName(const string& name) const; 970 971 // Find the FileDescriptor in the pool which defines the given symbol. 972 // If any of the Find*ByName() methods below would succeed, then this is 973 // equivalent to calling that method and calling the result's file() method. 974 // Otherwise this returns NULL. 975 const FileDescriptor* FindFileContainingSymbol( 976 const string& symbol_name) const; 977 978 // Looking up descriptors ------------------------------------------ 979 // These find descriptors by fully-qualified name. These will find both 980 // top-level descriptors and nested descriptors. They return NULL if not 981 // found. 982 983 const Descriptor* FindMessageTypeByName(const string& name) const; 984 const FieldDescriptor* FindFieldByName(const string& name) const; 985 const FieldDescriptor* FindExtensionByName(const string& name) const; 986 const EnumDescriptor* FindEnumTypeByName(const string& name) const; 987 const EnumValueDescriptor* FindEnumValueByName(const string& name) const; 988 const ServiceDescriptor* FindServiceByName(const string& name) const; 989 const MethodDescriptor* FindMethodByName(const string& name) const; 990 991 // Finds an extension of the given type by number. The extendee must be 992 // a member of this DescriptorPool or one of its underlays. 993 const FieldDescriptor* FindExtensionByNumber(const Descriptor* extendee, 994 int number) const; 995 996 // Finds extensions of extendee. The extensions will be appended to 997 // out in an undefined order. Only extensions defined directly in 998 // this DescriptorPool or one of its underlays are guaranteed to be 999 // found: extensions defined in the fallback database might not be found 1000 // depending on the database implementation. 1001 void FindAllExtensions(const Descriptor* extendee, 1002 vector<const FieldDescriptor*>* out) const; 1003 1004 // Building descriptors -------------------------------------------- 1005 1006 // When converting a FileDescriptorProto to a FileDescriptor, various 1007 // errors might be detected in the input. The caller may handle these 1008 // programmatically by implementing an ErrorCollector. 1009 class LIBPROTOBUF_EXPORT ErrorCollector { 1010 public: 1011 inline ErrorCollector() {} 1012 virtual ~ErrorCollector(); 1013 1014 // These constants specify what exact part of the construct is broken. 1015 // This is useful e.g. for mapping the error back to an exact location 1016 // in a .proto file. 1017 enum ErrorLocation { 1018 NAME, // the symbol name, or the package name for files 1019 NUMBER, // field or extension range number 1020 TYPE, // field type 1021 EXTENDEE, // field extendee 1022 DEFAULT_VALUE, // field default value 1023 INPUT_TYPE, // method input type 1024 OUTPUT_TYPE, // method output type 1025 OPTION_NAME, // name in assignment 1026 OPTION_VALUE, // value in option assignment 1027 OTHER // some other problem 1028 }; 1029 1030 // Reports an error in the FileDescriptorProto. 1031 virtual void AddError( 1032 const string& filename, // File name in which the error occurred. 1033 const string& element_name, // Full name of the erroneous element. 1034 const Message* descriptor, // Descriptor of the erroneous element. 1035 ErrorLocation location, // One of the location constants, above. 1036 const string& message // Human-readable error message. 1037 ) = 0; 1038 1039 private: 1040 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ErrorCollector); 1041 }; 1042 1043 // Convert the FileDescriptorProto to real descriptors and place them in 1044 // this DescriptorPool. All dependencies of the file must already be in 1045 // the pool. Returns the resulting FileDescriptor, or NULL if there were 1046 // problems with the input (e.g. the message was invalid, or dependencies 1047 // were missing). Details about the errors are written to GOOGLE_LOG(ERROR). 1048 const FileDescriptor* BuildFile(const FileDescriptorProto& proto); 1049 1050 // Same as BuildFile() except errors are sent to the given ErrorCollector. 1051 const FileDescriptor* BuildFileCollectingErrors( 1052 const FileDescriptorProto& proto, 1053 ErrorCollector* error_collector); 1054 1055 // By default, it is an error if a FileDescriptorProto contains references 1056 // to types or other files that are not found in the DescriptorPool (or its 1057 // backing DescriptorDatabase, if any). If you call 1058 // AllowUnknownDependencies(), however, then unknown types and files 1059 // will be replaced by placeholder descriptors. This can allow you to 1060 // perform some useful operations with a .proto file even if you do not 1061 // have access to other .proto files on which it depends. However, some 1062 // heuristics must be used to fill in the gaps in information, and these 1063 // can lead to descriptors which are inaccurate. For example, the 1064 // DescriptorPool may be forced to guess whether an unknown type is a message 1065 // or an enum, as well as what package it resides in. Furthermore, 1066 // placeholder types will not be discoverable via FindMessageTypeByName() 1067 // and similar methods, which could confuse some descriptor-based algorithms. 1068 // Generally, the results of this option should only be relied upon for 1069 // debugging purposes. 1070 void AllowUnknownDependencies() { allow_unknown_ = true; } 1071 1072 // Internal stuff -------------------------------------------------- 1073 // These methods MUST NOT be called from outside the proto2 library. 1074 // These methods may contain hidden pitfalls and may be removed in a 1075 // future library version. 1076 1077 // Create a DescriptorPool which is overlaid on top of some other pool. 1078 // If you search for a descriptor in the overlay and it is not found, the 1079 // underlay will be searched as a backup. If the underlay has its own 1080 // underlay, that will be searched next, and so on. This also means that 1081 // files built in the overlay will be cross-linked with the underlay's 1082 // descriptors if necessary. The underlay remains property of the caller; 1083 // it must remain valid for the lifetime of the newly-constructed pool. 1084 // 1085 // Example: Say you want to parse a .proto file at runtime in order to use 1086 // its type with a DynamicMessage. Say this .proto file has dependencies, 1087 // but you know that all the dependencies will be things that are already 1088 // compiled into the binary. For ease of use, you'd like to load the types 1089 // right out of generated_pool() rather than have to parse redundant copies 1090 // of all these .protos and runtime. But, you don't want to add the parsed 1091 // types directly into generated_pool(): this is not allowed, and would be 1092 // bad design anyway. So, instead, you could use generated_pool() as an 1093 // underlay for a new DescriptorPool in which you add only the new file. 1094 // 1095 // WARNING: Use of underlays can lead to many subtle gotchas. Instead, 1096 // try to formulate what you want to do in terms of DescriptorDatabases. 1097 explicit DescriptorPool(const DescriptorPool* underlay); 1098 1099 // Called by generated classes at init time to add their descriptors to 1100 // generated_pool. Do NOT call this in your own code! filename must be a 1101 // permanent string (e.g. a string literal). 1102 static void InternalAddGeneratedFile( 1103 const void* encoded_file_descriptor, int size); 1104 1105 1106 // For internal use only: Gets a non-const pointer to the generated pool. 1107 // This is called at static-initialization time only, so thread-safety is 1108 // not a concern. If both an underlay and a fallback database are present, 1109 // the fallback database takes precedence. 1110 static DescriptorPool* internal_generated_pool(); 1111 1112 // For internal use only: Changes the behavior of BuildFile() such that it 1113 // allows the file to make reference to message types declared in other files 1114 // which it did not officially declare as dependencies. 1115 void InternalDontEnforceDependencies(); 1116 1117 // For internal use only. 1118 void internal_set_underlay(const DescriptorPool* underlay) { 1119 underlay_ = underlay; 1120 } 1121 1122 // For internal (unit test) use only: Returns true if a FileDescriptor has 1123 // been constructed for the given file, false otherwise. Useful for testing 1124 // lazy descriptor initialization behavior. 1125 bool InternalIsFileLoaded(const string& filename) const; 1126 1127 private: 1128 friend class Descriptor; 1129 friend class FieldDescriptor; 1130 friend class EnumDescriptor; 1131 friend class ServiceDescriptor; 1132 friend class FileDescriptor; 1133 friend class DescriptorBuilder; 1134 1135 // Tries to find something in the fallback database and link in the 1136 // corresponding proto file. Returns true if successful, in which case 1137 // the caller should search for the thing again. These are declared 1138 // const because they are called by (semantically) const methods. 1139 bool TryFindFileInFallbackDatabase(const string& name) const; 1140 bool TryFindSymbolInFallbackDatabase(const string& name) const; 1141 bool TryFindExtensionInFallbackDatabase(const Descriptor* containing_type, 1142 int field_number) const; 1143 1144 // Like BuildFile() but called internally when the file has been loaded from 1145 // fallback_database_. Declared const because it is called by (semantically) 1146 // const methods. 1147 const FileDescriptor* BuildFileFromDatabase( 1148 const FileDescriptorProto& proto) const; 1149 1150 // If fallback_database_ is NULL, this is NULL. Otherwise, this is a mutex 1151 // which must be locked while accessing tables_. 1152 Mutex* mutex_; 1153 1154 // See constructor. 1155 DescriptorDatabase* fallback_database_; 1156 ErrorCollector* default_error_collector_; 1157 const DescriptorPool* underlay_; 1158 1159 // This class contains a lot of hash maps with complicated types that 1160 // we'd like to keep out of the header. 1161 class Tables; 1162 scoped_ptr<Tables> tables_; 1163 1164 bool enforce_dependencies_; 1165 bool allow_unknown_; 1166 1167 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorPool); 1168 }; 1169 1170 // inline methods ==================================================== 1171 1172 // These macros makes this repetitive code more readable. 1173 #define PROTOBUF_DEFINE_ACCESSOR(CLASS, FIELD, TYPE) \ 1174 inline TYPE CLASS::FIELD() const { return FIELD##_; } 1175 1176 // Strings fields are stored as pointers but returned as const references. 1177 #define PROTOBUF_DEFINE_STRING_ACCESSOR(CLASS, FIELD) \ 1178 inline const string& CLASS::FIELD() const { return *FIELD##_; } 1179 1180 // Arrays take an index parameter, obviously. 1181 #define PROTOBUF_DEFINE_ARRAY_ACCESSOR(CLASS, FIELD, TYPE) \ 1182 inline TYPE CLASS::FIELD(int index) const { return FIELD##s_ + index; } 1183 1184 #define PROTOBUF_DEFINE_OPTIONS_ACCESSOR(CLASS, TYPE) \ 1185 inline const TYPE& CLASS::options() const { return *options_; } 1186 1187 PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, name) 1188 PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, full_name) 1189 PROTOBUF_DEFINE_ACCESSOR(Descriptor, file, const FileDescriptor*) 1190 PROTOBUF_DEFINE_ACCESSOR(Descriptor, containing_type, const Descriptor*) 1191 1192 PROTOBUF_DEFINE_ACCESSOR(Descriptor, field_count, int) 1193 PROTOBUF_DEFINE_ACCESSOR(Descriptor, nested_type_count, int) 1194 PROTOBUF_DEFINE_ACCESSOR(Descriptor, enum_type_count, int) 1195 1196 PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, field, const FieldDescriptor*) 1197 PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, nested_type, const Descriptor*) 1198 PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, enum_type, const EnumDescriptor*) 1199 1200 PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_range_count, int) 1201 PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_count, int) 1202 PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension_range, 1203 const Descriptor::ExtensionRange*) 1204 PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension, 1205 const FieldDescriptor*) 1206 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(Descriptor, MessageOptions); 1207 1208 PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, name) 1209 PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, full_name) 1210 PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, lowercase_name) 1211 PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, camelcase_name) 1212 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, file, const FileDescriptor*) 1213 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, number, int) 1214 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, is_extension, bool) 1215 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, type, FieldDescriptor::Type) 1216 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, label, FieldDescriptor::Label) 1217 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, containing_type, const Descriptor*) 1218 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, extension_scope, const Descriptor*) 1219 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, message_type, const Descriptor*) 1220 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, enum_type, const EnumDescriptor*) 1221 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, experimental_map_key, 1222 const FieldDescriptor*) 1223 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FieldDescriptor, FieldOptions); 1224 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_default_value, bool) 1225 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int32 , int32 ) 1226 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int64 , int64 ) 1227 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint32, uint32) 1228 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint64, uint64) 1229 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_float , float ) 1230 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_double, double) 1231 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_bool , bool ) 1232 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_enum, 1233 const EnumValueDescriptor*) 1234 PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, default_value_string) 1235 1236 PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, name) 1237 PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, full_name) 1238 PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, file, const FileDescriptor*) 1239 PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, containing_type, const Descriptor*) 1240 PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, value_count, int) 1241 PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, value, 1242 const EnumValueDescriptor*) 1243 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumDescriptor, EnumOptions); 1244 1245 PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, name) 1246 PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, full_name) 1247 PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, number, int) 1248 PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, type, const EnumDescriptor*) 1249 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumValueDescriptor, EnumValueOptions); 1250 1251 PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, name) 1252 PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, full_name) 1253 PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, file, const FileDescriptor*) 1254 PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, method_count, int) 1255 PROTOBUF_DEFINE_ARRAY_ACCESSOR(ServiceDescriptor, method, 1256 const MethodDescriptor*) 1257 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(ServiceDescriptor, ServiceOptions); 1258 1259 PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, name) 1260 PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, full_name) 1261 PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, service, const ServiceDescriptor*) 1262 PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, input_type, const Descriptor*) 1263 PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, output_type, const Descriptor*) 1264 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(MethodDescriptor, MethodOptions); 1265 1266 PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, name) 1267 PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, package) 1268 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, pool, const DescriptorPool*) 1269 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, dependency_count, int) 1270 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, message_type_count, int) 1271 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, enum_type_count, int) 1272 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, service_count, int) 1273 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, extension_count, int) 1274 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FileDescriptor, FileOptions); 1275 1276 PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, message_type, const Descriptor*) 1277 PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, enum_type, const EnumDescriptor*) 1278 PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, service, 1279 const ServiceDescriptor*) 1280 PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, extension, 1281 const FieldDescriptor*) 1282 1283 #undef PROTOBUF_DEFINE_ACCESSOR 1284 #undef PROTOBUF_DEFINE_STRING_ACCESSOR 1285 #undef PROTOBUF_DEFINE_ARRAY_ACCESSOR 1286 1287 // A few accessors differ from the macros... 1288 1289 inline bool FieldDescriptor::is_required() const { 1290 return label() == LABEL_REQUIRED; 1291 } 1292 1293 inline bool FieldDescriptor::is_optional() const { 1294 return label() == LABEL_OPTIONAL; 1295 } 1296 1297 inline bool FieldDescriptor::is_repeated() const { 1298 return label() == LABEL_REPEATED; 1299 } 1300 1301 inline bool FieldDescriptor::is_packable() const { 1302 return is_repeated() && IsTypePackable(type()); 1303 } 1304 1305 // To save space, index() is computed by looking at the descriptor's position 1306 // in the parent's array of children. 1307 inline int FieldDescriptor::index() const { 1308 if (!is_extension_) { 1309 return this - containing_type_->fields_; 1310 } else if (extension_scope_ != NULL) { 1311 return this - extension_scope_->extensions_; 1312 } else { 1313 return this - file_->extensions_; 1314 } 1315 } 1316 1317 inline int Descriptor::index() const { 1318 if (containing_type_ == NULL) { 1319 return this - file_->message_types_; 1320 } else { 1321 return this - containing_type_->nested_types_; 1322 } 1323 } 1324 1325 inline int EnumDescriptor::index() const { 1326 if (containing_type_ == NULL) { 1327 return this - file_->enum_types_; 1328 } else { 1329 return this - containing_type_->enum_types_; 1330 } 1331 } 1332 1333 inline int EnumValueDescriptor::index() const { 1334 return this - type_->values_; 1335 } 1336 1337 inline int ServiceDescriptor::index() const { 1338 return this - file_->services_; 1339 } 1340 1341 inline int MethodDescriptor::index() const { 1342 return this - service_->methods_; 1343 } 1344 1345 inline FieldDescriptor::CppType FieldDescriptor::cpp_type() const { 1346 return kTypeToCppTypeMap[type_]; 1347 } 1348 1349 inline FieldDescriptor::CppType FieldDescriptor::TypeToCppType(Type type) { 1350 return kTypeToCppTypeMap[type]; 1351 } 1352 1353 inline bool FieldDescriptor::IsTypePackable(Type field_type) { 1354 return (field_type != FieldDescriptor::TYPE_STRING && 1355 field_type != FieldDescriptor::TYPE_GROUP && 1356 field_type != FieldDescriptor::TYPE_MESSAGE && 1357 field_type != FieldDescriptor::TYPE_BYTES); 1358 } 1359 1360 inline const FileDescriptor* FileDescriptor::dependency(int index) const { 1361 return dependencies_[index]; 1362 } 1363 1364 } // namespace protobuf 1365 1366 } // namespace google 1367 #endif // GOOGLE_PROTOBUF_DESCRIPTOR_H__ 1368