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