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