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