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 <map> |
58 | #include <memory> |
59 | #include <set> |
60 | #include <string> |
61 | #include <vector> |
62 | |
63 | #include <google/protobuf/stubs/common.h> |
64 | #include <google/protobuf/stubs/mutex.h> |
65 | #include <google/protobuf/stubs/once.h> |
66 | #include <google/protobuf/port_def.inc> |
67 | |
68 | // TYPE_BOOL is defined in the MacOS's ConditionalMacros.h. |
69 | #ifdef TYPE_BOOL |
70 | #undef TYPE_BOOL |
71 | #endif // TYPE_BOOL |
72 | |
73 | #ifdef SWIG |
74 | #define PROTOBUF_EXPORT |
75 | #endif |
76 | |
77 | |
78 | namespace google { |
79 | namespace protobuf { |
80 | |
81 | // Defined in this file. |
82 | class Descriptor; |
83 | class FieldDescriptor; |
84 | class OneofDescriptor; |
85 | class EnumDescriptor; |
86 | class EnumValueDescriptor; |
87 | class ServiceDescriptor; |
88 | class MethodDescriptor; |
89 | class FileDescriptor; |
90 | class DescriptorDatabase; |
91 | class DescriptorPool; |
92 | |
93 | // Defined in descriptor.proto |
94 | class DescriptorProto; |
95 | class DescriptorProto_ExtensionRange; |
96 | class FieldDescriptorProto; |
97 | class OneofDescriptorProto; |
98 | class EnumDescriptorProto; |
99 | class EnumValueDescriptorProto; |
100 | class ServiceDescriptorProto; |
101 | class MethodDescriptorProto; |
102 | class FileDescriptorProto; |
103 | class MessageOptions; |
104 | class FieldOptions; |
105 | class OneofOptions; |
106 | class EnumOptions; |
107 | class EnumValueOptions; |
108 | class ExtensionRangeOptions; |
109 | class ServiceOptions; |
110 | class MethodOptions; |
111 | class FileOptions; |
112 | class UninterpretedOption; |
113 | class SourceCodeInfo; |
114 | |
115 | // Defined in message.h |
116 | class Message; |
117 | class Reflection; |
118 | |
119 | // Defined in descriptor.cc |
120 | class DescriptorBuilder; |
121 | class FileDescriptorTables; |
122 | struct Symbol; |
123 | |
124 | // Defined in unknown_field_set.h. |
125 | class UnknownField; |
126 | |
127 | // Defined in command_line_interface.cc |
128 | namespace compiler { |
129 | class CommandLineInterface; |
130 | namespace cpp { |
131 | // Defined in helpers.h |
132 | class Formatter; |
133 | } // namespace cpp |
134 | } // namespace compiler |
135 | |
136 | namespace descriptor_unittest { |
137 | class DescriptorTest; |
138 | } // namespace descriptor_unittest |
139 | |
140 | // Defined in printer.h |
141 | namespace io { |
142 | class Printer; |
143 | } // namespace io |
144 | |
145 | // NB, all indices are zero-based. |
146 | struct SourceLocation { |
147 | int start_line; |
148 | int end_line; |
149 | int start_column; |
150 | int end_column; |
151 | |
152 | // Doc comments found at the source location. |
153 | // See the comments in SourceCodeInfo.Location (descriptor.proto) for details. |
154 | std::string ; |
155 | std::string ; |
156 | std::vector<std::string> ; |
157 | }; |
158 | |
159 | // Options when generating machine-parsable output from a descriptor with |
160 | // DebugString(). |
161 | struct DebugStringOptions { |
162 | // include original user comments as recorded in SourceLocation entries. N.B. |
163 | // that this must be |false| by default: several other pieces of code (for |
164 | // example, the C++ code generation for fields in the proto compiler) rely on |
165 | // DebugString() output being unobstructed by user comments. |
166 | bool ; |
167 | // If true, elide the braced body in the debug string. |
168 | bool elide_group_body; |
169 | bool elide_oneof_body; |
170 | |
171 | DebugStringOptions() |
172 | : include_comments(false), |
173 | elide_group_body(false), |
174 | elide_oneof_body(false) { |
175 | } |
176 | }; |
177 | |
178 | // A class to handle the simplest cases of a lazily linked descriptor |
179 | // for a message type that isn't built at the time of cross linking, |
180 | // which is needed when a pool has lazily_build_dependencies_ set. |
181 | // Must be instantiated as mutable in a descriptor. |
182 | namespace internal { |
183 | class PROTOBUF_EXPORT LazyDescriptor { |
184 | public: |
185 | // Init function to be called at init time of a descriptor containing |
186 | // a LazyDescriptor. |
187 | void Init() { |
188 | descriptor_ = nullptr; |
189 | name_ = nullptr; |
190 | once_ = nullptr; |
191 | file_ = nullptr; |
192 | } |
193 | |
194 | // Sets the value of the descriptor if it is known during the descriptor |
195 | // building process. Not thread safe, should only be called during the |
196 | // descriptor build process. Should not be called after SetLazy has been |
197 | // called. |
198 | void Set(const Descriptor* descriptor); |
199 | |
200 | // Sets the information needed to lazily cross link the descriptor at a later |
201 | // time, SetLazy is not thread safe, should be called only once at descriptor |
202 | // build time if the symbol wasn't found and building of the file containing |
203 | // that type is delayed because lazily_build_dependencies_ is set on the pool. |
204 | // Should not be called after Set() has been called. |
205 | void SetLazy(const std::string& name, const FileDescriptor* file); |
206 | |
207 | // Returns the current value of the descriptor, thread-safe. If SetLazy(...) |
208 | // has been called, will do a one-time cross link of the type specified, |
209 | // building the descriptor file that contains the type if necessary. |
210 | inline const Descriptor* Get() { |
211 | Once(); |
212 | return descriptor_; |
213 | } |
214 | |
215 | private: |
216 | static void OnceStatic(LazyDescriptor* lazy); |
217 | void OnceInternal(); |
218 | void Once(); |
219 | |
220 | const Descriptor* descriptor_; |
221 | const std::string* name_; |
222 | internal::once_flag* once_; |
223 | const FileDescriptor* file_; |
224 | }; |
225 | } // namespace internal |
226 | |
227 | // Describes a type of protocol message, or a particular group within a |
228 | // message. To obtain the Descriptor for a given message object, call |
229 | // Message::GetDescriptor(). Generated message classes also have a |
230 | // static method called descriptor() which returns the type's descriptor. |
231 | // Use DescriptorPool to construct your own descriptors. |
232 | class PROTOBUF_EXPORT Descriptor { |
233 | public: |
234 | typedef DescriptorProto Proto; |
235 | |
236 | // The name of the message type, not including its scope. |
237 | const std::string& name() const; |
238 | |
239 | // The fully-qualified name of the message type, scope delimited by |
240 | // periods. For example, message type "Foo" which is declared in package |
241 | // "bar" has full name "bar.Foo". If a type "Baz" is nested within |
242 | // Foo, Baz's full_name is "bar.Foo.Baz". To get only the part that |
243 | // comes after the last '.', use name(). |
244 | const std::string& full_name() const; |
245 | |
246 | // Index of this descriptor within the file or containing type's message |
247 | // type array. |
248 | int index() const; |
249 | |
250 | // The .proto file in which this message type was defined. Never nullptr. |
251 | const FileDescriptor* file() const; |
252 | |
253 | // If this Descriptor describes a nested type, this returns the type |
254 | // in which it is nested. Otherwise, returns nullptr. |
255 | const Descriptor* containing_type() const; |
256 | |
257 | // Get options for this message type. These are specified in the .proto file |
258 | // by placing lines like "option foo = 1234;" in the message definition. |
259 | // Allowed options are defined by MessageOptions in descriptor.proto, and any |
260 | // available extensions of that message. |
261 | const MessageOptions& options() const; |
262 | |
263 | // Write the contents of this Descriptor into the given DescriptorProto. |
264 | // The target DescriptorProto must be clear before calling this; if it |
265 | // isn't, the result may be garbage. |
266 | void CopyTo(DescriptorProto* proto) const; |
267 | |
268 | // Write the contents of this descriptor in a human-readable form. Output |
269 | // will be suitable for re-parsing. |
270 | std::string DebugString() const; |
271 | |
272 | // Similar to DebugString(), but additionally takes options (e.g., |
273 | // include original user comments in output). |
274 | std::string DebugStringWithOptions(const DebugStringOptions& options) const; |
275 | |
276 | // Returns true if this is a placeholder for an unknown type. This will |
277 | // only be the case if this descriptor comes from a DescriptorPool |
278 | // with AllowUnknownDependencies() set. |
279 | bool is_placeholder() const; |
280 | |
281 | enum WellKnownType { |
282 | WELLKNOWNTYPE_UNSPECIFIED, // Not a well-known type. |
283 | |
284 | // Wrapper types. |
285 | WELLKNOWNTYPE_DOUBLEVALUE, // google.protobuf.DoubleValue |
286 | WELLKNOWNTYPE_FLOATVALUE, // google.protobuf.FloatValue |
287 | WELLKNOWNTYPE_INT64VALUE, // google.protobuf.Int64Value |
288 | WELLKNOWNTYPE_UINT64VALUE, // google.protobuf.UInt64Value |
289 | WELLKNOWNTYPE_INT32VALUE, // google.protobuf.Int32Value |
290 | WELLKNOWNTYPE_UINT32VALUE, // google.protobuf.UInt32Value |
291 | WELLKNOWNTYPE_STRINGVALUE, // google.protobuf.StringValue |
292 | WELLKNOWNTYPE_BYTESVALUE, // google.protobuf.BytesValue |
293 | WELLKNOWNTYPE_BOOLVALUE, // google.protobuf.BoolValue |
294 | |
295 | // Other well known types. |
296 | WELLKNOWNTYPE_ANY, // google.protobuf.Any |
297 | WELLKNOWNTYPE_FIELDMASK, // google.protobuf.FieldMask |
298 | WELLKNOWNTYPE_DURATION, // google.protobuf.Duration |
299 | WELLKNOWNTYPE_TIMESTAMP, // google.protobuf.Timestamp |
300 | WELLKNOWNTYPE_VALUE, // google.protobuf.Value |
301 | WELLKNOWNTYPE_LISTVALUE, // google.protobuf.ListValue |
302 | WELLKNOWNTYPE_STRUCT, // google.protobuf.Struct |
303 | |
304 | // New well-known types may be added in the future. |
305 | // Please make sure any switch() statements have a 'default' case. |
306 | __WELLKNOWNTYPE__DO_NOT_USE__ADD_DEFAULT_INSTEAD__, |
307 | }; |
308 | |
309 | WellKnownType well_known_type() const; |
310 | |
311 | // Field stuff ----------------------------------------------------- |
312 | |
313 | // The number of fields in this message type. |
314 | int field_count() const; |
315 | // Gets a field by index, where 0 <= index < field_count(). |
316 | // These are returned in the order they were defined in the .proto file. |
317 | const FieldDescriptor* field(int index) const; |
318 | |
319 | // Looks up a field by declared tag number. Returns nullptr if no such field |
320 | // exists. |
321 | const FieldDescriptor* FindFieldByNumber(int number) const; |
322 | // Looks up a field by name. Returns nullptr if no such field exists. |
323 | const FieldDescriptor* FindFieldByName(const std::string& name) const; |
324 | |
325 | // Looks up a field by lowercased name (as returned by lowercase_name()). |
326 | // This lookup may be ambiguous if multiple field names differ only by case, |
327 | // in which case the field returned is chosen arbitrarily from the matches. |
328 | const FieldDescriptor* FindFieldByLowercaseName( |
329 | const std::string& lowercase_name) const; |
330 | |
331 | // Looks up a field by camel-case name (as returned by camelcase_name()). |
332 | // This lookup may be ambiguous if multiple field names differ in a way that |
333 | // leads them to have identical camel-case names, in which case the field |
334 | // returned is chosen arbitrarily from the matches. |
335 | const FieldDescriptor* FindFieldByCamelcaseName( |
336 | const std::string& camelcase_name) const; |
337 | |
338 | // The number of oneofs in this message type. |
339 | int oneof_decl_count() const; |
340 | // The number of oneofs in this message type, excluding synthetic oneofs. |
341 | // Real oneofs always come first, so iterating up to real_oneof_decl_cout() |
342 | // will yield all real oneofs. |
343 | int real_oneof_decl_count() const; |
344 | // Get a oneof by index, where 0 <= index < oneof_decl_count(). |
345 | // These are returned in the order they were defined in the .proto file. |
346 | const OneofDescriptor* oneof_decl(int index) const; |
347 | |
348 | // Looks up a oneof by name. Returns nullptr if no such oneof exists. |
349 | const OneofDescriptor* FindOneofByName(const std::string& name) const; |
350 | |
351 | // Nested type stuff ----------------------------------------------- |
352 | |
353 | // The number of nested types in this message type. |
354 | int nested_type_count() const; |
355 | // Gets a nested type by index, where 0 <= index < nested_type_count(). |
356 | // These are returned in the order they were defined in the .proto file. |
357 | const Descriptor* nested_type(int index) const; |
358 | |
359 | // Looks up a nested type by name. Returns nullptr if no such nested type |
360 | // exists. |
361 | const Descriptor* FindNestedTypeByName(const std::string& name) const; |
362 | |
363 | // Enum stuff ------------------------------------------------------ |
364 | |
365 | // The number of enum types in this message type. |
366 | int enum_type_count() const; |
367 | // Gets an enum type by index, where 0 <= index < enum_type_count(). |
368 | // These are returned in the order they were defined in the .proto file. |
369 | const EnumDescriptor* enum_type(int index) const; |
370 | |
371 | // Looks up an enum type by name. Returns nullptr if no such enum type |
372 | // exists. |
373 | const EnumDescriptor* FindEnumTypeByName(const std::string& name) const; |
374 | |
375 | // Looks up an enum value by name, among all enum types in this message. |
376 | // Returns nullptr if no such value exists. |
377 | const EnumValueDescriptor* FindEnumValueByName(const std::string& name) const; |
378 | |
379 | // Extensions ------------------------------------------------------ |
380 | |
381 | // A range of field numbers which are designated for third-party |
382 | // extensions. |
383 | struct ExtensionRange { |
384 | typedef DescriptorProto_ExtensionRange Proto; |
385 | |
386 | typedef ExtensionRangeOptions OptionsType; |
387 | |
388 | // See Descriptor::CopyTo(). |
389 | void CopyTo(DescriptorProto_ExtensionRange* proto) const; |
390 | |
391 | int start; // inclusive |
392 | int end; // exclusive |
393 | |
394 | const ExtensionRangeOptions* options_; |
395 | }; |
396 | |
397 | // The number of extension ranges in this message type. |
398 | int extension_range_count() const; |
399 | // Gets an extension range by index, where 0 <= index < |
400 | // extension_range_count(). These are returned in the order they were defined |
401 | // in the .proto file. |
402 | const ExtensionRange* extension_range(int index) const; |
403 | |
404 | // Returns true if the number is in one of the extension ranges. |
405 | bool IsExtensionNumber(int number) const; |
406 | |
407 | // Returns nullptr if no extension range contains the given number. |
408 | const ExtensionRange* FindExtensionRangeContainingNumber(int number) const; |
409 | |
410 | // The number of extensions defined nested within this message type's scope. |
411 | // See doc: |
412 | // https://developers.google.com/protocol-buffers/docs/proto#nested-extensions |
413 | // |
414 | // Note that the extensions may be extending *other* messages. |
415 | // |
416 | // For example: |
417 | // message M1 { |
418 | // extensions 1 to max; |
419 | // } |
420 | // |
421 | // message M2 { |
422 | // extend M1 { |
423 | // optional int32 foo = 1; |
424 | // } |
425 | // } |
426 | // |
427 | // In this case, |
428 | // DescriptorPool::generated_pool() |
429 | // ->FindMessageTypeByName("M2") |
430 | // ->extension(0) |
431 | // will return "foo", even though "foo" is an extension of M1. |
432 | // To find all known extensions of a given message, instead use |
433 | // DescriptorPool::FindAllExtensions. |
434 | int extension_count() const; |
435 | // Get an extension by index, where 0 <= index < extension_count(). |
436 | // These are returned in the order they were defined in the .proto file. |
437 | const FieldDescriptor* extension(int index) const; |
438 | |
439 | // Looks up a named extension (which extends some *other* message type) |
440 | // defined within this message type's scope. |
441 | const FieldDescriptor* FindExtensionByName(const std::string& name) const; |
442 | |
443 | // Similar to FindFieldByLowercaseName(), but finds extensions defined within |
444 | // this message type's scope. |
445 | const FieldDescriptor* FindExtensionByLowercaseName( |
446 | const std::string& name) const; |
447 | |
448 | // Similar to FindFieldByCamelcaseName(), but finds extensions defined within |
449 | // this message type's scope. |
450 | const FieldDescriptor* FindExtensionByCamelcaseName( |
451 | const std::string& name) const; |
452 | |
453 | // Reserved fields ------------------------------------------------- |
454 | |
455 | // A range of reserved field numbers. |
456 | struct ReservedRange { |
457 | int start; // inclusive |
458 | int end; // exclusive |
459 | }; |
460 | |
461 | // The number of reserved ranges in this message type. |
462 | int reserved_range_count() const; |
463 | // Gets an reserved range by index, where 0 <= index < |
464 | // reserved_range_count(). These are returned in the order they were defined |
465 | // in the .proto file. |
466 | const ReservedRange* reserved_range(int index) const; |
467 | |
468 | // Returns true if the number is in one of the reserved ranges. |
469 | bool IsReservedNumber(int number) const; |
470 | |
471 | // Returns nullptr if no reserved range contains the given number. |
472 | const ReservedRange* FindReservedRangeContainingNumber(int number) const; |
473 | |
474 | // The number of reserved field names in this message type. |
475 | int reserved_name_count() const; |
476 | |
477 | // Gets a reserved name by index, where 0 <= index < reserved_name_count(). |
478 | const std::string& reserved_name(int index) const; |
479 | |
480 | // Returns true if the field name is reserved. |
481 | bool IsReservedName(const std::string& name) const; |
482 | |
483 | // Source Location --------------------------------------------------- |
484 | |
485 | // Updates |*out_location| to the source location of the complete |
486 | // extent of this message declaration. Returns false and leaves |
487 | // |*out_location| unchanged iff location information was not available. |
488 | bool GetSourceLocation(SourceLocation* out_location) const; |
489 | |
490 | // Maps -------------------------------------------------------------- |
491 | |
492 | // Returns the FieldDescriptor for the "key" field. If this isn't a map entry |
493 | // field, returns nullptr. |
494 | const FieldDescriptor* map_key() const; |
495 | |
496 | // Returns the FieldDescriptor for the "value" field. If this isn't a map |
497 | // entry field, returns nullptr. |
498 | const FieldDescriptor* map_value() const; |
499 | |
500 | private: |
501 | typedef MessageOptions OptionsType; |
502 | |
503 | // Allows tests to test CopyTo(proto, true). |
504 | friend class descriptor_unittest::DescriptorTest; |
505 | |
506 | // Allows access to GetLocationPath for annotations. |
507 | friend class io::Printer; |
508 | friend class compiler::cpp::Formatter; |
509 | |
510 | // Fill the json_name field of FieldDescriptorProto. |
511 | void CopyJsonNameTo(DescriptorProto* proto) const; |
512 | |
513 | // Internal version of DebugString; controls the level of indenting for |
514 | // correct depth. Takes |options| to control debug-string options, and |
515 | // |include_opening_clause| to indicate whether the "message ... " part of the |
516 | // clause has already been generated (this varies depending on context). |
517 | void DebugString(int depth, std::string* contents, |
518 | const DebugStringOptions& options, |
519 | bool include_opening_clause) const; |
520 | |
521 | // Walks up the descriptor tree to generate the source location path |
522 | // to this descriptor from the file root. |
523 | void GetLocationPath(std::vector<int>* output) const; |
524 | |
525 | const std::string* name_; |
526 | const std::string* full_name_; |
527 | const FileDescriptor* file_; |
528 | const Descriptor* containing_type_; |
529 | const MessageOptions* options_; |
530 | |
531 | // These arrays are separated from their sizes to minimize padding on 64-bit. |
532 | FieldDescriptor* fields_; |
533 | OneofDescriptor* oneof_decls_; |
534 | Descriptor* nested_types_; |
535 | EnumDescriptor* enum_types_; |
536 | ExtensionRange* extension_ranges_; |
537 | FieldDescriptor* extensions_; |
538 | ReservedRange* reserved_ranges_; |
539 | const std::string** reserved_names_; |
540 | |
541 | int field_count_; |
542 | int oneof_decl_count_; |
543 | int real_oneof_decl_count_; |
544 | int nested_type_count_; |
545 | int enum_type_count_; |
546 | int extension_range_count_; |
547 | int extension_count_; |
548 | int reserved_range_count_; |
549 | int reserved_name_count_; |
550 | |
551 | // True if this is a placeholder for an unknown type. |
552 | bool is_placeholder_; |
553 | // True if this is a placeholder and the type name wasn't fully-qualified. |
554 | bool is_unqualified_placeholder_; |
555 | // Well known type. Stored as char to conserve space. |
556 | char well_known_type_; |
557 | |
558 | // IMPORTANT: If you add a new field, make sure to search for all instances |
559 | // of Allocate<Descriptor>() and AllocateArray<Descriptor>() in descriptor.cc |
560 | // and update them to initialize the field. |
561 | |
562 | // Must be constructed using DescriptorPool. |
563 | Descriptor() {} |
564 | friend class DescriptorBuilder; |
565 | friend class DescriptorPool; |
566 | friend class EnumDescriptor; |
567 | friend class FieldDescriptor; |
568 | friend class OneofDescriptor; |
569 | friend class MethodDescriptor; |
570 | friend class FileDescriptor; |
571 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Descriptor); |
572 | }; |
573 | |
574 | |
575 | // Describes a single field of a message. To get the descriptor for a given |
576 | // field, first get the Descriptor for the message in which it is defined, |
577 | // then call Descriptor::FindFieldByName(). To get a FieldDescriptor for |
578 | // an extension, do one of the following: |
579 | // - Get the Descriptor or FileDescriptor for its containing scope, then |
580 | // call Descriptor::FindExtensionByName() or |
581 | // FileDescriptor::FindExtensionByName(). |
582 | // - Given a DescriptorPool, call DescriptorPool::FindExtensionByNumber() or |
583 | // DescriptorPool::FindExtensionByPrintableName(). |
584 | // Use DescriptorPool to construct your own descriptors. |
585 | class PROTOBUF_EXPORT FieldDescriptor { |
586 | public: |
587 | typedef FieldDescriptorProto Proto; |
588 | |
589 | // Identifies a field type. 0 is reserved for errors. The order is weird |
590 | // for historical reasons. Types 12 and up are new in proto2. |
591 | enum Type { |
592 | TYPE_DOUBLE = 1, // double, exactly eight bytes on the wire. |
593 | TYPE_FLOAT = 2, // float, exactly four bytes on the wire. |
594 | TYPE_INT64 = 3, // int64, varint on the wire. Negative numbers |
595 | // take 10 bytes. Use TYPE_SINT64 if negative |
596 | // values are likely. |
597 | TYPE_UINT64 = 4, // uint64, varint on the wire. |
598 | TYPE_INT32 = 5, // int32, varint on the wire. Negative numbers |
599 | // take 10 bytes. Use TYPE_SINT32 if negative |
600 | // values are likely. |
601 | TYPE_FIXED64 = 6, // uint64, exactly eight bytes on the wire. |
602 | TYPE_FIXED32 = 7, // uint32, exactly four bytes on the wire. |
603 | TYPE_BOOL = 8, // bool, varint on the wire. |
604 | TYPE_STRING = 9, // UTF-8 text. |
605 | TYPE_GROUP = 10, // Tag-delimited message. Deprecated. |
606 | TYPE_MESSAGE = 11, // Length-delimited message. |
607 | |
608 | TYPE_BYTES = 12, // Arbitrary byte array. |
609 | TYPE_UINT32 = 13, // uint32, varint on the wire |
610 | TYPE_ENUM = 14, // Enum, varint on the wire |
611 | TYPE_SFIXED32 = 15, // int32, exactly four bytes on the wire |
612 | TYPE_SFIXED64 = 16, // int64, exactly eight bytes on the wire |
613 | TYPE_SINT32 = 17, // int32, ZigZag-encoded varint on the wire |
614 | TYPE_SINT64 = 18, // int64, ZigZag-encoded varint on the wire |
615 | |
616 | MAX_TYPE = 18, // Constant useful for defining lookup tables |
617 | // indexed by Type. |
618 | }; |
619 | |
620 | // Specifies the C++ data type used to represent the field. There is a |
621 | // fixed mapping from Type to CppType where each Type maps to exactly one |
622 | // CppType. 0 is reserved for errors. |
623 | enum CppType { |
624 | CPPTYPE_INT32 = 1, // TYPE_INT32, TYPE_SINT32, TYPE_SFIXED32 |
625 | CPPTYPE_INT64 = 2, // TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64 |
626 | CPPTYPE_UINT32 = 3, // TYPE_UINT32, TYPE_FIXED32 |
627 | CPPTYPE_UINT64 = 4, // TYPE_UINT64, TYPE_FIXED64 |
628 | CPPTYPE_DOUBLE = 5, // TYPE_DOUBLE |
629 | CPPTYPE_FLOAT = 6, // TYPE_FLOAT |
630 | CPPTYPE_BOOL = 7, // TYPE_BOOL |
631 | CPPTYPE_ENUM = 8, // TYPE_ENUM |
632 | CPPTYPE_STRING = 9, // TYPE_STRING, TYPE_BYTES |
633 | CPPTYPE_MESSAGE = 10, // TYPE_MESSAGE, TYPE_GROUP |
634 | |
635 | MAX_CPPTYPE = 10, // Constant useful for defining lookup tables |
636 | // indexed by CppType. |
637 | }; |
638 | |
639 | // Identifies whether the field is optional, required, or repeated. 0 is |
640 | // reserved for errors. |
641 | enum Label { |
642 | LABEL_OPTIONAL = 1, // optional |
643 | LABEL_REQUIRED = 2, // required |
644 | LABEL_REPEATED = 3, // repeated |
645 | |
646 | MAX_LABEL = 3, // Constant useful for defining lookup tables |
647 | // indexed by Label. |
648 | }; |
649 | |
650 | // Valid field numbers are positive integers up to kMaxNumber. |
651 | static const int kMaxNumber = (1 << 29) - 1; |
652 | |
653 | // First field number reserved for the protocol buffer library implementation. |
654 | // Users may not declare fields that use reserved numbers. |
655 | static const int kFirstReservedNumber = 19000; |
656 | // Last field number reserved for the protocol buffer library implementation. |
657 | // Users may not declare fields that use reserved numbers. |
658 | static const int kLastReservedNumber = 19999; |
659 | |
660 | const std::string& name() const; // Name of this field within the message. |
661 | const std::string& full_name() const; // Fully-qualified name of the field. |
662 | const std::string& json_name() const; // JSON name of this field. |
663 | const FileDescriptor* file() const; // File in which this field was defined. |
664 | bool is_extension() const; // Is this an extension field? |
665 | int number() const; // Declared tag number. |
666 | |
667 | // Same as name() except converted to lower-case. This (and especially the |
668 | // FindFieldByLowercaseName() method) can be useful when parsing formats |
669 | // which prefer to use lowercase naming style. (Although, technically |
670 | // field names should be lowercased anyway according to the protobuf style |
671 | // guide, so this only makes a difference when dealing with old .proto files |
672 | // which do not follow the guide.) |
673 | const std::string& lowercase_name() const; |
674 | |
675 | // Same as name() except converted to camel-case. In this conversion, any |
676 | // time an underscore appears in the name, it is removed and the next |
677 | // letter is capitalized. Furthermore, the first letter of the name is |
678 | // lower-cased. Examples: |
679 | // FooBar -> fooBar |
680 | // foo_bar -> fooBar |
681 | // fooBar -> fooBar |
682 | // This (and especially the FindFieldByCamelcaseName() method) can be useful |
683 | // when parsing formats which prefer to use camel-case naming style. |
684 | const std::string& camelcase_name() const; |
685 | |
686 | Type type() const; // Declared type of this field. |
687 | const char* type_name() const; // Name of the declared type. |
688 | CppType cpp_type() const; // C++ type of this field. |
689 | const char* cpp_type_name() const; // Name of the C++ type. |
690 | Label label() const; // optional/required/repeated |
691 | |
692 | bool is_required() const; // shorthand for label() == LABEL_REQUIRED |
693 | bool is_optional() const; // shorthand for label() == LABEL_OPTIONAL |
694 | bool is_repeated() const; // shorthand for label() == LABEL_REPEATED |
695 | bool is_packable() const; // shorthand for is_repeated() && |
696 | // IsTypePackable(type()) |
697 | bool is_packed() const; // shorthand for is_packable() && |
698 | // options().packed() |
699 | bool is_map() const; // shorthand for type() == TYPE_MESSAGE && |
700 | // message_type()->options().map_entry() |
701 | |
702 | // Returns true if this field was syntactically written with "optional" in the |
703 | // .proto file. Excludes singular proto3 fields that do not have a label. |
704 | bool has_optional_keyword() const; |
705 | |
706 | // Returns true if this field tracks presence, ie. does the field |
707 | // distinguish between "unset" and "present with default value." |
708 | // This includes required, optional, and oneof fields. It excludes maps, |
709 | // repeated fields, and singular proto3 fields without "optional". |
710 | // |
711 | // For fields where has_presence() == true, the return value of |
712 | // Reflection::HasField() is semantically meaningful. |
713 | bool has_presence() const; |
714 | |
715 | // Index of this field within the message's field array, or the file or |
716 | // extension scope's extensions array. |
717 | int index() const; |
718 | |
719 | // Does this field have an explicitly-declared default value? |
720 | bool has_default_value() const; |
721 | |
722 | // Whether the user has specified the json_name field option in the .proto |
723 | // file. |
724 | bool has_json_name() const; |
725 | |
726 | // Get the field default value if cpp_type() == CPPTYPE_INT32. If no |
727 | // explicit default was defined, the default is 0. |
728 | int32 default_value_int32() const; |
729 | // Get the field default value if cpp_type() == CPPTYPE_INT64. If no |
730 | // explicit default was defined, the default is 0. |
731 | int64 default_value_int64() const; |
732 | // Get the field default value if cpp_type() == CPPTYPE_UINT32. If no |
733 | // explicit default was defined, the default is 0. |
734 | uint32 default_value_uint32() const; |
735 | // Get the field default value if cpp_type() == CPPTYPE_UINT64. If no |
736 | // explicit default was defined, the default is 0. |
737 | uint64 default_value_uint64() const; |
738 | // Get the field default value if cpp_type() == CPPTYPE_FLOAT. If no |
739 | // explicit default was defined, the default is 0.0. |
740 | float default_value_float() const; |
741 | // Get the field default value if cpp_type() == CPPTYPE_DOUBLE. If no |
742 | // explicit default was defined, the default is 0.0. |
743 | double default_value_double() const; |
744 | // Get the field default value if cpp_type() == CPPTYPE_BOOL. If no |
745 | // explicit default was defined, the default is false. |
746 | bool default_value_bool() const; |
747 | // Get the field default value if cpp_type() == CPPTYPE_ENUM. If no |
748 | // explicit default was defined, the default is the first value defined |
749 | // in the enum type (all enum types are required to have at least one value). |
750 | // This never returns nullptr. |
751 | const EnumValueDescriptor* default_value_enum() const; |
752 | // Get the field default value if cpp_type() == CPPTYPE_STRING. If no |
753 | // explicit default was defined, the default is the empty string. |
754 | const std::string& default_value_string() const; |
755 | |
756 | // The Descriptor for the message of which this is a field. For extensions, |
757 | // this is the extended type. Never nullptr. |
758 | const Descriptor* containing_type() const; |
759 | |
760 | // If the field is a member of a oneof, this is the one, otherwise this is |
761 | // nullptr. |
762 | const OneofDescriptor* containing_oneof() const; |
763 | |
764 | // If the field is a member of a non-synthetic oneof, returns the descriptor |
765 | // for the oneof, otherwise returns nullptr. |
766 | const OneofDescriptor* real_containing_oneof() const; |
767 | |
768 | // If the field is a member of a oneof, returns the index in that oneof. |
769 | int index_in_oneof() const; |
770 | |
771 | // An extension may be declared within the scope of another message. If this |
772 | // field is an extension (is_extension() is true), then extension_scope() |
773 | // returns that message, or nullptr if the extension was declared at global |
774 | // scope. If this is not an extension, extension_scope() is undefined (may |
775 | // assert-fail). |
776 | const Descriptor* extension_scope() const; |
777 | |
778 | // If type is TYPE_MESSAGE or TYPE_GROUP, returns a descriptor for the |
779 | // message or the group type. Otherwise, returns null. |
780 | const Descriptor* message_type() const; |
781 | // If type is TYPE_ENUM, returns a descriptor for the enum. Otherwise, |
782 | // returns null. |
783 | const EnumDescriptor* enum_type() const; |
784 | |
785 | // Get the FieldOptions for this field. This includes things listed in |
786 | // square brackets after the field definition. E.g., the field: |
787 | // optional string text = 1 [ctype=CORD]; |
788 | // has the "ctype" option set. Allowed options are defined by FieldOptions in |
789 | // descriptor.proto, and any available extensions of that message. |
790 | const FieldOptions& options() const; |
791 | |
792 | // See Descriptor::CopyTo(). |
793 | void CopyTo(FieldDescriptorProto* proto) const; |
794 | |
795 | // See Descriptor::DebugString(). |
796 | std::string DebugString() const; |
797 | |
798 | // See Descriptor::DebugStringWithOptions(). |
799 | std::string DebugStringWithOptions(const DebugStringOptions& options) const; |
800 | |
801 | // Helper method to get the CppType for a particular Type. |
802 | static CppType TypeToCppType(Type type); |
803 | |
804 | // Helper method to get the name of a Type. |
805 | static const char* TypeName(Type type); |
806 | |
807 | // Helper method to get the name of a CppType. |
808 | static const char* CppTypeName(CppType cpp_type); |
809 | |
810 | // Return true iff [packed = true] is valid for fields of this type. |
811 | static inline bool IsTypePackable(Type field_type); |
812 | |
813 | // Returns full_name() except if the field is a MessageSet extension, |
814 | // in which case it returns the full_name() of the containing message type |
815 | // for backwards compatibility with proto1. |
816 | // |
817 | // A MessageSet extension is defined as an optional message extension |
818 | // whose containing type has the message_set_wire_format option set. |
819 | // This should be true of extensions of google.protobuf.bridge.MessageSet; |
820 | // by convention, such extensions are named "message_set_extension". |
821 | // |
822 | // The opposite operation (looking up an extension's FieldDescriptor given |
823 | // its printable name) can be accomplished with |
824 | // message->file()->pool()->FindExtensionByPrintableName(message, name) |
825 | // where the extension extends "message". |
826 | const std::string& PrintableNameForExtension() const; |
827 | |
828 | // Source Location --------------------------------------------------- |
829 | |
830 | // Updates |*out_location| to the source location of the complete |
831 | // extent of this field declaration. Returns false and leaves |
832 | // |*out_location| unchanged iff location information was not available. |
833 | bool GetSourceLocation(SourceLocation* out_location) const; |
834 | |
835 | private: |
836 | typedef FieldOptions OptionsType; |
837 | |
838 | // Allows access to GetLocationPath for annotations. |
839 | friend class io::Printer; |
840 | friend class compiler::cpp::Formatter; |
841 | |
842 | // Fill the json_name field of FieldDescriptorProto. |
843 | void CopyJsonNameTo(FieldDescriptorProto* proto) const; |
844 | |
845 | // See Descriptor::DebugString(). |
846 | void DebugString(int depth, std::string* contents, |
847 | const DebugStringOptions& options) const; |
848 | |
849 | // formats the default value appropriately and returns it as a string. |
850 | // Must have a default value to call this. If quote_string_type is true, then |
851 | // types of CPPTYPE_STRING whill be surrounded by quotes and CEscaped. |
852 | std::string DefaultValueAsString(bool quote_string_type) const; |
853 | |
854 | // Helper function that returns the field type name for DebugString. |
855 | std::string FieldTypeNameDebugString() const; |
856 | |
857 | // Walks up the descriptor tree to generate the source location path |
858 | // to this descriptor from the file root. |
859 | void GetLocationPath(std::vector<int>* output) const; |
860 | |
861 | // Returns true if this is a map message type. |
862 | bool is_map_message_type() const; |
863 | |
864 | const std::string* name_; |
865 | const std::string* full_name_; |
866 | const std::string* lowercase_name_; |
867 | const std::string* camelcase_name_; |
868 | // If has_json_name_ is true, it's the value specified by the user. |
869 | // Otherwise, it has the same value as camelcase_name_. |
870 | const std::string* json_name_; |
871 | const FileDescriptor* file_; |
872 | internal::once_flag* type_once_; |
873 | static void TypeOnceInit(const FieldDescriptor* to_init); |
874 | void InternalTypeOnceInit() const; |
875 | mutable Type type_; |
876 | Label label_; |
877 | bool has_default_value_; |
878 | bool proto3_optional_; |
879 | // Whether the user has specified the json_name field option in the .proto |
880 | // file. |
881 | bool has_json_name_; |
882 | bool is_extension_; |
883 | int number_; |
884 | int index_in_oneof_; |
885 | const Descriptor* containing_type_; |
886 | const OneofDescriptor* containing_oneof_; |
887 | const Descriptor* extension_scope_; |
888 | mutable const Descriptor* message_type_; |
889 | mutable const EnumDescriptor* enum_type_; |
890 | const FieldOptions* options_; |
891 | const std::string* type_name_; |
892 | const std::string* default_value_enum_name_; |
893 | // IMPORTANT: If you add a new field, make sure to search for all instances |
894 | // of Allocate<FieldDescriptor>() and AllocateArray<FieldDescriptor>() in |
895 | // descriptor.cc and update them to initialize the field. |
896 | |
897 | union { |
898 | int32 default_value_int32_; |
899 | int64 default_value_int64_; |
900 | uint32 default_value_uint32_; |
901 | uint64 default_value_uint64_; |
902 | float default_value_float_; |
903 | double default_value_double_; |
904 | bool default_value_bool_; |
905 | |
906 | mutable const EnumValueDescriptor* default_value_enum_; |
907 | const std::string* default_value_string_; |
908 | }; |
909 | |
910 | static const CppType kTypeToCppTypeMap[MAX_TYPE + 1]; |
911 | |
912 | static const char* const kTypeToName[MAX_TYPE + 1]; |
913 | |
914 | static const char* const kCppTypeToName[MAX_CPPTYPE + 1]; |
915 | |
916 | static const char* const kLabelToName[MAX_LABEL + 1]; |
917 | |
918 | // Must be constructed using DescriptorPool. |
919 | FieldDescriptor() {} |
920 | friend class DescriptorBuilder; |
921 | friend class FileDescriptor; |
922 | friend class Descriptor; |
923 | friend class OneofDescriptor; |
924 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldDescriptor); |
925 | }; |
926 | |
927 | |
928 | // Describes a oneof defined in a message type. |
929 | class PROTOBUF_EXPORT OneofDescriptor { |
930 | public: |
931 | typedef OneofDescriptorProto Proto; |
932 | |
933 | const std::string& name() const; // Name of this oneof. |
934 | const std::string& full_name() const; // Fully-qualified name of the oneof. |
935 | |
936 | // Index of this oneof within the message's oneof array. |
937 | int index() const; |
938 | |
939 | // Returns whether this oneof was inserted by the compiler to wrap a proto3 |
940 | // optional field. If this returns true, code generators should *not* emit it. |
941 | bool is_synthetic() const; |
942 | |
943 | // The .proto file in which this oneof was defined. Never nullptr. |
944 | const FileDescriptor* file() const; |
945 | // The Descriptor for the message containing this oneof. |
946 | const Descriptor* containing_type() const; |
947 | |
948 | // The number of (non-extension) fields which are members of this oneof. |
949 | int field_count() const; |
950 | // Get a member of this oneof, in the order in which they were declared in the |
951 | // .proto file. Does not include extensions. |
952 | const FieldDescriptor* field(int index) const; |
953 | |
954 | const OneofOptions& options() const; |
955 | |
956 | // See Descriptor::CopyTo(). |
957 | void CopyTo(OneofDescriptorProto* proto) const; |
958 | |
959 | // See Descriptor::DebugString(). |
960 | std::string DebugString() const; |
961 | |
962 | // See Descriptor::DebugStringWithOptions(). |
963 | std::string DebugStringWithOptions(const DebugStringOptions& options) const; |
964 | |
965 | // Source Location --------------------------------------------------- |
966 | |
967 | // Updates |*out_location| to the source location of the complete |
968 | // extent of this oneof declaration. Returns false and leaves |
969 | // |*out_location| unchanged iff location information was not available. |
970 | bool GetSourceLocation(SourceLocation* out_location) const; |
971 | |
972 | private: |
973 | typedef OneofOptions OptionsType; |
974 | |
975 | // Allows access to GetLocationPath for annotations. |
976 | friend class io::Printer; |
977 | friend class compiler::cpp::Formatter; |
978 | |
979 | // See Descriptor::DebugString(). |
980 | void DebugString(int depth, std::string* contents, |
981 | const DebugStringOptions& options) const; |
982 | |
983 | // Walks up the descriptor tree to generate the source location path |
984 | // to this descriptor from the file root. |
985 | void GetLocationPath(std::vector<int>* output) const; |
986 | |
987 | const std::string* name_; |
988 | const std::string* full_name_; |
989 | const Descriptor* containing_type_; |
990 | int field_count_; |
991 | const FieldDescriptor** fields_; |
992 | const OneofOptions* options_; |
993 | |
994 | // IMPORTANT: If you add a new field, make sure to search for all instances |
995 | // of Allocate<OneofDescriptor>() and AllocateArray<OneofDescriptor>() |
996 | // in descriptor.cc and update them to initialize the field. |
997 | |
998 | // Must be constructed using DescriptorPool. |
999 | OneofDescriptor() {} |
1000 | friend class DescriptorBuilder; |
1001 | friend class Descriptor; |
1002 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(OneofDescriptor); |
1003 | }; |
1004 | |
1005 | // Describes an enum type defined in a .proto file. To get the EnumDescriptor |
1006 | // for a generated enum type, call TypeName_descriptor(). Use DescriptorPool |
1007 | // to construct your own descriptors. |
1008 | class PROTOBUF_EXPORT EnumDescriptor { |
1009 | public: |
1010 | typedef EnumDescriptorProto Proto; |
1011 | |
1012 | // The name of this enum type in the containing scope. |
1013 | const std::string& name() const; |
1014 | |
1015 | // The fully-qualified name of the enum type, scope delimited by periods. |
1016 | const std::string& full_name() const; |
1017 | |
1018 | // Index of this enum within the file or containing message's enum array. |
1019 | int index() const; |
1020 | |
1021 | // The .proto file in which this enum type was defined. Never nullptr. |
1022 | const FileDescriptor* file() const; |
1023 | |
1024 | // The number of values for this EnumDescriptor. Guaranteed to be greater |
1025 | // than zero. |
1026 | int value_count() const; |
1027 | // Gets a value by index, where 0 <= index < value_count(). |
1028 | // These are returned in the order they were defined in the .proto file. |
1029 | const EnumValueDescriptor* value(int index) const; |
1030 | |
1031 | // Looks up a value by name. Returns nullptr if no such value exists. |
1032 | const EnumValueDescriptor* FindValueByName(const std::string& name) const; |
1033 | // Looks up a value by number. Returns nullptr if no such value exists. If |
1034 | // multiple values have this number, the first one defined is returned. |
1035 | const EnumValueDescriptor* FindValueByNumber(int number) const; |
1036 | |
1037 | // If this enum type is nested in a message type, this is that message type. |
1038 | // Otherwise, nullptr. |
1039 | const Descriptor* containing_type() const; |
1040 | |
1041 | // Get options for this enum type. These are specified in the .proto file by |
1042 | // placing lines like "option foo = 1234;" in the enum definition. Allowed |
1043 | // options are defined by EnumOptions in descriptor.proto, and any available |
1044 | // extensions of that message. |
1045 | const EnumOptions& options() const; |
1046 | |
1047 | // See Descriptor::CopyTo(). |
1048 | void CopyTo(EnumDescriptorProto* proto) const; |
1049 | |
1050 | // See Descriptor::DebugString(). |
1051 | std::string DebugString() const; |
1052 | |
1053 | // See Descriptor::DebugStringWithOptions(). |
1054 | std::string DebugStringWithOptions(const DebugStringOptions& options) const; |
1055 | |
1056 | // Returns true if this is a placeholder for an unknown enum. This will |
1057 | // only be the case if this descriptor comes from a DescriptorPool |
1058 | // with AllowUnknownDependencies() set. |
1059 | bool is_placeholder() const; |
1060 | |
1061 | // Reserved fields ------------------------------------------------- |
1062 | |
1063 | // A range of reserved field numbers. |
1064 | struct ReservedRange { |
1065 | int start; // inclusive |
1066 | int end; // inclusive |
1067 | }; |
1068 | |
1069 | // The number of reserved ranges in this message type. |
1070 | int reserved_range_count() const; |
1071 | // Gets an reserved range by index, where 0 <= index < |
1072 | // reserved_range_count(). These are returned in the order they were defined |
1073 | // in the .proto file. |
1074 | const EnumDescriptor::ReservedRange* reserved_range(int index) const; |
1075 | |
1076 | // Returns true if the number is in one of the reserved ranges. |
1077 | bool IsReservedNumber(int number) const; |
1078 | |
1079 | // Returns nullptr if no reserved range contains the given number. |
1080 | const EnumDescriptor::ReservedRange* FindReservedRangeContainingNumber( |
1081 | int number) const; |
1082 | |
1083 | // The number of reserved field names in this message type. |
1084 | int reserved_name_count() const; |
1085 | |
1086 | // Gets a reserved name by index, where 0 <= index < reserved_name_count(). |
1087 | const std::string& reserved_name(int index) const; |
1088 | |
1089 | // Returns true if the field name is reserved. |
1090 | bool IsReservedName(const std::string& name) const; |
1091 | |
1092 | // Source Location --------------------------------------------------- |
1093 | |
1094 | // Updates |*out_location| to the source location of the complete |
1095 | // extent of this enum declaration. Returns false and leaves |
1096 | // |*out_location| unchanged iff location information was not available. |
1097 | bool GetSourceLocation(SourceLocation* out_location) const; |
1098 | |
1099 | private: |
1100 | typedef EnumOptions OptionsType; |
1101 | |
1102 | // Allows access to GetLocationPath for annotations. |
1103 | friend class io::Printer; |
1104 | friend class compiler::cpp::Formatter; |
1105 | |
1106 | // Looks up a value by number. If the value does not exist, dynamically |
1107 | // creates a new EnumValueDescriptor for that value, assuming that it was |
1108 | // unknown. If a new descriptor is created, this is done in a thread-safe way, |
1109 | // and future calls will return the same value descriptor pointer. |
1110 | // |
1111 | // This is private but is used by Reflection (which is friended below) to |
1112 | // return a valid EnumValueDescriptor from GetEnum() when this feature is |
1113 | // enabled. |
1114 | const EnumValueDescriptor* FindValueByNumberCreatingIfUnknown( |
1115 | int number) const; |
1116 | |
1117 | // See Descriptor::DebugString(). |
1118 | void DebugString(int depth, std::string* contents, |
1119 | const DebugStringOptions& options) const; |
1120 | |
1121 | // Walks up the descriptor tree to generate the source location path |
1122 | // to this descriptor from the file root. |
1123 | void GetLocationPath(std::vector<int>* output) const; |
1124 | |
1125 | const std::string* name_; |
1126 | const std::string* full_name_; |
1127 | const FileDescriptor* file_; |
1128 | const Descriptor* containing_type_; |
1129 | const EnumOptions* options_; |
1130 | |
1131 | // True if this is a placeholder for an unknown type. |
1132 | bool is_placeholder_; |
1133 | // True if this is a placeholder and the type name wasn't fully-qualified. |
1134 | bool is_unqualified_placeholder_; |
1135 | |
1136 | int value_count_; |
1137 | EnumValueDescriptor* values_; |
1138 | |
1139 | int reserved_range_count_; |
1140 | int reserved_name_count_; |
1141 | EnumDescriptor::ReservedRange* reserved_ranges_; |
1142 | const std::string** reserved_names_; |
1143 | |
1144 | // IMPORTANT: If you add a new field, make sure to search for all instances |
1145 | // of Allocate<EnumDescriptor>() and AllocateArray<EnumDescriptor>() in |
1146 | // descriptor.cc and update them to initialize the field. |
1147 | |
1148 | // Must be constructed using DescriptorPool. |
1149 | EnumDescriptor() {} |
1150 | friend class DescriptorBuilder; |
1151 | friend class Descriptor; |
1152 | friend class FieldDescriptor; |
1153 | friend class EnumValueDescriptor; |
1154 | friend class FileDescriptor; |
1155 | friend class DescriptorPool; |
1156 | friend class Reflection; |
1157 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumDescriptor); |
1158 | }; |
1159 | |
1160 | // Describes an individual enum constant of a particular type. To get the |
1161 | // EnumValueDescriptor for a given enum value, first get the EnumDescriptor |
1162 | // for its type, then use EnumDescriptor::FindValueByName() or |
1163 | // EnumDescriptor::FindValueByNumber(). Use DescriptorPool to construct |
1164 | // your own descriptors. |
1165 | class PROTOBUF_EXPORT EnumValueDescriptor { |
1166 | public: |
1167 | typedef EnumValueDescriptorProto Proto; |
1168 | |
1169 | const std::string& name() const; // Name of this enum constant. |
1170 | int index() const; // Index within the enums's Descriptor. |
1171 | int number() const; // Numeric value of this enum constant. |
1172 | |
1173 | // The full_name of an enum value is a sibling symbol of the enum type. |
1174 | // e.g. the full name of FieldDescriptorProto::TYPE_INT32 is actually |
1175 | // "google.protobuf.FieldDescriptorProto.TYPE_INT32", NOT |
1176 | // "google.protobuf.FieldDescriptorProto.Type.TYPE_INT32". This is to conform |
1177 | // with C++ scoping rules for enums. |
1178 | const std::string& full_name() const; |
1179 | |
1180 | // The .proto file in which this value was defined. Never nullptr. |
1181 | const FileDescriptor* file() const; |
1182 | // The type of this value. Never nullptr. |
1183 | const EnumDescriptor* type() const; |
1184 | |
1185 | // Get options for this enum value. These are specified in the .proto file by |
1186 | // adding text like "[foo = 1234]" after an enum value definition. Allowed |
1187 | // options are defined by EnumValueOptions in descriptor.proto, and any |
1188 | // available extensions of that message. |
1189 | const EnumValueOptions& options() const; |
1190 | |
1191 | // See Descriptor::CopyTo(). |
1192 | void CopyTo(EnumValueDescriptorProto* proto) const; |
1193 | |
1194 | // See Descriptor::DebugString(). |
1195 | std::string DebugString() const; |
1196 | |
1197 | // See Descriptor::DebugStringWithOptions(). |
1198 | std::string DebugStringWithOptions(const DebugStringOptions& options) const; |
1199 | |
1200 | // Source Location --------------------------------------------------- |
1201 | |
1202 | // Updates |*out_location| to the source location of the complete |
1203 | // extent of this enum value declaration. Returns false and leaves |
1204 | // |*out_location| unchanged iff location information was not available. |
1205 | bool GetSourceLocation(SourceLocation* out_location) const; |
1206 | |
1207 | private: |
1208 | typedef EnumValueOptions OptionsType; |
1209 | |
1210 | // Allows access to GetLocationPath for annotations. |
1211 | friend class io::Printer; |
1212 | friend class compiler::cpp::Formatter; |
1213 | |
1214 | // See Descriptor::DebugString(). |
1215 | void DebugString(int depth, std::string* contents, |
1216 | const DebugStringOptions& options) const; |
1217 | |
1218 | // Walks up the descriptor tree to generate the source location path |
1219 | // to this descriptor from the file root. |
1220 | void GetLocationPath(std::vector<int>* output) const; |
1221 | |
1222 | const std::string* name_; |
1223 | const std::string* full_name_; |
1224 | int number_; |
1225 | const EnumDescriptor* type_; |
1226 | const EnumValueOptions* options_; |
1227 | // IMPORTANT: If you add a new field, make sure to search for all instances |
1228 | // of Allocate<EnumValueDescriptor>() and AllocateArray<EnumValueDescriptor>() |
1229 | // in descriptor.cc and update them to initialize the field. |
1230 | |
1231 | // Must be constructed using DescriptorPool. |
1232 | EnumValueDescriptor() {} |
1233 | friend class DescriptorBuilder; |
1234 | friend class EnumDescriptor; |
1235 | friend class DescriptorPool; |
1236 | friend class FileDescriptorTables; |
1237 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumValueDescriptor); |
1238 | }; |
1239 | |
1240 | // Describes an RPC service. Use DescriptorPool to construct your own |
1241 | // descriptors. |
1242 | class PROTOBUF_EXPORT ServiceDescriptor { |
1243 | public: |
1244 | typedef ServiceDescriptorProto Proto; |
1245 | |
1246 | // The name of the service, not including its containing scope. |
1247 | const std::string& name() const; |
1248 | // The fully-qualified name of the service, scope delimited by periods. |
1249 | const std::string& full_name() const; |
1250 | // Index of this service within the file's services array. |
1251 | int index() const; |
1252 | |
1253 | // The .proto file in which this service was defined. Never nullptr. |
1254 | const FileDescriptor* file() const; |
1255 | |
1256 | // Get options for this service type. These are specified in the .proto file |
1257 | // by placing lines like "option foo = 1234;" in the service definition. |
1258 | // Allowed options are defined by ServiceOptions in descriptor.proto, and any |
1259 | // available extensions of that message. |
1260 | const ServiceOptions& options() const; |
1261 | |
1262 | // The number of methods this service defines. |
1263 | int method_count() const; |
1264 | // Gets a MethodDescriptor by index, where 0 <= index < method_count(). |
1265 | // These are returned in the order they were defined in the .proto file. |
1266 | const MethodDescriptor* method(int index) const; |
1267 | |
1268 | // Look up a MethodDescriptor by name. |
1269 | const MethodDescriptor* FindMethodByName(const std::string& name) const; |
1270 | // See Descriptor::CopyTo(). |
1271 | void CopyTo(ServiceDescriptorProto* proto) const; |
1272 | |
1273 | // See Descriptor::DebugString(). |
1274 | std::string DebugString() const; |
1275 | |
1276 | // See Descriptor::DebugStringWithOptions(). |
1277 | std::string DebugStringWithOptions(const DebugStringOptions& options) const; |
1278 | |
1279 | // Source Location --------------------------------------------------- |
1280 | |
1281 | // Updates |*out_location| to the source location of the complete |
1282 | // extent of this service declaration. Returns false and leaves |
1283 | // |*out_location| unchanged iff location information was not available. |
1284 | bool GetSourceLocation(SourceLocation* out_location) const; |
1285 | |
1286 | private: |
1287 | typedef ServiceOptions OptionsType; |
1288 | |
1289 | // Allows access to GetLocationPath for annotations. |
1290 | friend class io::Printer; |
1291 | friend class compiler::cpp::Formatter; |
1292 | |
1293 | // See Descriptor::DebugString(). |
1294 | void DebugString(std::string* contents, |
1295 | const DebugStringOptions& options) const; |
1296 | |
1297 | // Walks up the descriptor tree to generate the source location path |
1298 | // to this descriptor from the file root. |
1299 | void GetLocationPath(std::vector<int>* output) const; |
1300 | |
1301 | const std::string* name_; |
1302 | const std::string* full_name_; |
1303 | const FileDescriptor* file_; |
1304 | const ServiceOptions* options_; |
1305 | MethodDescriptor* methods_; |
1306 | int method_count_; |
1307 | // IMPORTANT: If you add a new field, make sure to search for all instances |
1308 | // of Allocate<ServiceDescriptor>() and AllocateArray<ServiceDescriptor>() in |
1309 | // descriptor.cc and update them to initialize the field. |
1310 | |
1311 | // Must be constructed using DescriptorPool. |
1312 | ServiceDescriptor() {} |
1313 | friend class DescriptorBuilder; |
1314 | friend class FileDescriptor; |
1315 | friend class MethodDescriptor; |
1316 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ServiceDescriptor); |
1317 | }; |
1318 | |
1319 | |
1320 | // Describes an individual service method. To obtain a MethodDescriptor given |
1321 | // a service, first get its ServiceDescriptor, then call |
1322 | // ServiceDescriptor::FindMethodByName(). Use DescriptorPool to construct your |
1323 | // own descriptors. |
1324 | class PROTOBUF_EXPORT MethodDescriptor { |
1325 | public: |
1326 | typedef MethodDescriptorProto Proto; |
1327 | |
1328 | // Name of this method, not including containing scope. |
1329 | const std::string& name() const; |
1330 | // The fully-qualified name of the method, scope delimited by periods. |
1331 | const std::string& full_name() const; |
1332 | // Index within the service's Descriptor. |
1333 | int index() const; |
1334 | |
1335 | // The .proto file in which this method was defined. Never nullptr. |
1336 | const FileDescriptor* file() const; |
1337 | // Gets the service to which this method belongs. Never nullptr. |
1338 | const ServiceDescriptor* service() const; |
1339 | |
1340 | // Gets the type of protocol message which this method accepts as input. |
1341 | const Descriptor* input_type() const; |
1342 | // Gets the type of protocol message which this message produces as output. |
1343 | const Descriptor* output_type() const; |
1344 | |
1345 | // Gets whether the client streams multiple requests. |
1346 | bool client_streaming() const; |
1347 | // Gets whether the server streams multiple responses. |
1348 | bool server_streaming() const; |
1349 | |
1350 | // Get options for this method. These are specified in the .proto file by |
1351 | // placing lines like "option foo = 1234;" in curly-braces after a method |
1352 | // declaration. Allowed options are defined by MethodOptions in |
1353 | // descriptor.proto, and any available extensions of that message. |
1354 | const MethodOptions& options() const; |
1355 | |
1356 | // See Descriptor::CopyTo(). |
1357 | void CopyTo(MethodDescriptorProto* proto) const; |
1358 | |
1359 | // See Descriptor::DebugString(). |
1360 | std::string DebugString() const; |
1361 | |
1362 | // See Descriptor::DebugStringWithOptions(). |
1363 | std::string DebugStringWithOptions(const DebugStringOptions& options) const; |
1364 | |
1365 | // Source Location --------------------------------------------------- |
1366 | |
1367 | // Updates |*out_location| to the source location of the complete |
1368 | // extent of this method declaration. Returns false and leaves |
1369 | // |*out_location| unchanged iff location information was not available. |
1370 | bool GetSourceLocation(SourceLocation* out_location) const; |
1371 | |
1372 | private: |
1373 | typedef MethodOptions OptionsType; |
1374 | |
1375 | // Allows access to GetLocationPath for annotations. |
1376 | friend class io::Printer; |
1377 | friend class compiler::cpp::Formatter; |
1378 | |
1379 | // See Descriptor::DebugString(). |
1380 | void DebugString(int depth, std::string* contents, |
1381 | const DebugStringOptions& options) const; |
1382 | |
1383 | // Walks up the descriptor tree to generate the source location path |
1384 | // to this descriptor from the file root. |
1385 | void GetLocationPath(std::vector<int>* output) const; |
1386 | |
1387 | const std::string* name_; |
1388 | const std::string* full_name_; |
1389 | const ServiceDescriptor* service_; |
1390 | mutable internal::LazyDescriptor input_type_; |
1391 | mutable internal::LazyDescriptor output_type_; |
1392 | const MethodOptions* options_; |
1393 | bool client_streaming_; |
1394 | bool server_streaming_; |
1395 | // IMPORTANT: If you add a new field, make sure to search for all instances |
1396 | // of Allocate<MethodDescriptor>() and AllocateArray<MethodDescriptor>() in |
1397 | // descriptor.cc and update them to initialize the field. |
1398 | |
1399 | // Must be constructed using DescriptorPool. |
1400 | MethodDescriptor() {} |
1401 | friend class DescriptorBuilder; |
1402 | friend class ServiceDescriptor; |
1403 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MethodDescriptor); |
1404 | }; |
1405 | |
1406 | |
1407 | // Describes a whole .proto file. To get the FileDescriptor for a compiled-in |
1408 | // file, get the descriptor for something defined in that file and call |
1409 | // descriptor->file(). Use DescriptorPool to construct your own descriptors. |
1410 | class PROTOBUF_EXPORT FileDescriptor { |
1411 | public: |
1412 | typedef FileDescriptorProto Proto; |
1413 | |
1414 | // The filename, relative to the source tree. |
1415 | // e.g. "foo/bar/baz.proto" |
1416 | const std::string& name() const; |
1417 | |
1418 | // The package, e.g. "google.protobuf.compiler". |
1419 | const std::string& package() const; |
1420 | |
1421 | // The DescriptorPool in which this FileDescriptor and all its contents were |
1422 | // allocated. Never nullptr. |
1423 | const DescriptorPool* pool() const; |
1424 | |
1425 | // The number of files imported by this one. |
1426 | int dependency_count() const; |
1427 | // Gets an imported file by index, where 0 <= index < dependency_count(). |
1428 | // These are returned in the order they were defined in the .proto file. |
1429 | const FileDescriptor* dependency(int index) const; |
1430 | |
1431 | // The number of files public imported by this one. |
1432 | // The public dependency list is a subset of the dependency list. |
1433 | int public_dependency_count() const; |
1434 | // Gets a public imported file by index, where 0 <= index < |
1435 | // public_dependency_count(). |
1436 | // These are returned in the order they were defined in the .proto file. |
1437 | const FileDescriptor* public_dependency(int index) const; |
1438 | |
1439 | // The number of files that are imported for weak fields. |
1440 | // The weak dependency list is a subset of the dependency list. |
1441 | int weak_dependency_count() const; |
1442 | // Gets a weak imported file by index, where 0 <= index < |
1443 | // weak_dependency_count(). |
1444 | // These are returned in the order they were defined in the .proto file. |
1445 | const FileDescriptor* weak_dependency(int index) const; |
1446 | |
1447 | // Number of top-level message types defined in this file. (This does not |
1448 | // include nested types.) |
1449 | int message_type_count() const; |
1450 | // Gets a top-level message type, where 0 <= index < message_type_count(). |
1451 | // These are returned in the order they were defined in the .proto file. |
1452 | const Descriptor* message_type(int index) const; |
1453 | |
1454 | // Number of top-level enum types defined in this file. (This does not |
1455 | // include nested types.) |
1456 | int enum_type_count() const; |
1457 | // Gets a top-level enum type, where 0 <= index < enum_type_count(). |
1458 | // These are returned in the order they were defined in the .proto file. |
1459 | const EnumDescriptor* enum_type(int index) const; |
1460 | |
1461 | // Number of services defined in this file. |
1462 | int service_count() const; |
1463 | // Gets a service, where 0 <= index < service_count(). |
1464 | // These are returned in the order they were defined in the .proto file. |
1465 | const ServiceDescriptor* service(int index) const; |
1466 | |
1467 | // Number of extensions defined at file scope. (This does not include |
1468 | // extensions nested within message types.) |
1469 | int extension_count() const; |
1470 | // Gets an extension's descriptor, where 0 <= index < extension_count(). |
1471 | // These are returned in the order they were defined in the .proto file. |
1472 | const FieldDescriptor* extension(int index) const; |
1473 | |
1474 | // Get options for this file. These are specified in the .proto file by |
1475 | // placing lines like "option foo = 1234;" at the top level, outside of any |
1476 | // other definitions. Allowed options are defined by FileOptions in |
1477 | // descriptor.proto, and any available extensions of that message. |
1478 | const FileOptions& options() const; |
1479 | |
1480 | // Syntax of this file. |
1481 | enum Syntax { |
1482 | SYNTAX_UNKNOWN = 0, |
1483 | SYNTAX_PROTO2 = 2, |
1484 | SYNTAX_PROTO3 = 3, |
1485 | }; |
1486 | Syntax syntax() const; |
1487 | static const char* SyntaxName(Syntax syntax); |
1488 | |
1489 | // Find a top-level message type by name. Returns nullptr if not found. |
1490 | const Descriptor* FindMessageTypeByName(const std::string& name) const; |
1491 | // Find a top-level enum type by name. Returns nullptr if not found. |
1492 | const EnumDescriptor* FindEnumTypeByName(const std::string& name) const; |
1493 | // Find an enum value defined in any top-level enum by name. Returns nullptr |
1494 | // if not found. |
1495 | const EnumValueDescriptor* FindEnumValueByName(const std::string& name) const; |
1496 | // Find a service definition by name. Returns nullptr if not found. |
1497 | const ServiceDescriptor* FindServiceByName(const std::string& name) const; |
1498 | // Find a top-level extension definition by name. Returns nullptr if not |
1499 | // found. |
1500 | const FieldDescriptor* FindExtensionByName(const std::string& name) const; |
1501 | // Similar to FindExtensionByName(), but searches by lowercased-name. See |
1502 | // Descriptor::FindFieldByLowercaseName(). |
1503 | const FieldDescriptor* FindExtensionByLowercaseName( |
1504 | const std::string& name) const; |
1505 | // Similar to FindExtensionByName(), but searches by camelcased-name. See |
1506 | // Descriptor::FindFieldByCamelcaseName(). |
1507 | const FieldDescriptor* FindExtensionByCamelcaseName( |
1508 | const std::string& name) const; |
1509 | |
1510 | // See Descriptor::CopyTo(). |
1511 | // Notes: |
1512 | // - This method does NOT copy source code information since it is relatively |
1513 | // large and rarely needed. See CopySourceCodeInfoTo() below. |
1514 | void CopyTo(FileDescriptorProto* proto) const; |
1515 | // Write the source code information of this FileDescriptor into the given |
1516 | // FileDescriptorProto. See CopyTo() above. |
1517 | void CopySourceCodeInfoTo(FileDescriptorProto* proto) const; |
1518 | // Fill the json_name field of FieldDescriptorProto for all fields. Can only |
1519 | // be called after CopyTo(). |
1520 | void CopyJsonNameTo(FileDescriptorProto* proto) const; |
1521 | |
1522 | // See Descriptor::DebugString(). |
1523 | std::string DebugString() const; |
1524 | |
1525 | // See Descriptor::DebugStringWithOptions(). |
1526 | std::string DebugStringWithOptions(const DebugStringOptions& options) const; |
1527 | |
1528 | // Returns true if this is a placeholder for an unknown file. This will |
1529 | // only be the case if this descriptor comes from a DescriptorPool |
1530 | // with AllowUnknownDependencies() set. |
1531 | bool is_placeholder() const; |
1532 | |
1533 | // Updates |*out_location| to the source location of the complete extent of |
1534 | // this file declaration (namely, the empty path). |
1535 | bool GetSourceLocation(SourceLocation* out_location) const; |
1536 | |
1537 | // Updates |*out_location| to the source location of the complete |
1538 | // extent of the declaration or declaration-part denoted by |path|. |
1539 | // Returns false and leaves |*out_location| unchanged iff location |
1540 | // information was not available. (See SourceCodeInfo for |
1541 | // description of path encoding.) |
1542 | bool GetSourceLocation(const std::vector<int>& path, |
1543 | SourceLocation* out_location) const; |
1544 | |
1545 | private: |
1546 | typedef FileOptions OptionsType; |
1547 | |
1548 | const std::string* name_; |
1549 | const std::string* package_; |
1550 | const DescriptorPool* pool_; |
1551 | internal::once_flag* dependencies_once_; |
1552 | static void DependenciesOnceInit(const FileDescriptor* to_init); |
1553 | void InternalDependenciesOnceInit() const; |
1554 | |
1555 | // These are arranged to minimize padding on 64-bit. |
1556 | int dependency_count_; |
1557 | int public_dependency_count_; |
1558 | int weak_dependency_count_; |
1559 | int message_type_count_; |
1560 | int enum_type_count_; |
1561 | int service_count_; |
1562 | int extension_count_; |
1563 | Syntax syntax_; |
1564 | bool is_placeholder_; |
1565 | |
1566 | // Indicates the FileDescriptor is completed building. Used to verify |
1567 | // that type accessor functions that can possibly build a dependent file |
1568 | // aren't called during the process of building the file. |
1569 | bool finished_building_; |
1570 | |
1571 | mutable const FileDescriptor** dependencies_; |
1572 | const std::string** dependencies_names_; |
1573 | int* public_dependencies_; |
1574 | int* weak_dependencies_; |
1575 | Descriptor* message_types_; |
1576 | EnumDescriptor* enum_types_; |
1577 | ServiceDescriptor* services_; |
1578 | FieldDescriptor* extensions_; |
1579 | const FileOptions* options_; |
1580 | |
1581 | const FileDescriptorTables* tables_; |
1582 | const SourceCodeInfo* source_code_info_; |
1583 | |
1584 | // IMPORTANT: If you add a new field, make sure to search for all instances |
1585 | // of Allocate<FileDescriptor>() and AllocateArray<FileDescriptor>() in |
1586 | // descriptor.cc and update them to initialize the field. |
1587 | |
1588 | FileDescriptor() {} |
1589 | friend class DescriptorBuilder; |
1590 | friend class DescriptorPool; |
1591 | friend class Descriptor; |
1592 | friend class FieldDescriptor; |
1593 | friend class internal::LazyDescriptor; |
1594 | friend class OneofDescriptor; |
1595 | friend class EnumDescriptor; |
1596 | friend class EnumValueDescriptor; |
1597 | friend class MethodDescriptor; |
1598 | friend class ServiceDescriptor; |
1599 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileDescriptor); |
1600 | }; |
1601 | |
1602 | |
1603 | // =================================================================== |
1604 | |
1605 | // Used to construct descriptors. |
1606 | // |
1607 | // Normally you won't want to build your own descriptors. Message classes |
1608 | // constructed by the protocol compiler will provide them for you. However, |
1609 | // if you are implementing Message on your own, or if you are writing a |
1610 | // program which can operate on totally arbitrary types and needs to load |
1611 | // them from some sort of database, you might need to. |
1612 | // |
1613 | // Since Descriptors are composed of a whole lot of cross-linked bits of |
1614 | // data that would be a pain to put together manually, the |
1615 | // DescriptorPool class is provided to make the process easier. It can |
1616 | // take a FileDescriptorProto (defined in descriptor.proto), validate it, |
1617 | // and convert it to a set of nicely cross-linked Descriptors. |
1618 | // |
1619 | // DescriptorPool also helps with memory management. Descriptors are |
1620 | // composed of many objects containing static data and pointers to each |
1621 | // other. In all likelihood, when it comes time to delete this data, |
1622 | // you'll want to delete it all at once. In fact, it is not uncommon to |
1623 | // have a whole pool of descriptors all cross-linked with each other which |
1624 | // you wish to delete all at once. This class represents such a pool, and |
1625 | // handles the memory management for you. |
1626 | // |
1627 | // You can also search for descriptors within a DescriptorPool by name, and |
1628 | // extensions by number. |
1629 | class PROTOBUF_EXPORT DescriptorPool { |
1630 | public: |
1631 | // Create a normal, empty DescriptorPool. |
1632 | DescriptorPool(); |
1633 | |
1634 | // Constructs a DescriptorPool that, when it can't find something among the |
1635 | // descriptors already in the pool, looks for it in the given |
1636 | // DescriptorDatabase. |
1637 | // Notes: |
1638 | // - If a DescriptorPool is constructed this way, its BuildFile*() methods |
1639 | // must not be called (they will assert-fail). The only way to populate |
1640 | // the pool with descriptors is to call the Find*By*() methods. |
1641 | // - The Find*By*() methods may block the calling thread if the |
1642 | // DescriptorDatabase blocks. This in turn means that parsing messages |
1643 | // may block if they need to look up extensions. |
1644 | // - The Find*By*() methods will use mutexes for thread-safety, thus making |
1645 | // them slower even when they don't have to fall back to the database. |
1646 | // In fact, even the Find*By*() methods of descriptor objects owned by |
1647 | // this pool will be slower, since they will have to obtain locks too. |
1648 | // - An ErrorCollector may optionally be given to collect validation errors |
1649 | // in files loaded from the database. If not given, errors will be printed |
1650 | // to GOOGLE_LOG(ERROR). Remember that files are built on-demand, so this |
1651 | // ErrorCollector may be called from any thread that calls one of the |
1652 | // Find*By*() methods. |
1653 | // - The DescriptorDatabase must not be mutated during the lifetime of |
1654 | // the DescriptorPool. Even if the client takes care to avoid data races, |
1655 | // changes to the content of the DescriptorDatabase may not be reflected |
1656 | // in subsequent lookups in the DescriptorPool. |
1657 | class ErrorCollector; |
1658 | explicit DescriptorPool(DescriptorDatabase* fallback_database, |
1659 | ErrorCollector* error_collector = nullptr); |
1660 | |
1661 | ~DescriptorPool(); |
1662 | |
1663 | // Get a pointer to the generated pool. Generated protocol message classes |
1664 | // which are compiled into the binary will allocate their descriptors in |
1665 | // this pool. Do not add your own descriptors to this pool. |
1666 | static const DescriptorPool* generated_pool(); |
1667 | |
1668 | |
1669 | // Find a FileDescriptor in the pool by file name. Returns nullptr if not |
1670 | // found. |
1671 | const FileDescriptor* FindFileByName(const std::string& name) const; |
1672 | |
1673 | // Find the FileDescriptor in the pool which defines the given symbol. |
1674 | // If any of the Find*ByName() methods below would succeed, then this is |
1675 | // equivalent to calling that method and calling the result's file() method. |
1676 | // Otherwise this returns nullptr. |
1677 | const FileDescriptor* FindFileContainingSymbol( |
1678 | const std::string& symbol_name) const; |
1679 | |
1680 | // Looking up descriptors ------------------------------------------ |
1681 | // These find descriptors by fully-qualified name. These will find both |
1682 | // top-level descriptors and nested descriptors. They return nullptr if not |
1683 | // found. |
1684 | |
1685 | const Descriptor* FindMessageTypeByName(const std::string& name) const; |
1686 | const FieldDescriptor* FindFieldByName(const std::string& name) const; |
1687 | const FieldDescriptor* FindExtensionByName(const std::string& name) const; |
1688 | const OneofDescriptor* FindOneofByName(const std::string& name) const; |
1689 | const EnumDescriptor* FindEnumTypeByName(const std::string& name) const; |
1690 | const EnumValueDescriptor* FindEnumValueByName(const std::string& name) const; |
1691 | const ServiceDescriptor* FindServiceByName(const std::string& name) const; |
1692 | const MethodDescriptor* FindMethodByName(const std::string& name) const; |
1693 | |
1694 | // Finds an extension of the given type by number. The extendee must be |
1695 | // a member of this DescriptorPool or one of its underlays. |
1696 | const FieldDescriptor* FindExtensionByNumber(const Descriptor* extendee, |
1697 | int number) const; |
1698 | |
1699 | // Finds an extension of the given type by its printable name. |
1700 | // See comments above PrintableNameForExtension() for the definition of |
1701 | // "printable name". The extendee must be a member of this DescriptorPool |
1702 | // or one of its underlays. Returns nullptr if there is no known message |
1703 | // extension with the given printable name. |
1704 | const FieldDescriptor* FindExtensionByPrintableName( |
1705 | const Descriptor* extendee, const std::string& printable_name) const; |
1706 | |
1707 | // Finds extensions of extendee. The extensions will be appended to |
1708 | // out in an undefined order. Only extensions defined directly in |
1709 | // this DescriptorPool or one of its underlays are guaranteed to be |
1710 | // found: extensions defined in the fallback database might not be found |
1711 | // depending on the database implementation. |
1712 | void FindAllExtensions(const Descriptor* extendee, |
1713 | std::vector<const FieldDescriptor*>* out) const; |
1714 | |
1715 | // Building descriptors -------------------------------------------- |
1716 | |
1717 | // When converting a FileDescriptorProto to a FileDescriptor, various |
1718 | // errors might be detected in the input. The caller may handle these |
1719 | // programmatically by implementing an ErrorCollector. |
1720 | class PROTOBUF_EXPORT ErrorCollector { |
1721 | public: |
1722 | inline ErrorCollector() {} |
1723 | virtual ~ErrorCollector(); |
1724 | |
1725 | // These constants specify what exact part of the construct is broken. |
1726 | // This is useful e.g. for mapping the error back to an exact location |
1727 | // in a .proto file. |
1728 | enum ErrorLocation { |
1729 | NAME, // the symbol name, or the package name for files |
1730 | NUMBER, // field or extension range number |
1731 | TYPE, // field type |
1732 | EXTENDEE, // field extendee |
1733 | DEFAULT_VALUE, // field default value |
1734 | INPUT_TYPE, // method input type |
1735 | OUTPUT_TYPE, // method output type |
1736 | OPTION_NAME, // name in assignment |
1737 | OPTION_VALUE, // value in option assignment |
1738 | IMPORT, // import error |
1739 | OTHER // some other problem |
1740 | }; |
1741 | |
1742 | // Reports an error in the FileDescriptorProto. Use this function if the |
1743 | // problem occurred should interrupt building the FileDescriptorProto. |
1744 | virtual void AddError( |
1745 | const std::string& filename, // File name in which the error occurred. |
1746 | const std::string& element_name, // Full name of the erroneous element. |
1747 | const Message* descriptor, // Descriptor of the erroneous element. |
1748 | ErrorLocation location, // One of the location constants, above. |
1749 | const std::string& message // Human-readable error message. |
1750 | ) = 0; |
1751 | |
1752 | // Reports a warning in the FileDescriptorProto. Use this function if the |
1753 | // problem occurred should NOT interrupt building the FileDescriptorProto. |
1754 | virtual void AddWarning( |
1755 | const std::string& /*filename*/, // File name in which the error |
1756 | // occurred. |
1757 | const std::string& /*element_name*/, // Full name of the erroneous |
1758 | // element. |
1759 | const Message* /*descriptor*/, // Descriptor of the erroneous element. |
1760 | ErrorLocation /*location*/, // One of the location constants, above. |
1761 | const std::string& /*message*/ // Human-readable error message. |
1762 | ) {} |
1763 | |
1764 | private: |
1765 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ErrorCollector); |
1766 | }; |
1767 | |
1768 | // Convert the FileDescriptorProto to real descriptors and place them in |
1769 | // this DescriptorPool. All dependencies of the file must already be in |
1770 | // the pool. Returns the resulting FileDescriptor, or nullptr if there were |
1771 | // problems with the input (e.g. the message was invalid, or dependencies |
1772 | // were missing). Details about the errors are written to GOOGLE_LOG(ERROR). |
1773 | const FileDescriptor* BuildFile(const FileDescriptorProto& proto); |
1774 | |
1775 | // Same as BuildFile() except errors are sent to the given ErrorCollector. |
1776 | const FileDescriptor* BuildFileCollectingErrors( |
1777 | const FileDescriptorProto& proto, ErrorCollector* error_collector); |
1778 | |
1779 | // By default, it is an error if a FileDescriptorProto contains references |
1780 | // to types or other files that are not found in the DescriptorPool (or its |
1781 | // backing DescriptorDatabase, if any). If you call |
1782 | // AllowUnknownDependencies(), however, then unknown types and files |
1783 | // will be replaced by placeholder descriptors (which can be identified by |
1784 | // the is_placeholder() method). This can allow you to |
1785 | // perform some useful operations with a .proto file even if you do not |
1786 | // have access to other .proto files on which it depends. However, some |
1787 | // heuristics must be used to fill in the gaps in information, and these |
1788 | // can lead to descriptors which are inaccurate. For example, the |
1789 | // DescriptorPool may be forced to guess whether an unknown type is a message |
1790 | // or an enum, as well as what package it resides in. Furthermore, |
1791 | // placeholder types will not be discoverable via FindMessageTypeByName() |
1792 | // and similar methods, which could confuse some descriptor-based algorithms. |
1793 | // Generally, the results of this option should be handled with extreme care. |
1794 | void AllowUnknownDependencies() { allow_unknown_ = true; } |
1795 | |
1796 | // By default, weak imports are allowed to be missing, in which case we will |
1797 | // use a placeholder for the dependency and convert the field to be an Empty |
1798 | // message field. If you call EnforceWeakDependencies(true), however, the |
1799 | // DescriptorPool will report a import not found error. |
1800 | void EnforceWeakDependencies(bool enforce) { enforce_weak_ = enforce; } |
1801 | |
1802 | // Internal stuff -------------------------------------------------- |
1803 | // These methods MUST NOT be called from outside the proto2 library. |
1804 | // These methods may contain hidden pitfalls and may be removed in a |
1805 | // future library version. |
1806 | |
1807 | // Create a DescriptorPool which is overlaid on top of some other pool. |
1808 | // If you search for a descriptor in the overlay and it is not found, the |
1809 | // underlay will be searched as a backup. If the underlay has its own |
1810 | // underlay, that will be searched next, and so on. This also means that |
1811 | // files built in the overlay will be cross-linked with the underlay's |
1812 | // descriptors if necessary. The underlay remains property of the caller; |
1813 | // it must remain valid for the lifetime of the newly-constructed pool. |
1814 | // |
1815 | // Example: Say you want to parse a .proto file at runtime in order to use |
1816 | // its type with a DynamicMessage. Say this .proto file has dependencies, |
1817 | // but you know that all the dependencies will be things that are already |
1818 | // compiled into the binary. For ease of use, you'd like to load the types |
1819 | // right out of generated_pool() rather than have to parse redundant copies |
1820 | // of all these .protos and runtime. But, you don't want to add the parsed |
1821 | // types directly into generated_pool(): this is not allowed, and would be |
1822 | // bad design anyway. So, instead, you could use generated_pool() as an |
1823 | // underlay for a new DescriptorPool in which you add only the new file. |
1824 | // |
1825 | // WARNING: Use of underlays can lead to many subtle gotchas. Instead, |
1826 | // try to formulate what you want to do in terms of DescriptorDatabases. |
1827 | explicit DescriptorPool(const DescriptorPool* underlay); |
1828 | |
1829 | // Called by generated classes at init time to add their descriptors to |
1830 | // generated_pool. Do NOT call this in your own code! filename must be a |
1831 | // permanent string (e.g. a string literal). |
1832 | static void InternalAddGeneratedFile(const void* encoded_file_descriptor, |
1833 | int size); |
1834 | |
1835 | // Disallow [enforce_utf8 = false] in .proto files. |
1836 | void DisallowEnforceUtf8() { disallow_enforce_utf8_ = true; } |
1837 | |
1838 | |
1839 | // For internal use only: Gets a non-const pointer to the generated pool. |
1840 | // This is called at static-initialization time only, so thread-safety is |
1841 | // not a concern. If both an underlay and a fallback database are present, |
1842 | // the underlay takes precedence. |
1843 | static DescriptorPool* internal_generated_pool(); |
1844 | |
1845 | // For internal use only: Changes the behavior of BuildFile() such that it |
1846 | // allows the file to make reference to message types declared in other files |
1847 | // which it did not officially declare as dependencies. |
1848 | void InternalDontEnforceDependencies(); |
1849 | |
1850 | // For internal use only: Enables lazy building of dependencies of a file. |
1851 | // Delay the building of dependencies of a file descriptor until absolutely |
1852 | // necessary, like when message_type() is called on a field that is defined |
1853 | // in that dependency's file. This will cause functional issues if a proto |
1854 | // or one of it's dependencies has errors. Should only be enabled for the |
1855 | // generated_pool_ (because no descriptor build errors are guaranteed by |
1856 | // the compilation generation process), testing, or if a lack of descriptor |
1857 | // build errors can be guaranteed for a pool. |
1858 | void InternalSetLazilyBuildDependencies() { |
1859 | lazily_build_dependencies_ = true; |
1860 | // This needs to be set when lazily building dependencies, as it breaks |
1861 | // dependency checking. |
1862 | InternalDontEnforceDependencies(); |
1863 | } |
1864 | |
1865 | // For internal use only. |
1866 | void internal_set_underlay(const DescriptorPool* underlay) { |
1867 | underlay_ = underlay; |
1868 | } |
1869 | |
1870 | // For internal (unit test) use only: Returns true if a FileDescriptor has |
1871 | // been constructed for the given file, false otherwise. Useful for testing |
1872 | // lazy descriptor initialization behavior. |
1873 | bool InternalIsFileLoaded(const std::string& filename) const; |
1874 | |
1875 | // Add a file to unused_import_track_files_. DescriptorBuilder will log |
1876 | // warnings or errors for those files if there is any unused import. |
1877 | void AddUnusedImportTrackFile(const std::string& file_name, |
1878 | bool is_error = false); |
1879 | void ClearUnusedImportTrackFiles(); |
1880 | |
1881 | private: |
1882 | friend class Descriptor; |
1883 | friend class internal::LazyDescriptor; |
1884 | friend class FieldDescriptor; |
1885 | friend class EnumDescriptor; |
1886 | friend class ServiceDescriptor; |
1887 | friend class MethodDescriptor; |
1888 | friend class FileDescriptor; |
1889 | friend class StreamDescriptor; |
1890 | friend class DescriptorBuilder; |
1891 | friend class FileDescriptorTables; |
1892 | |
1893 | // Return true if the given name is a sub-symbol of any non-package |
1894 | // descriptor that already exists in the descriptor pool. (The full |
1895 | // definition of such types is already known.) |
1896 | bool IsSubSymbolOfBuiltType(const std::string& name) const; |
1897 | |
1898 | // Tries to find something in the fallback database and link in the |
1899 | // corresponding proto file. Returns true if successful, in which case |
1900 | // the caller should search for the thing again. These are declared |
1901 | // const because they are called by (semantically) const methods. |
1902 | bool TryFindFileInFallbackDatabase(const std::string& name) const; |
1903 | bool TryFindSymbolInFallbackDatabase(const std::string& name) const; |
1904 | bool TryFindExtensionInFallbackDatabase(const Descriptor* containing_type, |
1905 | int field_number) const; |
1906 | |
1907 | // This internal find extension method only check with its table and underlay |
1908 | // descriptor_pool's table. It does not check with fallback DB and no |
1909 | // additional proto file will be build in this method. |
1910 | const FieldDescriptor* InternalFindExtensionByNumberNoLock( |
1911 | const Descriptor* extendee, int number) const; |
1912 | |
1913 | // Like BuildFile() but called internally when the file has been loaded from |
1914 | // fallback_database_. Declared const because it is called by (semantically) |
1915 | // const methods. |
1916 | const FileDescriptor* BuildFileFromDatabase( |
1917 | const FileDescriptorProto& proto) const; |
1918 | |
1919 | // Helper for when lazily_build_dependencies_ is set, can look up a symbol |
1920 | // after the file's descriptor is built, and can build the file where that |
1921 | // symbol is defined if necessary. Will create a placeholder if the type |
1922 | // doesn't exist in the fallback database, or the file doesn't build |
1923 | // successfully. |
1924 | Symbol CrossLinkOnDemandHelper(const std::string& name, |
1925 | bool expecting_enum) const; |
1926 | |
1927 | // Create a placeholder FileDescriptor of the specified name |
1928 | FileDescriptor* NewPlaceholderFile(const std::string& name) const; |
1929 | FileDescriptor* NewPlaceholderFileWithMutexHeld( |
1930 | const std::string& name) const; |
1931 | |
1932 | enum PlaceholderType { |
1933 | PLACEHOLDER_MESSAGE, |
1934 | PLACEHOLDER_ENUM, |
1935 | PLACEHOLDER_EXTENDABLE_MESSAGE |
1936 | }; |
1937 | // Create a placeholder Descriptor of the specified name |
1938 | Symbol NewPlaceholder(const std::string& name, |
1939 | PlaceholderType placeholder_type) const; |
1940 | Symbol NewPlaceholderWithMutexHeld(const std::string& name, |
1941 | PlaceholderType placeholder_type) const; |
1942 | |
1943 | // If fallback_database_ is nullptr, this is nullptr. Otherwise, this is a |
1944 | // mutex which must be locked while accessing tables_. |
1945 | internal::WrappedMutex* mutex_; |
1946 | |
1947 | // See constructor. |
1948 | DescriptorDatabase* fallback_database_; |
1949 | ErrorCollector* default_error_collector_; |
1950 | const DescriptorPool* underlay_; |
1951 | |
1952 | // This class contains a lot of hash maps with complicated types that |
1953 | // we'd like to keep out of the header. |
1954 | class Tables; |
1955 | std::unique_ptr<Tables> tables_; |
1956 | |
1957 | bool enforce_dependencies_; |
1958 | bool lazily_build_dependencies_; |
1959 | bool allow_unknown_; |
1960 | bool enforce_weak_; |
1961 | bool disallow_enforce_utf8_; |
1962 | |
1963 | // Set of files to track for unused imports. The bool value when true means |
1964 | // unused imports are treated as errors (and as warnings when false). |
1965 | std::map<std::string, bool> unused_import_track_files_; |
1966 | |
1967 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorPool); |
1968 | }; |
1969 | |
1970 | |
1971 | // inline methods ==================================================== |
1972 | |
1973 | // These macros makes this repetitive code more readable. |
1974 | #define PROTOBUF_DEFINE_ACCESSOR(CLASS, FIELD, TYPE) \ |
1975 | inline TYPE CLASS::FIELD() const { return FIELD##_; } |
1976 | |
1977 | // Strings fields are stored as pointers but returned as const references. |
1978 | #define PROTOBUF_DEFINE_STRING_ACCESSOR(CLASS, FIELD) \ |
1979 | inline const std::string& CLASS::FIELD() const { return *FIELD##_; } |
1980 | |
1981 | // Arrays take an index parameter, obviously. |
1982 | #define PROTOBUF_DEFINE_ARRAY_ACCESSOR(CLASS, FIELD, TYPE) \ |
1983 | inline TYPE CLASS::FIELD(int index) const { return FIELD##s_ + index; } |
1984 | |
1985 | #define PROTOBUF_DEFINE_OPTIONS_ACCESSOR(CLASS, TYPE) \ |
1986 | inline const TYPE& CLASS::options() const { return *options_; } |
1987 | |
1988 | PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, name) |
1989 | PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, full_name) |
1990 | PROTOBUF_DEFINE_ACCESSOR(Descriptor, file, const FileDescriptor*) |
1991 | PROTOBUF_DEFINE_ACCESSOR(Descriptor, containing_type, const Descriptor*) |
1992 | |
1993 | PROTOBUF_DEFINE_ACCESSOR(Descriptor, field_count, int) |
1994 | PROTOBUF_DEFINE_ACCESSOR(Descriptor, oneof_decl_count, int) |
1995 | PROTOBUF_DEFINE_ACCESSOR(Descriptor, real_oneof_decl_count, int) |
1996 | PROTOBUF_DEFINE_ACCESSOR(Descriptor, nested_type_count, int) |
1997 | PROTOBUF_DEFINE_ACCESSOR(Descriptor, enum_type_count, int) |
1998 | |
1999 | PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, field, const FieldDescriptor*) |
2000 | PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, oneof_decl, const OneofDescriptor*) |
2001 | PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, nested_type, const Descriptor*) |
2002 | PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, enum_type, const EnumDescriptor*) |
2003 | |
2004 | PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_range_count, int) |
2005 | PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_count, int) |
2006 | PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension_range, |
2007 | const Descriptor::ExtensionRange*) |
2008 | PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension, const FieldDescriptor*) |
2009 | |
2010 | PROTOBUF_DEFINE_ACCESSOR(Descriptor, reserved_range_count, int) |
2011 | PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, reserved_range, |
2012 | const Descriptor::ReservedRange*) |
2013 | PROTOBUF_DEFINE_ACCESSOR(Descriptor, reserved_name_count, int) |
2014 | |
2015 | PROTOBUF_DEFINE_OPTIONS_ACCESSOR(Descriptor, MessageOptions) |
2016 | PROTOBUF_DEFINE_ACCESSOR(Descriptor, is_placeholder, bool) |
2017 | |
2018 | PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, name) |
2019 | PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, full_name) |
2020 | PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, json_name) |
2021 | PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, lowercase_name) |
2022 | PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, camelcase_name) |
2023 | PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, file, const FileDescriptor*) |
2024 | PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, number, int) |
2025 | PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, is_extension, bool) |
2026 | PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, label, FieldDescriptor::Label) |
2027 | PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, containing_type, const Descriptor*) |
2028 | PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, containing_oneof, |
2029 | const OneofDescriptor*) |
2030 | PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, index_in_oneof, int) |
2031 | PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, extension_scope, const Descriptor*) |
2032 | PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FieldDescriptor, FieldOptions) |
2033 | PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_default_value, bool) |
2034 | PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_json_name, bool) |
2035 | PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int32, int32) |
2036 | PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int64, int64) |
2037 | PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint32, uint32) |
2038 | PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint64, uint64) |
2039 | PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_float, float) |
2040 | PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_double, double) |
2041 | PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_bool, bool) |
2042 | PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, default_value_string) |
2043 | |
2044 | PROTOBUF_DEFINE_STRING_ACCESSOR(OneofDescriptor, name) |
2045 | PROTOBUF_DEFINE_STRING_ACCESSOR(OneofDescriptor, full_name) |
2046 | PROTOBUF_DEFINE_ACCESSOR(OneofDescriptor, containing_type, const Descriptor*) |
2047 | PROTOBUF_DEFINE_ACCESSOR(OneofDescriptor, field_count, int) |
2048 | PROTOBUF_DEFINE_OPTIONS_ACCESSOR(OneofDescriptor, OneofOptions) |
2049 | |
2050 | PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, name) |
2051 | PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, full_name) |
2052 | PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, file, const FileDescriptor*) |
2053 | PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, containing_type, const Descriptor*) |
2054 | PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, value_count, int) |
2055 | PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, value, |
2056 | const EnumValueDescriptor*) |
2057 | PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumDescriptor, EnumOptions) |
2058 | PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, is_placeholder, bool) |
2059 | PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, reserved_range_count, int) |
2060 | PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, reserved_range, |
2061 | const EnumDescriptor::ReservedRange*) |
2062 | PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, reserved_name_count, int) |
2063 | |
2064 | PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, name) |
2065 | PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, full_name) |
2066 | PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, number, int) |
2067 | PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, type, const EnumDescriptor*) |
2068 | PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumValueDescriptor, EnumValueOptions) |
2069 | |
2070 | PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, name) |
2071 | PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, full_name) |
2072 | PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, file, const FileDescriptor*) |
2073 | PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, method_count, int) |
2074 | PROTOBUF_DEFINE_ARRAY_ACCESSOR(ServiceDescriptor, method, |
2075 | const MethodDescriptor*) |
2076 | PROTOBUF_DEFINE_OPTIONS_ACCESSOR(ServiceDescriptor, ServiceOptions) |
2077 | |
2078 | PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, name) |
2079 | PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, full_name) |
2080 | PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, service, const ServiceDescriptor*) |
2081 | PROTOBUF_DEFINE_OPTIONS_ACCESSOR(MethodDescriptor, MethodOptions) |
2082 | PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, client_streaming, bool) |
2083 | PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, server_streaming, bool) |
2084 | |
2085 | PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, name) |
2086 | PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, package) |
2087 | PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, pool, const DescriptorPool*) |
2088 | PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, dependency_count, int) |
2089 | PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, public_dependency_count, int) |
2090 | PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, weak_dependency_count, int) |
2091 | PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, message_type_count, int) |
2092 | PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, enum_type_count, int) |
2093 | PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, service_count, int) |
2094 | PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, extension_count, int) |
2095 | PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FileDescriptor, FileOptions) |
2096 | PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, is_placeholder, bool) |
2097 | |
2098 | PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, message_type, const Descriptor*) |
2099 | PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, enum_type, const EnumDescriptor*) |
2100 | PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, service, |
2101 | const ServiceDescriptor*) |
2102 | PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, extension, |
2103 | const FieldDescriptor*) |
2104 | |
2105 | #undef PROTOBUF_DEFINE_ACCESSOR |
2106 | #undef PROTOBUF_DEFINE_STRING_ACCESSOR |
2107 | #undef PROTOBUF_DEFINE_ARRAY_ACCESSOR |
2108 | |
2109 | // A few accessors differ from the macros... |
2110 | |
2111 | inline Descriptor::WellKnownType Descriptor::well_known_type() const { |
2112 | return static_cast<Descriptor::WellKnownType>(well_known_type_); |
2113 | } |
2114 | |
2115 | inline bool Descriptor::IsExtensionNumber(int number) const { |
2116 | return FindExtensionRangeContainingNumber(number) != nullptr; |
2117 | } |
2118 | |
2119 | inline bool Descriptor::IsReservedNumber(int number) const { |
2120 | return FindReservedRangeContainingNumber(number) != nullptr; |
2121 | } |
2122 | |
2123 | inline bool Descriptor::IsReservedName(const std::string& name) const { |
2124 | for (int i = 0; i < reserved_name_count(); i++) { |
2125 | if (name == reserved_name(index: i)) { |
2126 | return true; |
2127 | } |
2128 | } |
2129 | return false; |
2130 | } |
2131 | |
2132 | // Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because reserved_names_ is actually |
2133 | // an array of pointers rather than the usual array of objects. |
2134 | inline const std::string& Descriptor::reserved_name(int index) const { |
2135 | return *reserved_names_[index]; |
2136 | } |
2137 | |
2138 | inline bool EnumDescriptor::IsReservedNumber(int number) const { |
2139 | return FindReservedRangeContainingNumber(number) != nullptr; |
2140 | } |
2141 | |
2142 | inline bool EnumDescriptor::IsReservedName(const std::string& name) const { |
2143 | for (int i = 0; i < reserved_name_count(); i++) { |
2144 | if (name == reserved_name(index: i)) { |
2145 | return true; |
2146 | } |
2147 | } |
2148 | return false; |
2149 | } |
2150 | |
2151 | // Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because reserved_names_ is actually |
2152 | // an array of pointers rather than the usual array of objects. |
2153 | inline const std::string& EnumDescriptor::reserved_name(int index) const { |
2154 | return *reserved_names_[index]; |
2155 | } |
2156 | |
2157 | inline FieldDescriptor::Type FieldDescriptor::type() const { |
2158 | if (type_once_) { |
2159 | internal::call_once(args&: *type_once_, args: &FieldDescriptor::TypeOnceInit, args: this); |
2160 | } |
2161 | return type_; |
2162 | } |
2163 | |
2164 | inline bool FieldDescriptor::is_required() const { |
2165 | return label() == LABEL_REQUIRED; |
2166 | } |
2167 | |
2168 | inline bool FieldDescriptor::is_optional() const { |
2169 | return label() == LABEL_OPTIONAL; |
2170 | } |
2171 | |
2172 | inline bool FieldDescriptor::is_repeated() const { |
2173 | return label() == LABEL_REPEATED; |
2174 | } |
2175 | |
2176 | inline bool FieldDescriptor::is_packable() const { |
2177 | return is_repeated() && IsTypePackable(field_type: type()); |
2178 | } |
2179 | |
2180 | inline bool FieldDescriptor::is_map() const { |
2181 | return type() == TYPE_MESSAGE && is_map_message_type(); |
2182 | } |
2183 | |
2184 | inline bool FieldDescriptor::has_optional_keyword() const { |
2185 | return proto3_optional_ || |
2186 | (file()->syntax() == FileDescriptor::SYNTAX_PROTO2 && is_optional() && |
2187 | !containing_oneof()); |
2188 | } |
2189 | |
2190 | inline const OneofDescriptor* FieldDescriptor::real_containing_oneof() const { |
2191 | return containing_oneof_ && !containing_oneof_->is_synthetic() |
2192 | ? containing_oneof_ |
2193 | : nullptr; |
2194 | } |
2195 | |
2196 | inline bool FieldDescriptor::has_presence() const { |
2197 | if (is_repeated()) return false; |
2198 | return cpp_type() == CPPTYPE_MESSAGE || containing_oneof() || |
2199 | file()->syntax() == FileDescriptor::SYNTAX_PROTO2; |
2200 | } |
2201 | |
2202 | // To save space, index() is computed by looking at the descriptor's position |
2203 | // in the parent's array of children. |
2204 | inline int FieldDescriptor::index() const { |
2205 | if (!is_extension_) { |
2206 | return static_cast<int>(this - containing_type()->fields_); |
2207 | } else if (extension_scope_ != nullptr) { |
2208 | return static_cast<int>(this - extension_scope_->extensions_); |
2209 | } else { |
2210 | return static_cast<int>(this - file_->extensions_); |
2211 | } |
2212 | } |
2213 | |
2214 | inline int Descriptor::index() const { |
2215 | if (containing_type_ == nullptr) { |
2216 | return static_cast<int>(this - file_->message_types_); |
2217 | } else { |
2218 | return static_cast<int>(this - containing_type_->nested_types_); |
2219 | } |
2220 | } |
2221 | |
2222 | inline const FileDescriptor* OneofDescriptor::file() const { |
2223 | return containing_type()->file(); |
2224 | } |
2225 | |
2226 | inline int OneofDescriptor::index() const { |
2227 | return static_cast<int>(this - containing_type_->oneof_decls_); |
2228 | } |
2229 | |
2230 | inline bool OneofDescriptor::is_synthetic() const { |
2231 | return field_count() == 1 && field(index: 0)->proto3_optional_; |
2232 | } |
2233 | |
2234 | inline int EnumDescriptor::index() const { |
2235 | if (containing_type_ == nullptr) { |
2236 | return static_cast<int>(this - file_->enum_types_); |
2237 | } else { |
2238 | return static_cast<int>(this - containing_type_->enum_types_); |
2239 | } |
2240 | } |
2241 | |
2242 | inline const FileDescriptor* EnumValueDescriptor::file() const { |
2243 | return type()->file(); |
2244 | } |
2245 | |
2246 | inline int EnumValueDescriptor::index() const { |
2247 | return static_cast<int>(this - type_->values_); |
2248 | } |
2249 | |
2250 | inline int ServiceDescriptor::index() const { |
2251 | return static_cast<int>(this - file_->services_); |
2252 | } |
2253 | |
2254 | inline const FileDescriptor* MethodDescriptor::file() const { |
2255 | return service()->file(); |
2256 | } |
2257 | |
2258 | inline int MethodDescriptor::index() const { |
2259 | return static_cast<int>(this - service_->methods_); |
2260 | } |
2261 | |
2262 | inline const char* FieldDescriptor::type_name() const { |
2263 | return kTypeToName[type()]; |
2264 | } |
2265 | |
2266 | inline FieldDescriptor::CppType FieldDescriptor::cpp_type() const { |
2267 | return kTypeToCppTypeMap[type()]; |
2268 | } |
2269 | |
2270 | inline const char* FieldDescriptor::cpp_type_name() const { |
2271 | return kCppTypeToName[kTypeToCppTypeMap[type()]]; |
2272 | } |
2273 | |
2274 | inline FieldDescriptor::CppType FieldDescriptor::TypeToCppType(Type type) { |
2275 | return kTypeToCppTypeMap[type]; |
2276 | } |
2277 | |
2278 | inline const char* FieldDescriptor::TypeName(Type type) { |
2279 | return kTypeToName[type]; |
2280 | } |
2281 | |
2282 | inline const char* FieldDescriptor::CppTypeName(CppType cpp_type) { |
2283 | return kCppTypeToName[cpp_type]; |
2284 | } |
2285 | |
2286 | inline bool FieldDescriptor::IsTypePackable(Type field_type) { |
2287 | return (field_type != FieldDescriptor::TYPE_STRING && |
2288 | field_type != FieldDescriptor::TYPE_GROUP && |
2289 | field_type != FieldDescriptor::TYPE_MESSAGE && |
2290 | field_type != FieldDescriptor::TYPE_BYTES); |
2291 | } |
2292 | |
2293 | inline const FileDescriptor* FileDescriptor::public_dependency( |
2294 | int index) const { |
2295 | return dependency(index: public_dependencies_[index]); |
2296 | } |
2297 | |
2298 | inline const FileDescriptor* FileDescriptor::weak_dependency(int index) const { |
2299 | return dependency(index: weak_dependencies_[index]); |
2300 | } |
2301 | |
2302 | inline FileDescriptor::Syntax FileDescriptor::syntax() const { return syntax_; } |
2303 | |
2304 | // Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because fields_ is actually an array |
2305 | // of pointers rather than the usual array of objects. |
2306 | inline const FieldDescriptor* OneofDescriptor::field(int index) const { |
2307 | return fields_[index]; |
2308 | } |
2309 | |
2310 | } // namespace protobuf |
2311 | } // namespace google |
2312 | |
2313 | #include <google/protobuf/port_undef.inc> |
2314 | |
2315 | #endif // GOOGLE_PROTOBUF_DESCRIPTOR_H__ |
2316 | |