LCOV - code coverage report
Current view: top level - third_party/protobuf/src/google/protobuf - descriptor.h (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 48 69 69.6 %
Date: 2015-10-10 Functions: 17 25 68.0 %

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

Generated by: LCOV version 1.10