BWAPI
Undermind/proxy/cpp/include/google/protobuf/descriptor.h
Go to the documentation of this file.
00001 // Protocol Buffers - Google's data interchange format
00002 // Copyright 2008 Google Inc.  All rights reserved.
00003 // http://code.google.com/p/protobuf/
00004 //
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are
00007 // met:
00008 //
00009 //     * Redistributions of source code must retain the above copyright
00010 // notice, this list of conditions and the following disclaimer.
00011 //     * Redistributions in binary form must reproduce the above
00012 // copyright notice, this list of conditions and the following disclaimer
00013 // in the documentation and/or other materials provided with the
00014 // distribution.
00015 //     * Neither the name of Google Inc. nor the names of its
00016 // contributors may be used to endorse or promote products derived from
00017 // this software without specific prior written permission.
00018 //
00019 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00022 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00023 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00025 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00026 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00027 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00029 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 
00031 // Author: kenton@google.com (Kenton Varda)
00032 //  Based on original Protocol Buffers design by
00033 //  Sanjay Ghemawat, Jeff Dean, and others.
00034 //
00035 // This file contains classes which describe a type of protocol message.
00036 // You can use a message's descriptor to learn at runtime what fields
00037 // it contains and what the types of those fields are.  The Message
00038 // interface also allows you to dynamically access and modify individual
00039 // fields by passing the FieldDescriptor of the field you are interested
00040 // in.
00041 //
00042 // Most users will not care about descriptors, because they will write
00043 // code specific to certain protocol types and will simply use the classes
00044 // generated by the protocol compiler directly.  Advanced users who want
00045 // to operate on arbitrary types (not known at compile time) may want to
00046 // read descriptors in order to learn about the contents of a message.
00047 // A very small number of users will want to construct their own
00048 // Descriptors, either because they are implementing Message manually or
00049 // because they are writing something like the protocol compiler.
00050 //
00051 // For an example of how you might use descriptors, see the code example
00052 // at the top of message.h.
00053 
00054 #ifndef GOOGLE_PROTOBUF_DESCRIPTOR_H__
00055 #define GOOGLE_PROTOBUF_DESCRIPTOR_H__
00056 
00057 #include <string>
00058 #include <vector>
00059 #include <google/protobuf/stubs/common.h>
00060 
00061 
00062 namespace google {
00063 namespace protobuf {
00064 
00065 // Defined in this file.
00066 class Descriptor;
00067 class FieldDescriptor;
00068 class EnumDescriptor;
00069 class EnumValueDescriptor;
00070 class ServiceDescriptor;
00071 class MethodDescriptor;
00072 class FileDescriptor;
00073 class DescriptorDatabase;
00074 class DescriptorPool;
00075 
00076 // Defined in descriptor.proto
00077 class DescriptorProto;
00078 class FieldDescriptorProto;
00079 class EnumDescriptorProto;
00080 class EnumValueDescriptorProto;
00081 class ServiceDescriptorProto;
00082 class MethodDescriptorProto;
00083 class FileDescriptorProto;
00084 class MessageOptions;
00085 class FieldOptions;
00086 class EnumOptions;
00087 class EnumValueOptions;
00088 class ServiceOptions;
00089 class MethodOptions;
00090 class FileOptions;
00091 class UninterpretedOption;
00092 
00093 // Defined in message.h
00094 class Message;
00095 
00096 // Defined in descriptor.cc
00097 class DescriptorBuilder;
00098 class FileDescriptorTables;
00099 
00100 // Defined in unknown_field_set.h.
00101 class UnknownField;
00102 
00103 // Describes a type of protocol message, or a particular group within a
00104 // message.  To obtain the Descriptor for a given message object, call
00105 // Message::GetDescriptor().  Generated message classes also have a
00106 // static method called descriptor() which returns the type's descriptor.
00107 // Use DescriptorPool to construct your own descriptors.
00108 class LIBPROTOBUF_EXPORT Descriptor {
00109  public:
00110   // The name of the message type, not including its scope.
00111   const string& name() const;
00112 
00113   // The fully-qualified name of the message type, scope delimited by
00114   // periods.  For example, message type "Foo" which is declared in package
00115   // "bar" has full name "bar.Foo".  If a type "Baz" is nested within
00116   // Foo, Baz's full_name is "bar.Foo.Baz".  To get only the part that
00117   // comes after the last '.', use name().
00118   const string& full_name() const;
00119 
00120   // Index of this descriptor within the file or containing type's message
00121   // type array.
00122   int index() const;
00123 
00124   // The .proto file in which this message type was defined.  Never NULL.
00125   const FileDescriptor* file() const;
00126 
00127   // If this Descriptor describes a nested type, this returns the type
00128   // in which it is nested.  Otherwise, returns NULL.
00129   const Descriptor* containing_type() const;
00130 
00131   // Get options for this message type.  These are specified in the .proto file
00132   // by placing lines like "option foo = 1234;" in the message definition.
00133   // Allowed options are defined by MessageOptions in
00134   // google/protobuf/descriptor.proto, and any available extensions of that
00135   // message.
00136   const MessageOptions& options() const;
00137 
00138   // Write the contents of this Descriptor into the given DescriptorProto.
00139   // The target DescriptorProto must be clear before calling this; if it
00140   // isn't, the result may be garbage.
00141   void CopyTo(DescriptorProto* proto) const;
00142 
00143   // Write the contents of this decriptor in a human-readable form. Output
00144   // will be suitable for re-parsing.
00145   string DebugString() const;
00146 
00147   // Field stuff -----------------------------------------------------
00148 
00149   // The number of fields in this message type.
00150   int field_count() const;
00151   // Gets a field by index, where 0 <= index < field_count().
00152   // These are returned in the order they were defined in the .proto file.
00153   const FieldDescriptor* field(int index) const;
00154 
00155   // Looks up a field by declared tag number.  Returns NULL if no such field
00156   // exists.
00157   const FieldDescriptor* FindFieldByNumber(int number) const;
00158   // Looks up a field by name.  Returns NULL if no such field exists.
00159   const FieldDescriptor* FindFieldByName(const string& name) const;
00160 
00161   // Looks up a field by lowercased name (as returned by lowercase_name()).
00162   // This lookup may be ambiguous if multiple field names differ only by case,
00163   // in which case the field returned is chosen arbitrarily from the matches.
00164   const FieldDescriptor* FindFieldByLowercaseName(
00165       const string& lowercase_name) const;
00166 
00167   // Looks up a field by camel-case name (as returned by camelcase_name()).
00168   // This lookup may be ambiguous if multiple field names differ in a way that
00169   // leads them to have identical camel-case names, in which case the field
00170   // returned is chosen arbitrarily from the matches.
00171   const FieldDescriptor* FindFieldByCamelcaseName(
00172       const string& camelcase_name) const;
00173 
00174   // Nested type stuff -----------------------------------------------
00175 
00176   // The number of nested types in this message type.
00177   int nested_type_count() const;
00178   // Gets a nested type by index, where 0 <= index < nested_type_count().
00179   // These are returned in the order they were defined in the .proto file.
00180   const Descriptor* nested_type(int index) const;
00181 
00182   // Looks up a nested type by name.  Returns NULL if no such nested type
00183   // exists.
00184   const Descriptor* FindNestedTypeByName(const string& name) const;
00185 
00186   // Enum stuff ------------------------------------------------------
00187 
00188   // The number of enum types in this message type.
00189   int enum_type_count() const;
00190   // Gets an enum type by index, where 0 <= index < enum_type_count().
00191   // These are returned in the order they were defined in the .proto file.
00192   const EnumDescriptor* enum_type(int index) const;
00193 
00194   // Looks up an enum type by name.  Returns NULL if no such enum type exists.
00195   const EnumDescriptor* FindEnumTypeByName(const string& name) const;
00196 
00197   // Looks up an enum value by name, among all enum types in this message.
00198   // Returns NULL if no such value exists.
00199   const EnumValueDescriptor* FindEnumValueByName(const string& name) const;
00200 
00201   // Extensions ------------------------------------------------------
00202 
00203   // A range of field numbers which are designated for third-party
00204   // extensions.
00205   struct ExtensionRange {
00206     int start;  // inclusive
00207     int end;    // exclusive
00208   };
00209 
00210   // The number of extension ranges in this message type.
00211   int extension_range_count() const;
00212   // Gets an extension range by index, where 0 <= index <
00213   // extension_range_count(). These are returned in the order they were defined
00214   // in the .proto file.
00215   const ExtensionRange* extension_range(int index) const;
00216 
00217   // Returns true if the number is in one of the extension ranges.
00218   bool IsExtensionNumber(int number) const;
00219 
00220   // The number of extensions -- extending *other* messages -- that were
00221   // defined nested within this message type's scope.
00222   int extension_count() const;
00223   // Get an extension by index, where 0 <= index < extension_count().
00224   // These are returned in the order they were defined in the .proto file.
00225   const FieldDescriptor* extension(int index) const;
00226 
00227   // Looks up a named extension (which extends some *other* message type)
00228   // defined within this message type's scope.
00229   const FieldDescriptor* FindExtensionByName(const string& name) const;
00230 
00231   // Similar to FindFieldByLowercaseName(), but finds extensions defined within
00232   // this message type's scope.
00233   const FieldDescriptor* FindExtensionByLowercaseName(const string& name) const;
00234 
00235   // Similar to FindFieldByCamelcaseName(), but finds extensions defined within
00236   // this message type's scope.
00237   const FieldDescriptor* FindExtensionByCamelcaseName(const string& name) const;
00238 
00239  private:
00240   typedef MessageOptions OptionsType;
00241 
00242   // Internal version of DebugString; controls the level of indenting for
00243   // correct depth
00244   void DebugString(int depth, string *contents) const;
00245 
00246   const string* name_;
00247   const string* full_name_;
00248   const FileDescriptor* file_;
00249   const Descriptor* containing_type_;
00250   const MessageOptions* options_;
00251 
00252   // True if this is a placeholder for an unknown type.
00253   bool is_placeholder_;
00254   // True if this is a placeholder and the type name wasn't fully-qualified.
00255   bool is_unqualified_placeholder_;
00256 
00257   int field_count_;
00258   FieldDescriptor* fields_;
00259   int nested_type_count_;
00260   Descriptor* nested_types_;
00261   int enum_type_count_;
00262   EnumDescriptor* enum_types_;
00263   int extension_range_count_;
00264   ExtensionRange* extension_ranges_;
00265   int extension_count_;
00266   FieldDescriptor* extensions_;
00267   // IMPORTANT:  If you add a new field, make sure to search for all instances
00268   // of Allocate<Descriptor>() and AllocateArray<Descriptor>() in descriptor.cc
00269   // and update them to initialize the field.
00270 
00271   // Must be constructed using DescriptorPool.
00272   Descriptor() {}
00273   friend class DescriptorBuilder;
00274   friend class EnumDescriptor;
00275   friend class FieldDescriptor;
00276   friend class MethodDescriptor;
00277   friend class FileDescriptor;
00278   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Descriptor);
00279 };
00280 
00281 // Describes a single field of a message.  To get the descriptor for a given
00282 // field, first get the Descriptor for the message in which it is defined,
00283 // then call Descriptor::FindFieldByName().  To get a FieldDescriptor for
00284 // an extension, do one of the following:
00285 // - Get the Descriptor or FileDescriptor for its containing scope, then
00286 //   call Descriptor::FindExtensionByName() or
00287 //   FileDescriptor::FindExtensionByName().
00288 // - Given a DescriptorPool, call DescriptorPool::FindExtensionByNumber().
00289 // - Given a Reflection for a message object, call
00290 //   Reflection::FindKnownExtensionByName() or
00291 //   Reflection::FindKnownExtensionByNumber().
00292 // Use DescriptorPool to construct your own descriptors.
00293 class LIBPROTOBUF_EXPORT FieldDescriptor {
00294  public:
00295   // Identifies a field type.  0 is reserved for errors.  The order is weird
00296   // for historical reasons.  Types 12 and up are new in proto2.
00297   enum Type {
00298     TYPE_DOUBLE         = 1,   // double, exactly eight bytes on the wire.
00299     TYPE_FLOAT          = 2,   // float, exactly four bytes on the wire.
00300     TYPE_INT64          = 3,   // int64, varint on the wire.  Negative numbers
00301                                // take 10 bytes.  Use TYPE_SINT64 if negative
00302                                // values are likely.
00303     TYPE_UINT64         = 4,   // uint64, varint on the wire.
00304     TYPE_INT32          = 5,   // int32, varint on the wire.  Negative numbers
00305                                // take 10 bytes.  Use TYPE_SINT32 if negative
00306                                // values are likely.
00307     TYPE_FIXED64        = 6,   // uint64, exactly eight bytes on the wire.
00308     TYPE_FIXED32        = 7,   // uint32, exactly four bytes on the wire.
00309     TYPE_BOOL           = 8,   // bool, varint on the wire.
00310     TYPE_STRING         = 9,   // UTF-8 text.
00311     TYPE_GROUP          = 10,  // Tag-delimited message.  Deprecated.
00312     TYPE_MESSAGE        = 11,  // Length-delimited message.
00313 
00314     TYPE_BYTES          = 12,  // Arbitrary byte array.
00315     TYPE_UINT32         = 13,  // uint32, varint on the wire
00316     TYPE_ENUM           = 14,  // Enum, varint on the wire
00317     TYPE_SFIXED32       = 15,  // int32, exactly four bytes on the wire
00318     TYPE_SFIXED64       = 16,  // int64, exactly eight bytes on the wire
00319     TYPE_SINT32         = 17,  // int32, ZigZag-encoded varint on the wire
00320     TYPE_SINT64         = 18,  // int64, ZigZag-encoded varint on the wire
00321 
00322     MAX_TYPE            = 18,  // Constant useful for defining lookup tables
00323                                // indexed by Type.
00324   };
00325 
00326   // Specifies the C++ data type used to represent the field.  There is a
00327   // fixed mapping from Type to CppType where each Type maps to exactly one
00328   // CppType.  0 is reserved for errors.
00329   enum CppType {
00330     CPPTYPE_INT32       = 1,     // TYPE_INT32, TYPE_SINT32, TYPE_SFIXED32
00331     CPPTYPE_INT64       = 2,     // TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64
00332     CPPTYPE_UINT32      = 3,     // TYPE_UINT32, TYPE_FIXED32
00333     CPPTYPE_UINT64      = 4,     // TYPE_UINT64, TYPE_FIXED64
00334     CPPTYPE_DOUBLE      = 5,     // TYPE_DOUBLE
00335     CPPTYPE_FLOAT       = 6,     // TYPE_FLOAT
00336     CPPTYPE_BOOL        = 7,     // TYPE_BOOL
00337     CPPTYPE_ENUM        = 8,     // TYPE_ENUM
00338     CPPTYPE_STRING      = 9,     // TYPE_STRING, TYPE_BYTES
00339     CPPTYPE_MESSAGE     = 10,    // TYPE_MESSAGE, TYPE_GROUP
00340 
00341     MAX_CPPTYPE         = 10,    // Constant useful for defining lookup tables
00342                                  // indexed by CppType.
00343   };
00344 
00345   // Identifies whether the field is optional, required, or repeated.  0 is
00346   // reserved for errors.
00347   enum Label {
00348     LABEL_OPTIONAL      = 1,    // optional
00349     LABEL_REQUIRED      = 2,    // required
00350     LABEL_REPEATED      = 3,    // repeated
00351 
00352     MAX_LABEL           = 3,    // Constant useful for defining lookup tables
00353                                 // indexed by Label.
00354   };
00355 
00356   // Valid field numbers are positive integers up to kMaxNumber.
00357   static const int kMaxNumber = (1 << 29) - 1;
00358 
00359   // First field number reserved for the protocol buffer library implementation.
00360   // Users may not declare fields that use reserved numbers.
00361   static const int kFirstReservedNumber = 19000;
00362   // Last field number reserved for the protocol buffer library implementation.
00363   // Users may not declare fields that use reserved numbers.
00364   static const int kLastReservedNumber  = 19999;
00365 
00366   const string& name() const;        // Name of this field within the message.
00367   const string& full_name() const;   // Fully-qualified name of the field.
00368   const FileDescriptor* file() const;// File in which this field was defined.
00369   bool is_extension() const;         // Is this an extension field?
00370   int number() const;                // Declared tag number.
00371 
00372   // Same as name() except converted to lower-case.  This (and especially the
00373   // FindFieldByLowercaseName() method) can be useful when parsing formats
00374   // which prefer to use lowercase naming style.  (Although, technically
00375   // field names should be lowercased anyway according to the protobuf style
00376   // guide, so this only makes a difference when dealing with old .proto files
00377   // which do not follow the guide.)
00378   const string& lowercase_name() const;
00379 
00380   // Same as name() except converted to camel-case.  In this conversion, any
00381   // time an underscore appears in the name, it is removed and the next
00382   // letter is capitalized.  Furthermore, the first letter of the name is
00383   // lower-cased.  Examples:
00384   //   FooBar -> fooBar
00385   //   foo_bar -> fooBar
00386   //   fooBar -> fooBar
00387   // This (and especially the FindFieldByCamelcaseName() method) can be useful
00388   // when parsing formats which prefer to use camel-case naming style.
00389   const string& camelcase_name() const;
00390 
00391   Type type() const;                 // Declared type of this field.
00392   CppType cpp_type() const;          // C++ type of this field.
00393   Label label() const;               // optional/required/repeated
00394 
00395   bool is_required() const;      // shorthand for label() == LABEL_REQUIRED
00396   bool is_optional() const;      // shorthand for label() == LABEL_OPTIONAL
00397   bool is_repeated() const;      // shorthand for label() == LABEL_REPEATED
00398   bool is_packable() const;      // shorthand for is_repeated() &&
00399                                  //               IsTypePackable(type())
00400 
00401   // Index of this field within the message's field array, or the file or
00402   // extension scope's extensions array.
00403   int index() const;
00404 
00405   // Does this field have an explicitly-declared default value?
00406   bool has_default_value() const;
00407 
00408   // Get the field default value if cpp_type() == CPPTYPE_INT32.  If no
00409   // explicit default was defined, the default is 0.
00410   int32 default_value_int32() const;
00411   // Get the field default value if cpp_type() == CPPTYPE_INT64.  If no
00412   // explicit default was defined, the default is 0.
00413   int64 default_value_int64() const;
00414   // Get the field default value if cpp_type() == CPPTYPE_UINT32.  If no
00415   // explicit default was defined, the default is 0.
00416   uint32 default_value_uint32() const;
00417   // Get the field default value if cpp_type() == CPPTYPE_UINT64.  If no
00418   // explicit default was defined, the default is 0.
00419   uint64 default_value_uint64() const;
00420   // Get the field default value if cpp_type() == CPPTYPE_FLOAT.  If no
00421   // explicit default was defined, the default is 0.0.
00422   float default_value_float() const;
00423   // Get the field default value if cpp_type() == CPPTYPE_DOUBLE.  If no
00424   // explicit default was defined, the default is 0.0.
00425   double default_value_double() const;
00426   // Get the field default value if cpp_type() == CPPTYPE_BOOL.  If no
00427   // explicit default was defined, the default is false.
00428   bool default_value_bool() const;
00429   // Get the field default value if cpp_type() == CPPTYPE_ENUM.  If no
00430   // explicit default was defined, the default is the first value defined
00431   // in the enum type (all enum types are required to have at least one value).
00432   // This never returns NULL.
00433   const EnumValueDescriptor* default_value_enum() const;
00434   // Get the field default value if cpp_type() == CPPTYPE_STRING.  If no
00435   // explicit default was defined, the default is the empty string.
00436   const string& default_value_string() const;
00437 
00438   // The Descriptor for the message of which this is a field.  For extensions,
00439   // this is the extended type.  Never NULL.
00440   const Descriptor* containing_type() const;
00441 
00442   // An extension may be declared within the scope of another message.  If this
00443   // field is an extension (is_extension() is true), then extension_scope()
00444   // returns that message, or NULL if the extension was declared at global
00445   // scope.  If this is not an extension, extension_scope() is undefined (may
00446   // assert-fail).
00447   const Descriptor* extension_scope() const;
00448 
00449   // If type is TYPE_MESSAGE or TYPE_GROUP, returns a descriptor for the
00450   // message or the group type.  Otherwise, undefined.
00451   const Descriptor* message_type() const;
00452   // If type is TYPE_ENUM, returns a descriptor for the enum.  Otherwise,
00453   // undefined.
00454   const EnumDescriptor* enum_type() const;
00455 
00456   // EXPERIMENTAL; DO NOT USE.
00457   // If this field is a map field, experimental_map_key() is the field
00458   // that is the key for this map.
00459   // experimental_map_key()->containing_type() is the same as message_type().
00460   const FieldDescriptor* experimental_map_key() const;
00461 
00462   // Get the FieldOptions for this field.  This includes things listed in
00463   // square brackets after the field definition.  E.g., the field:
00464   //   optional string text = 1 [ctype=CORD];
00465   // has the "ctype" option set.  Allowed options are defined by FieldOptions
00466   // in google/protobuf/descriptor.proto, and any available extensions of that
00467   // message.
00468   const FieldOptions& options() const;
00469 
00470   // See Descriptor::CopyTo().
00471   void CopyTo(FieldDescriptorProto* proto) const;
00472 
00473   // See Descriptor::DebugString().
00474   string DebugString() const;
00475 
00476   // Helper method to get the CppType for a particular Type.
00477   static CppType TypeToCppType(Type type);
00478 
00479   // Return true iff [packed = true] is valid for fields of this type.
00480   static inline bool IsTypePackable(Type field_type);
00481 
00482  private:
00483   typedef FieldOptions OptionsType;
00484 
00485   // See Descriptor::DebugString().
00486   void DebugString(int depth, string *contents) const;
00487 
00488   // formats the default value appropriately and returns it as a string.
00489   // Must have a default value to call this. If quote_string_type is true, then
00490   // types of CPPTYPE_STRING whill be surrounded by quotes and CEscaped.
00491   string DefaultValueAsString(bool quote_string_type) const;
00492 
00493   const string* name_;
00494   const string* full_name_;
00495   const string* lowercase_name_;
00496   const string* camelcase_name_;
00497   const FileDescriptor* file_;
00498   int number_;
00499   Type type_;
00500   Label label_;
00501   bool is_extension_;
00502   const Descriptor* containing_type_;
00503   const Descriptor* extension_scope_;
00504   const Descriptor* message_type_;
00505   const EnumDescriptor* enum_type_;
00506   const FieldDescriptor* experimental_map_key_;
00507   const FieldOptions* options_;
00508   // IMPORTANT:  If you add a new field, make sure to search for all instances
00509   // of Allocate<FieldDescriptor>() and AllocateArray<FieldDescriptor>() in
00510   // descriptor.cc and update them to initialize the field.
00511 
00512   bool has_default_value_;
00513   union {
00514     int32  default_value_int32_;
00515     int64  default_value_int64_;
00516     uint32 default_value_uint32_;
00517     uint64 default_value_uint64_;
00518     float  default_value_float_;
00519     double default_value_double_;
00520     bool   default_value_bool_;
00521 
00522     const EnumValueDescriptor* default_value_enum_;
00523     const string* default_value_string_;
00524   };
00525 
00526   static const CppType kTypeToCppTypeMap[MAX_TYPE + 1];
00527 
00528   static const char * const kTypeToName[MAX_TYPE + 1];
00529 
00530   static const char * const kLabelToName[MAX_LABEL + 1];
00531 
00532   // Must be constructed using DescriptorPool.
00533   FieldDescriptor() {}
00534   friend class DescriptorBuilder;
00535   friend class FileDescriptor;
00536   friend class Descriptor;
00537   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldDescriptor);
00538 };
00539 
00540 // Describes an enum type defined in a .proto file.  To get the EnumDescriptor
00541 // for a generated enum type, call TypeName_descriptor().  Use DescriptorPool
00542 // to construct your own descriptors.
00543 class LIBPROTOBUF_EXPORT EnumDescriptor {
00544  public:
00545   // The name of this enum type in the containing scope.
00546   const string& name() const;
00547 
00548   // The fully-qualified name of the enum type, scope delimited by periods.
00549   const string& full_name() const;
00550 
00551   // Index of this enum within the file or containing message's enum array.
00552   int index() const;
00553 
00554   // The .proto file in which this enum type was defined.  Never NULL.
00555   const FileDescriptor* file() const;
00556 
00557   // The number of values for this EnumDescriptor.  Guaranteed to be greater
00558   // than zero.
00559   int value_count() const;
00560   // Gets a value by index, where 0 <= index < value_count().
00561   // These are returned in the order they were defined in the .proto file.
00562   const EnumValueDescriptor* value(int index) const;
00563 
00564   // Looks up a value by name.  Returns NULL if no such value exists.
00565   const EnumValueDescriptor* FindValueByName(const string& name) const;
00566   // Looks up a value by number.  Returns NULL if no such value exists.  If
00567   // multiple values have this number, the first one defined is returned.
00568   const EnumValueDescriptor* FindValueByNumber(int number) const;
00569 
00570   // If this enum type is nested in a message type, this is that message type.
00571   // Otherwise, NULL.
00572   const Descriptor* containing_type() const;
00573 
00574   // Get options for this enum type.  These are specified in the .proto file by
00575   // placing lines like "option foo = 1234;" in the enum definition.  Allowed
00576   // options are defined by EnumOptions in google/protobuf/descriptor.proto,
00577   // and any available extensions of that message.
00578   const EnumOptions& options() const;
00579 
00580   // See Descriptor::CopyTo().
00581   void CopyTo(EnumDescriptorProto* proto) const;
00582 
00583   // See Descriptor::DebugString().
00584   string DebugString() const;
00585 
00586  private:
00587   typedef EnumOptions OptionsType;
00588 
00589   // See Descriptor::DebugString().
00590   void DebugString(int depth, string *contents) const;
00591 
00592   const string* name_;
00593   const string* full_name_;
00594   const FileDescriptor* file_;
00595   const Descriptor* containing_type_;
00596   const EnumOptions* options_;
00597 
00598   // True if this is a placeholder for an unknown type.
00599   bool is_placeholder_;
00600   // True if this is a placeholder and the type name wasn't fully-qualified.
00601   bool is_unqualified_placeholder_;
00602 
00603   int value_count_;
00604   EnumValueDescriptor* values_;
00605   // IMPORTANT:  If you add a new field, make sure to search for all instances
00606   // of Allocate<EnumDescriptor>() and AllocateArray<EnumDescriptor>() in
00607   // descriptor.cc and update them to initialize the field.
00608 
00609   // Must be constructed using DescriptorPool.
00610   EnumDescriptor() {}
00611   friend class DescriptorBuilder;
00612   friend class Descriptor;
00613   friend class FieldDescriptor;
00614   friend class EnumValueDescriptor;
00615   friend class FileDescriptor;
00616   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumDescriptor);
00617 };
00618 
00619 // Describes an individual enum constant of a particular type.  To get the
00620 // EnumValueDescriptor for a given enum value, first get the EnumDescriptor
00621 // for its type, then use EnumDescriptor::FindValueByName() or
00622 // EnumDescriptor::FindValueByNumber().  Use DescriptorPool to construct
00623 // your own descriptors.
00624 class LIBPROTOBUF_EXPORT EnumValueDescriptor {
00625  public:
00626   const string& name() const;  // Name of this enum constant.
00627   int index() const;           // Index within the enums's Descriptor.
00628   int number() const;          // Numeric value of this enum constant.
00629 
00630   // The full_name of an enum value is a sibling symbol of the enum type.
00631   // e.g. the full name of FieldDescriptorProto::TYPE_INT32 is actually
00632   // "google.protobuf.FieldDescriptorProto.TYPE_INT32", NOT
00633   // "google.protobuf.FieldDescriptorProto.Type.TYPE_INT32".  This is to conform
00634   // with C++ scoping rules for enums.
00635   const string& full_name() const;
00636 
00637   // The type of this value.  Never NULL.
00638   const EnumDescriptor* type() const;
00639 
00640   // Get options for this enum value.  These are specified in the .proto file
00641   // by adding text like "[foo = 1234]" after an enum value definition.
00642   // Allowed options are defined by EnumValueOptions in
00643   // google/protobuf/descriptor.proto, and any available extensions of that
00644   // message.
00645   const EnumValueOptions& options() const;
00646 
00647   // See Descriptor::CopyTo().
00648   void CopyTo(EnumValueDescriptorProto* proto) const;
00649 
00650   // See Descriptor::DebugString().
00651   string DebugString() const;
00652 
00653  private:
00654   typedef EnumValueOptions OptionsType;
00655 
00656   // See Descriptor::DebugString().
00657   void DebugString(int depth, string *contents) const;
00658 
00659   const string* name_;
00660   const string* full_name_;
00661   int number_;
00662   const EnumDescriptor* type_;
00663   const EnumValueOptions* options_;
00664   // IMPORTANT:  If you add a new field, make sure to search for all instances
00665   // of Allocate<EnumValueDescriptor>() and AllocateArray<EnumValueDescriptor>()
00666   // in descriptor.cc and update them to initialize the field.
00667 
00668   // Must be constructed using DescriptorPool.
00669   EnumValueDescriptor() {}
00670   friend class DescriptorBuilder;
00671   friend class EnumDescriptor;
00672   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumValueDescriptor);
00673 };
00674 
00675 // Describes an RPC service.  To get the ServiceDescriptor for a service,
00676 // call Service::GetDescriptor().  Generated service classes also have a
00677 // static method called descriptor() which returns the type's
00678 // ServiceDescriptor.  Use DescriptorPool to construct your own descriptors.
00679 class LIBPROTOBUF_EXPORT ServiceDescriptor {
00680  public:
00681   // The name of the service, not including its containing scope.
00682   const string& name() const;
00683   // The fully-qualified name of the service, scope delimited by periods.
00684   const string& full_name() const;
00685   // Index of this service within the file's services array.
00686   int index() const;
00687 
00688   // The .proto file in which this service was defined.  Never NULL.
00689   const FileDescriptor* file() const;
00690 
00691   // Get options for this service type.  These are specified in the .proto file
00692   // by placing lines like "option foo = 1234;" in the service definition.
00693   // Allowed options are defined by ServiceOptions in
00694   // google/protobuf/descriptor.proto, and any available extensions of that
00695   // message.
00696   const ServiceOptions& options() const;
00697 
00698   // The number of methods this service defines.
00699   int method_count() const;
00700   // Gets a MethodDescriptor by index, where 0 <= index < method_count().
00701   // These are returned in the order they were defined in the .proto file.
00702   const MethodDescriptor* method(int index) const;
00703 
00704   // Look up a MethodDescriptor by name.
00705   const MethodDescriptor* FindMethodByName(const string& name) const;
00706 
00707   // See Descriptor::CopyTo().
00708   void CopyTo(ServiceDescriptorProto* proto) const;
00709 
00710   // See Descriptor::DebugString().
00711   string DebugString() const;
00712 
00713  private:
00714   typedef ServiceOptions OptionsType;
00715 
00716   // See Descriptor::DebugString().
00717   void DebugString(string *contents) const;
00718 
00719   const string* name_;
00720   const string* full_name_;
00721   const FileDescriptor* file_;
00722   const ServiceOptions* options_;
00723   int method_count_;
00724   MethodDescriptor* methods_;
00725   // IMPORTANT:  If you add a new field, make sure to search for all instances
00726   // of Allocate<ServiceDescriptor>() and AllocateArray<ServiceDescriptor>() in
00727   // descriptor.cc and update them to initialize the field.
00728 
00729   // Must be constructed using DescriptorPool.
00730   ServiceDescriptor() {}
00731   friend class DescriptorBuilder;
00732   friend class FileDescriptor;
00733   friend class MethodDescriptor;
00734   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ServiceDescriptor);
00735 };
00736 
00737 // Describes an individual service method.  To obtain a MethodDescriptor given
00738 // a service, first get its ServiceDescriptor, then call
00739 // ServiceDescriptor::FindMethodByName().  Use DescriptorPool to construct your
00740 // own descriptors.
00741 class LIBPROTOBUF_EXPORT MethodDescriptor {
00742  public:
00743   // Name of this method, not including containing scope.
00744   const string& name() const;
00745   // The fully-qualified name of the method, scope delimited by periods.
00746   const string& full_name() const;
00747   // Index within the service's Descriptor.
00748   int index() const;
00749 
00750   // Gets the service to which this method belongs.  Never NULL.
00751   const ServiceDescriptor* service() const;
00752 
00753   // Gets the type of protocol message which this method accepts as input.
00754   const Descriptor* input_type() const;
00755   // Gets the type of protocol message which this message produces as output.
00756   const Descriptor* output_type() const;
00757 
00758   // Get options for this method.  These are specified in the .proto file by
00759   // placing lines like "option foo = 1234;" in curly-braces after a method
00760   // declaration.  Allowed options are defined by MethodOptions in
00761   // google/protobuf/descriptor.proto, and any available extensions of that
00762   // message.
00763   const MethodOptions& options() const;
00764 
00765   // See Descriptor::CopyTo().
00766   void CopyTo(MethodDescriptorProto* proto) const;
00767 
00768   // See Descriptor::DebugString().
00769   string DebugString() const;
00770 
00771  private:
00772   typedef MethodOptions OptionsType;
00773 
00774   // See Descriptor::DebugString().
00775   void DebugString(int depth, string *contents) const;
00776 
00777   const string* name_;
00778   const string* full_name_;
00779   const ServiceDescriptor* service_;
00780   const Descriptor* input_type_;
00781   const Descriptor* output_type_;
00782   const MethodOptions* options_;
00783   // IMPORTANT:  If you add a new field, make sure to search for all instances
00784   // of Allocate<MethodDescriptor>() and AllocateArray<MethodDescriptor>() in
00785   // descriptor.cc and update them to initialize the field.
00786 
00787   // Must be constructed using DescriptorPool.
00788   MethodDescriptor() {}
00789   friend class DescriptorBuilder;
00790   friend class ServiceDescriptor;
00791   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MethodDescriptor);
00792 };
00793 
00794 // Describes a whole .proto file.  To get the FileDescriptor for a compiled-in
00795 // file, get the descriptor for something defined in that file and call
00796 // descriptor->file().  Use DescriptorPool to construct your own descriptors.
00797 class LIBPROTOBUF_EXPORT FileDescriptor {
00798  public:
00799   // The filename, relative to the source tree.
00800   // e.g. "google/protobuf/descriptor.proto"
00801   const string& name() const;
00802 
00803   // The package, e.g. "google.protobuf.compiler".
00804   const string& package() const;
00805 
00806   // The DescriptorPool in which this FileDescriptor and all its contents were
00807   // allocated.  Never NULL.
00808   const DescriptorPool* pool() const;
00809 
00810   // The number of files imported by this one.
00811   int dependency_count() const;
00812   // Gets an imported file by index, where 0 <= index < dependency_count().
00813   // These are returned in the order they were defined in the .proto file.
00814   const FileDescriptor* dependency(int index) const;
00815 
00816   // Number of top-level message types defined in this file.  (This does not
00817   // include nested types.)
00818   int message_type_count() const;
00819   // Gets a top-level message type, where 0 <= index < message_type_count().
00820   // These are returned in the order they were defined in the .proto file.
00821   const Descriptor* message_type(int index) const;
00822 
00823   // Number of top-level enum types defined in this file.  (This does not
00824   // include nested types.)
00825   int enum_type_count() const;
00826   // Gets a top-level enum type, where 0 <= index < enum_type_count().
00827   // These are returned in the order they were defined in the .proto file.
00828   const EnumDescriptor* enum_type(int index) const;
00829 
00830   // Number of services defined in this file.
00831   int service_count() const;
00832   // Gets a service, where 0 <= index < service_count().
00833   // These are returned in the order they were defined in the .proto file.
00834   const ServiceDescriptor* service(int index) const;
00835 
00836   // Number of extensions defined at file scope.  (This does not include
00837   // extensions nested within message types.)
00838   int extension_count() const;
00839   // Gets an extension's descriptor, where 0 <= index < extension_count().
00840   // These are returned in the order they were defined in the .proto file.
00841   const FieldDescriptor* extension(int index) const;
00842 
00843   // Get options for this file.  These are specified in the .proto file by
00844   // placing lines like "option foo = 1234;" at the top level, outside of any
00845   // other definitions.  Allowed options are defined by FileOptions in
00846   // google/protobuf/descriptor.proto, and any available extensions of that
00847   // message.
00848   const FileOptions& options() const;
00849 
00850   // Find a top-level message type by name.  Returns NULL if not found.
00851   const Descriptor* FindMessageTypeByName(const string& name) const;
00852   // Find a top-level enum type by name.  Returns NULL if not found.
00853   const EnumDescriptor* FindEnumTypeByName(const string& name) const;
00854   // Find an enum value defined in any top-level enum by name.  Returns NULL if
00855   // not found.
00856   const EnumValueDescriptor* FindEnumValueByName(const string& name) const;
00857   // Find a service definition by name.  Returns NULL if not found.
00858   const ServiceDescriptor* FindServiceByName(const string& name) const;
00859   // Find a top-level extension definition by name.  Returns NULL if not found.
00860   const FieldDescriptor* FindExtensionByName(const string& name) const;
00861   // Similar to FindExtensionByName(), but searches by lowercased-name.  See
00862   // Descriptor::FindFieldByLowercaseName().
00863   const FieldDescriptor* FindExtensionByLowercaseName(const string& name) const;
00864   // Similar to FindExtensionByName(), but searches by camelcased-name.  See
00865   // Descriptor::FindFieldByCamelcaseName().
00866   const FieldDescriptor* FindExtensionByCamelcaseName(const string& name) const;
00867 
00868   // See Descriptor::CopyTo().
00869   void CopyTo(FileDescriptorProto* proto) const;
00870 
00871   // See Descriptor::DebugString().
00872   string DebugString() const;
00873 
00874  private:
00875   typedef FileOptions OptionsType;
00876 
00877   const string* name_;
00878   const string* package_;
00879   const DescriptorPool* pool_;
00880   int dependency_count_;
00881   const FileDescriptor** dependencies_;
00882   int message_type_count_;
00883   Descriptor* message_types_;
00884   int enum_type_count_;
00885   EnumDescriptor* enum_types_;
00886   int service_count_;
00887   ServiceDescriptor* services_;
00888   int extension_count_;
00889   FieldDescriptor* extensions_;
00890   const FileOptions* options_;
00891 
00892   const FileDescriptorTables* tables_;
00893   // IMPORTANT:  If you add a new field, make sure to search for all instances
00894   // of Allocate<FileDescriptor>() and AllocateArray<FileDescriptor>() in
00895   // descriptor.cc and update them to initialize the field.
00896 
00897   FileDescriptor() {}
00898   friend class DescriptorBuilder;
00899   friend class Descriptor;
00900   friend class FieldDescriptor;
00901   friend class EnumDescriptor;
00902   friend class ServiceDescriptor;
00903   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileDescriptor);
00904 };
00905 
00906 // ===================================================================
00907 
00908 // Used to construct descriptors.
00909 //
00910 // Normally you won't want to build your own descriptors.  Message classes
00911 // constructed by the protocol compiler will provide them for you.  However,
00912 // if you are implementing Message on your own, or if you are writing a
00913 // program which can operate on totally arbitrary types and needs to load
00914 // them from some sort of database, you might need to.
00915 //
00916 // Since Descriptors are composed of a whole lot of cross-linked bits of
00917 // data that would be a pain to put together manually, the
00918 // DescriptorPool class is provided to make the process easier.  It can
00919 // take a FileDescriptorProto (defined in descriptor.proto), validate it,
00920 // and convert it to a set of nicely cross-linked Descriptors.
00921 //
00922 // DescriptorPool also helps with memory management.  Descriptors are
00923 // composed of many objects containing static data and pointers to each
00924 // other.  In all likelihood, when it comes time to delete this data,
00925 // you'll want to delete it all at once.  In fact, it is not uncommon to
00926 // have a whole pool of descriptors all cross-linked with each other which
00927 // you wish to delete all at once.  This class represents such a pool, and
00928 // handles the memory management for you.
00929 //
00930 // You can also search for descriptors within a DescriptorPool by name, and
00931 // extensions by number.
00932 class LIBPROTOBUF_EXPORT DescriptorPool {
00933  public:
00934   // Create a normal, empty DescriptorPool.
00935   DescriptorPool();
00936 
00937   // Constructs a DescriptorPool that, when it can't find something among the
00938   // descriptors already in the pool, looks for it in the given
00939   // DescriptorDatabase.
00940   // Notes:
00941   // - If a DescriptorPool is constructed this way, its BuildFile*() methods
00942   //   must not be called (they will assert-fail).  The only way to populate
00943   //   the pool with descriptors is to call the Find*By*() methods.
00944   // - The Find*By*() methods may block the calling thread if the
00945   //   DescriptorDatabase blocks.  This in turn means that parsing messages
00946   //   may block if they need to look up extensions.
00947   // - The Find*By*() methods will use mutexes for thread-safety, thus making
00948   //   them slower even when they don't have to fall back to the database.
00949   //   In fact, even the Find*By*() methods of descriptor objects owned by
00950   //   this pool will be slower, since they will have to obtain locks too.
00951   // - An ErrorCollector may optionally be given to collect validation errors
00952   //   in files loaded from the database.  If not given, errors will be printed
00953   //   to GOOGLE_LOG(ERROR).  Remember that files are built on-demand, so this
00954   //   ErrorCollector may be called from any thread that calls one of the
00955   //   Find*By*() methods.
00956   class ErrorCollector;
00957   explicit DescriptorPool(DescriptorDatabase* fallback_database,
00958                           ErrorCollector* error_collector = NULL);
00959 
00960   ~DescriptorPool();
00961 
00962   // Get a pointer to the generated pool.  Generated protocol message classes
00963   // which are compiled into the binary will allocate their descriptors in
00964   // this pool.  Do not add your own descriptors to this pool.
00965   static const DescriptorPool* generated_pool();
00966 
00967   // Find a FileDescriptor in the pool by file name.  Returns NULL if not
00968   // found.
00969   const FileDescriptor* FindFileByName(const string& name) const;
00970 
00971   // Find the FileDescriptor in the pool which defines the given symbol.
00972   // If any of the Find*ByName() methods below would succeed, then this is
00973   // equivalent to calling that method and calling the result's file() method.
00974   // Otherwise this returns NULL.
00975   const FileDescriptor* FindFileContainingSymbol(
00976       const string& symbol_name) const;
00977 
00978   // Looking up descriptors ------------------------------------------
00979   // These find descriptors by fully-qualified name.  These will find both
00980   // top-level descriptors and nested descriptors.  They return NULL if not
00981   // found.
00982 
00983   const Descriptor* FindMessageTypeByName(const string& name) const;
00984   const FieldDescriptor* FindFieldByName(const string& name) const;
00985   const FieldDescriptor* FindExtensionByName(const string& name) const;
00986   const EnumDescriptor* FindEnumTypeByName(const string& name) const;
00987   const EnumValueDescriptor* FindEnumValueByName(const string& name) const;
00988   const ServiceDescriptor* FindServiceByName(const string& name) const;
00989   const MethodDescriptor* FindMethodByName(const string& name) const;
00990 
00991   // Finds an extension of the given type by number.  The extendee must be
00992   // a member of this DescriptorPool or one of its underlays.
00993   const FieldDescriptor* FindExtensionByNumber(const Descriptor* extendee,
00994                                                int number) const;
00995 
00996   // Finds extensions of extendee. The extensions will be appended to
00997   // out in an undefined order. Only extensions defined directly in
00998   // this DescriptorPool or one of its underlays are guaranteed to be
00999   // found: extensions defined in the fallback database might not be found
01000   // depending on the database implementation.
01001   void FindAllExtensions(const Descriptor* extendee,
01002                          vector<const FieldDescriptor*>* out) const;
01003 
01004   // Building descriptors --------------------------------------------
01005 
01006   // When converting a FileDescriptorProto to a FileDescriptor, various
01007   // errors might be detected in the input.  The caller may handle these
01008   // programmatically by implementing an ErrorCollector.
01009   class LIBPROTOBUF_EXPORT ErrorCollector {
01010    public:
01011     inline ErrorCollector() {}
01012     virtual ~ErrorCollector();
01013 
01014     // These constants specify what exact part of the construct is broken.
01015     // This is useful e.g. for mapping the error back to an exact location
01016     // in a .proto file.
01017     enum ErrorLocation {
01018       NAME,              // the symbol name, or the package name for files
01019       NUMBER,            // field or extension range number
01020       TYPE,              // field type
01021       EXTENDEE,          // field extendee
01022       DEFAULT_VALUE,     // field default value
01023       INPUT_TYPE,        // method input type
01024       OUTPUT_TYPE,       // method output type
01025       OPTION_NAME,       // name in assignment
01026       OPTION_VALUE,      // value in option assignment
01027       OTHER              // some other problem
01028     };
01029 
01030     // Reports an error in the FileDescriptorProto.
01031     virtual void AddError(
01032       const string& filename,      // File name in which the error occurred.
01033       const string& element_name,  // Full name of the erroneous element.
01034       const Message* descriptor,   // Descriptor of the erroneous element.
01035       ErrorLocation location,      // One of the location constants, above.
01036       const string& message        // Human-readable error message.
01037       ) = 0;
01038 
01039    private:
01040     GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ErrorCollector);
01041   };
01042 
01043   // Convert the FileDescriptorProto to real descriptors and place them in
01044   // this DescriptorPool.  All dependencies of the file must already be in
01045   // the pool.  Returns the resulting FileDescriptor, or NULL if there were
01046   // problems with the input (e.g. the message was invalid, or dependencies
01047   // were missing).  Details about the errors are written to GOOGLE_LOG(ERROR).
01048   const FileDescriptor* BuildFile(const FileDescriptorProto& proto);
01049 
01050   // Same as BuildFile() except errors are sent to the given ErrorCollector.
01051   const FileDescriptor* BuildFileCollectingErrors(
01052     const FileDescriptorProto& proto,
01053     ErrorCollector* error_collector);
01054 
01055   // By default, it is an error if a FileDescriptorProto contains references
01056   // to types or other files that are not found in the DescriptorPool (or its
01057   // backing DescriptorDatabase, if any).  If you call
01058   // AllowUnknownDependencies(), however, then unknown types and files
01059   // will be replaced by placeholder descriptors.  This can allow you to
01060   // perform some useful operations with a .proto file even if you do not
01061   // have access to other .proto files on which it depends.  However, some
01062   // heuristics must be used to fill in the gaps in information, and these
01063   // can lead to descriptors which are inaccurate.  For example, the
01064   // DescriptorPool may be forced to guess whether an unknown type is a message
01065   // or an enum, as well as what package it resides in.  Furthermore,
01066   // placeholder types will not be discoverable via FindMessageTypeByName()
01067   // and similar methods, which could confuse some descriptor-based algorithms.
01068   // Generally, the results of this option should only be relied upon for
01069   // debugging purposes.
01070   void AllowUnknownDependencies() { allow_unknown_ = true; }
01071 
01072   // Internal stuff --------------------------------------------------
01073   // These methods MUST NOT be called from outside the proto2 library.
01074   // These methods may contain hidden pitfalls and may be removed in a
01075   // future library version.
01076 
01077   // Create a DescriptorPool which is overlaid on top of some other pool.
01078   // If you search for a descriptor in the overlay and it is not found, the
01079   // underlay will be searched as a backup.  If the underlay has its own
01080   // underlay, that will be searched next, and so on.  This also means that
01081   // files built in the overlay will be cross-linked with the underlay's
01082   // descriptors if necessary.  The underlay remains property of the caller;
01083   // it must remain valid for the lifetime of the newly-constructed pool.
01084   //
01085   // Example:  Say you want to parse a .proto file at runtime in order to use
01086   // its type with a DynamicMessage.  Say this .proto file has dependencies,
01087   // but you know that all the dependencies will be things that are already
01088   // compiled into the binary.  For ease of use, you'd like to load the types
01089   // right out of generated_pool() rather than have to parse redundant copies
01090   // of all these .protos and runtime.  But, you don't want to add the parsed
01091   // types directly into generated_pool(): this is not allowed, and would be
01092   // bad design anyway.  So, instead, you could use generated_pool() as an
01093   // underlay for a new DescriptorPool in which you add only the new file.
01094   //
01095   // WARNING:  Use of underlays can lead to many subtle gotchas.  Instead,
01096   //   try to formulate what you want to do in terms of DescriptorDatabases.
01097   explicit DescriptorPool(const DescriptorPool* underlay);
01098 
01099   // Called by generated classes at init time to add their descriptors to
01100   // generated_pool.  Do NOT call this in your own code!  filename must be a
01101   // permanent string (e.g. a string literal).
01102   static void InternalAddGeneratedFile(
01103       const void* encoded_file_descriptor, int size);
01104 
01105 
01106   // For internal use only:  Gets a non-const pointer to the generated pool.
01107   // This is called at static-initialization time only, so thread-safety is
01108   // not a concern.  If both an underlay and a fallback database are present,
01109   // the fallback database takes precedence.
01110   static DescriptorPool* internal_generated_pool();
01111 
01112   // For internal use only:  Changes the behavior of BuildFile() such that it
01113   // allows the file to make reference to message types declared in other files
01114   // which it did not officially declare as dependencies.
01115   void InternalDontEnforceDependencies();
01116 
01117   // For internal use only.
01118   void internal_set_underlay(const DescriptorPool* underlay) {
01119     underlay_ = underlay;
01120   }
01121 
01122   // For internal (unit test) use only:  Returns true if a FileDescriptor has
01123   // been constructed for the given file, false otherwise.  Useful for testing
01124   // lazy descriptor initialization behavior.
01125   bool InternalIsFileLoaded(const string& filename) const;
01126 
01127  private:
01128   friend class Descriptor;
01129   friend class FieldDescriptor;
01130   friend class EnumDescriptor;
01131   friend class ServiceDescriptor;
01132   friend class FileDescriptor;
01133   friend class DescriptorBuilder;
01134 
01135   // Tries to find something in the fallback database and link in the
01136   // corresponding proto file.  Returns true if successful, in which case
01137   // the caller should search for the thing again.  These are declared
01138   // const because they are called by (semantically) const methods.
01139   bool TryFindFileInFallbackDatabase(const string& name) const;
01140   bool TryFindSymbolInFallbackDatabase(const string& name) const;
01141   bool TryFindExtensionInFallbackDatabase(const Descriptor* containing_type,
01142                                           int field_number) const;
01143 
01144   // Like BuildFile() but called internally when the file has been loaded from
01145   // fallback_database_.  Declared const because it is called by (semantically)
01146   // const methods.
01147   const FileDescriptor* BuildFileFromDatabase(
01148     const FileDescriptorProto& proto) const;
01149 
01150   // If fallback_database_ is NULL, this is NULL.  Otherwise, this is a mutex
01151   // which must be locked while accessing tables_.
01152   Mutex* mutex_;
01153 
01154   // See constructor.
01155   DescriptorDatabase* fallback_database_;
01156   ErrorCollector* default_error_collector_;
01157   const DescriptorPool* underlay_;
01158 
01159   // This class contains a lot of hash maps with complicated types that
01160   // we'd like to keep out of the header.
01161   class Tables;
01162   scoped_ptr<Tables> tables_;
01163 
01164   bool enforce_dependencies_;
01165   bool allow_unknown_;
01166 
01167   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorPool);
01168 };
01169 
01170 // inline methods ====================================================
01171 
01172 // These macros makes this repetitive code more readable.
01173 #define PROTOBUF_DEFINE_ACCESSOR(CLASS, FIELD, TYPE) \
01174   inline TYPE CLASS::FIELD() const { return FIELD##_; }
01175 
01176 // Strings fields are stored as pointers but returned as const references.
01177 #define PROTOBUF_DEFINE_STRING_ACCESSOR(CLASS, FIELD) \
01178   inline const string& CLASS::FIELD() const { return *FIELD##_; }
01179 
01180 // Arrays take an index parameter, obviously.
01181 #define PROTOBUF_DEFINE_ARRAY_ACCESSOR(CLASS, FIELD, TYPE) \
01182   inline TYPE CLASS::FIELD(int index) const { return FIELD##s_ + index; }
01183 
01184 #define PROTOBUF_DEFINE_OPTIONS_ACCESSOR(CLASS, TYPE) \
01185   inline const TYPE& CLASS::options() const { return *options_; }
01186 
01187 PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, name)
01188 PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, full_name)
01189 PROTOBUF_DEFINE_ACCESSOR(Descriptor, file, const FileDescriptor*)
01190 PROTOBUF_DEFINE_ACCESSOR(Descriptor, containing_type, const Descriptor*)
01191 
01192 PROTOBUF_DEFINE_ACCESSOR(Descriptor, field_count, int)
01193 PROTOBUF_DEFINE_ACCESSOR(Descriptor, nested_type_count, int)
01194 PROTOBUF_DEFINE_ACCESSOR(Descriptor, enum_type_count, int)
01195 
01196 PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, field, const FieldDescriptor*)
01197 PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, nested_type, const Descriptor*)
01198 PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, enum_type, const EnumDescriptor*)
01199 
01200 PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_range_count, int)
01201 PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_count, int)
01202 PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension_range,
01203                                const Descriptor::ExtensionRange*)
01204 PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension,
01205                                const FieldDescriptor*)
01206 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(Descriptor, MessageOptions);
01207 
01208 PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, name)
01209 PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, full_name)
01210 PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, lowercase_name)
01211 PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, camelcase_name)
01212 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, file, const FileDescriptor*)
01213 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, number, int)
01214 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, is_extension, bool)
01215 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, type, FieldDescriptor::Type)
01216 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, label, FieldDescriptor::Label)
01217 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, containing_type, const Descriptor*)
01218 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, extension_scope, const Descriptor*)
01219 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, message_type, const Descriptor*)
01220 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, enum_type, const EnumDescriptor*)
01221 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, experimental_map_key,
01222                          const FieldDescriptor*)
01223 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FieldDescriptor, FieldOptions);
01224 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_default_value, bool)
01225 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int32 , int32 )
01226 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int64 , int64 )
01227 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint32, uint32)
01228 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint64, uint64)
01229 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_float , float )
01230 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_double, double)
01231 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_bool  , bool  )
01232 PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_enum,
01233                          const EnumValueDescriptor*)
01234 PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, default_value_string)
01235 
01236 PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, name)
01237 PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, full_name)
01238 PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, file, const FileDescriptor*)
01239 PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, containing_type, const Descriptor*)
01240 PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, value_count, int)
01241 PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, value,
01242                                const EnumValueDescriptor*)
01243 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumDescriptor, EnumOptions);
01244 
01245 PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, name)
01246 PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, full_name)
01247 PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, number, int)
01248 PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, type, const EnumDescriptor*)
01249 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumValueDescriptor, EnumValueOptions);
01250 
01251 PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, name)
01252 PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, full_name)
01253 PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, file, const FileDescriptor*)
01254 PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, method_count, int)
01255 PROTOBUF_DEFINE_ARRAY_ACCESSOR(ServiceDescriptor, method,
01256                                const MethodDescriptor*)
01257 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(ServiceDescriptor, ServiceOptions);
01258 
01259 PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, name)
01260 PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, full_name)
01261 PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, service, const ServiceDescriptor*)
01262 PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, input_type, const Descriptor*)
01263 PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, output_type, const Descriptor*)
01264 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(MethodDescriptor, MethodOptions);
01265 
01266 PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, name)
01267 PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, package)
01268 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, pool, const DescriptorPool*)
01269 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, dependency_count, int)
01270 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, message_type_count, int)
01271 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, enum_type_count, int)
01272 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, service_count, int)
01273 PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, extension_count, int)
01274 PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FileDescriptor, FileOptions);
01275 
01276 PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, message_type, const Descriptor*)
01277 PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, enum_type, const EnumDescriptor*)
01278 PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, service,
01279                                const ServiceDescriptor*)
01280 PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, extension,
01281                                const FieldDescriptor*)
01282 
01283 #undef PROTOBUF_DEFINE_ACCESSOR
01284 #undef PROTOBUF_DEFINE_STRING_ACCESSOR
01285 #undef PROTOBUF_DEFINE_ARRAY_ACCESSOR
01286 
01287 // A few accessors differ from the macros...
01288 
01289 inline bool FieldDescriptor::is_required() const {
01290   return label() == LABEL_REQUIRED;
01291 }
01292 
01293 inline bool FieldDescriptor::is_optional() const {
01294   return label() == LABEL_OPTIONAL;
01295 }
01296 
01297 inline bool FieldDescriptor::is_repeated() const {
01298   return label() == LABEL_REPEATED;
01299 }
01300 
01301 inline bool FieldDescriptor::is_packable() const {
01302   return is_repeated() && IsTypePackable(type());
01303 }
01304 
01305 // To save space, index() is computed by looking at the descriptor's position
01306 // in the parent's array of children.
01307 inline int FieldDescriptor::index() const {
01308   if (!is_extension_) {
01309     return this - containing_type_->fields_;
01310   } else if (extension_scope_ != NULL) {
01311     return this - extension_scope_->extensions_;
01312   } else {
01313     return this - file_->extensions_;
01314   }
01315 }
01316 
01317 inline int Descriptor::index() const {
01318   if (containing_type_ == NULL) {
01319     return this - file_->message_types_;
01320   } else {
01321     return this - containing_type_->nested_types_;
01322   }
01323 }
01324 
01325 inline int EnumDescriptor::index() const {
01326   if (containing_type_ == NULL) {
01327     return this - file_->enum_types_;
01328   } else {
01329     return this - containing_type_->enum_types_;
01330   }
01331 }
01332 
01333 inline int EnumValueDescriptor::index() const {
01334   return this - type_->values_;
01335 }
01336 
01337 inline int ServiceDescriptor::index() const {
01338   return this - file_->services_;
01339 }
01340 
01341 inline int MethodDescriptor::index() const {
01342   return this - service_->methods_;
01343 }
01344 
01345 inline FieldDescriptor::CppType FieldDescriptor::cpp_type() const {
01346   return kTypeToCppTypeMap[type_];
01347 }
01348 
01349 inline FieldDescriptor::CppType FieldDescriptor::TypeToCppType(Type type) {
01350   return kTypeToCppTypeMap[type];
01351 }
01352 
01353 inline bool FieldDescriptor::IsTypePackable(Type field_type) {
01354   return (field_type != FieldDescriptor::TYPE_STRING &&
01355           field_type != FieldDescriptor::TYPE_GROUP &&
01356           field_type != FieldDescriptor::TYPE_MESSAGE &&
01357           field_type != FieldDescriptor::TYPE_BYTES);
01358 }
01359 
01360 inline const FileDescriptor* FileDescriptor::dependency(int index) const {
01361   return dependencies_[index];
01362 }
01363 
01364 }  // namespace protobuf
01365 
01366 }  // namespace google
01367 #endif  // GOOGLE_PROTOBUF_DESCRIPTOR_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines