~ubuntu-branches/ubuntu/oneiric/protobuf/oneiric

« back to all changes in this revision

Viewing changes to src/google/protobuf/descriptor.h

  • Committer: Bazaar Package Importer
  • Author(s): Iustin Pop
  • Date: 2008-08-03 11:01:44 UTC
  • Revision ID: james.westby@ubuntu.com-20080803110144-uyiw41bf1m2oe17t
Tags: upstream-2.0.0~b
ImportĀ upstreamĀ versionĀ 2.0.0~b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Protocol Buffers - Google's data interchange format
 
2
// Copyright 2008 Google Inc.
 
3
// http://code.google.com/p/protobuf/
 
4
//
 
5
// Licensed under the Apache License, Version 2.0 (the "License");
 
6
// you may not use this file except in compliance with the License.
 
7
// You may obtain a copy of the License at
 
8
//
 
9
//      http://www.apache.org/licenses/LICENSE-2.0
 
10
//
 
11
// Unless required by applicable law or agreed to in writing, software
 
12
// distributed under the License is distributed on an "AS IS" BASIS,
 
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
// See the License for the specific language governing permissions and
 
15
// limitations under the License.
 
16
 
 
17
// Author: kenton@google.com (Kenton Varda)
 
18
//  Based on original Protocol Buffers design by
 
19
//  Sanjay Ghemawat, Jeff Dean, and others.
 
20
//
 
21
// This file contains classes which describe a type of protocol message.
 
22
// You can use a message's descriptor to learn at runtime what fields
 
23
// it contains and what the types of those fields are.  The Message
 
24
// interface also allows you to dynamically access and modify individual
 
25
// fields by passing the FieldDescriptor of the field you are interested
 
26
// in.
 
27
//
 
28
// Most users will not care about descriptors, because they will write
 
29
// code specific to certain protocol types and will simply use the classes
 
30
// generated by the protocol compiler directly.  Advanced users who want
 
31
// to operate on arbitrary types (not known at compile time) may want to
 
32
// read descriptors in order to learn about the contents of a message.
 
33
// A very small number of users will want to construct their own
 
34
// Descriptors, either because they are implementing Message manually or
 
35
// because they are writing something like the protocol compiler.
 
36
//
 
37
// For an example of how you might use descriptors, see the code example
 
38
// at the top of message.h.
 
39
 
 
40
#ifndef GOOGLE_PROTOBUF_DESCRIPTOR_H__
 
41
#define GOOGLE_PROTOBUF_DESCRIPTOR_H__
 
42
 
 
43
#include <string>
 
44
#include <google/protobuf/stubs/common.h>
 
45
 
 
46
 
 
47
namespace google {
 
48
namespace protobuf {
 
49
 
 
50
// Defined in this file.
 
51
class Descriptor;
 
52
class FieldDescriptor;
 
53
class EnumDescriptor;
 
54
class EnumValueDescriptor;
 
55
class ServiceDescriptor;
 
56
class MethodDescriptor;
 
57
class FileDescriptor;
 
58
class DescriptorDatabase;
 
59
class DescriptorPool;
 
60
 
 
61
// Defined in descriptor.proto
 
62
class DescriptorProto;
 
63
class FieldDescriptorProto;
 
64
class EnumDescriptorProto;
 
65
class EnumValueDescriptorProto;
 
66
class ServiceDescriptorProto;
 
67
class MethodDescriptorProto;
 
68
class FileDescriptorProto;
 
69
class MessageOptions;
 
70
class FieldOptions;
 
71
class EnumOptions;
 
72
class EnumValueOptions;
 
73
class ServiceOptions;
 
74
class MethodOptions;
 
75
class FileOptions;
 
76
 
 
77
// Defined in message.h
 
78
class Message;
 
79
 
 
80
// Defined in descriptor.cc
 
81
class DescriptorBuilder;
 
82
 
 
83
// Describes a type of protocol message, or a particular group within a
 
84
// message.  To obtain the Descriptor for a given message object, call
 
85
// Message::GetDescriptor().  Generated message classes also have a
 
86
// static method called descriptor() which returns the type's descriptor.
 
87
// Use DescriptorPool to construct your own descriptors.
 
88
class LIBPROTOBUF_EXPORT Descriptor {
 
89
 public:
 
90
  // The name of the message type, not including its scope.
 
91
  const string& name() const;
 
92
 
 
93
  // The fully-qualified name of the message type, scope delimited by
 
94
  // periods.  For example, message type "Foo" which is declared in package
 
95
  // "bar" has full name "bar.Foo".  If a type "Baz" is nested within
 
96
  // Foo, Baz's full_name is "bar.Foo.Baz".  To get only the part that
 
97
  // comes after the last '.', use name().
 
98
  const string& full_name() const;
 
99
 
 
100
  // Index of this descriptor within the file or containing type's message
 
101
  // type array.
 
102
  int index() const;
 
103
 
 
104
  // The .proto file in which this message type was defined.  Never NULL.
 
105
  const FileDescriptor* file() const;
 
106
 
 
107
  // If this Descriptor describes a nested type, this returns the type
 
108
  // in which it is nested.  Otherwise, returns NULL.
 
109
  const Descriptor* containing_type() const;
 
110
 
 
111
  // Get options for this message type.  These are specified in the .proto
 
112
  // file by placing lines like "option foo = 1234;" in the message definition.
 
113
  // The exact set of known options is defined by MessageOptions in
 
114
  // google/protobuf/descriptor.proto.
 
115
  const MessageOptions& options() const;
 
116
 
 
117
  // Write the contents of this Descriptor into the given DescriptorProto.
 
118
  // The target DescriptorProto must be clear before calling this; if it
 
119
  // isn't, the result may be garbage.
 
120
  void CopyTo(DescriptorProto* proto) const;
 
121
 
 
122
  // Write the contents of this decriptor in a human-readable form. Output
 
123
  // will be suitable for re-parsing.
 
124
  string DebugString() const;
 
125
 
 
126
  // Field stuff -----------------------------------------------------
 
127
 
 
128
  // The number of fields in this message type.
 
129
  int field_count() const;
 
130
  // Gets a field by index, where 0 <= index < field_count().
 
131
  const FieldDescriptor* field(int index) const;
 
132
 
 
133
  // Looks up a field by declared tag number.  Returns NULL if no such field
 
134
  // exists.
 
135
  const FieldDescriptor* FindFieldByNumber(int number) const;
 
136
  // Looks up a field by name.  Returns NULL if no such field exists.
 
137
  const FieldDescriptor* FindFieldByName(const string& name) const;
 
138
 
 
139
  // Nested type stuff -----------------------------------------------
 
140
 
 
141
  // The number of nested types in this message type.
 
142
  int nested_type_count() const;
 
143
  // Gets a nested type by index, where 0 <= index < nested_type_count().
 
144
  const Descriptor* nested_type(int index) const;
 
145
 
 
146
  // Looks up a nested type by name.  Returns NULL if no such nested type
 
147
  // exists.
 
148
  const Descriptor* FindNestedTypeByName(const string& name) const;
 
149
 
 
150
  // Enum stuff ------------------------------------------------------
 
151
 
 
152
  // The number of enum types in this message type.
 
153
  int enum_type_count() const;
 
154
  // Gets an enum type by index, where 0 <= index < enum_type_count().
 
155
  const EnumDescriptor* enum_type(int index) const;
 
156
 
 
157
  // Looks up an enum type by name.  Returns NULL if no such enum type exists.
 
158
  const EnumDescriptor* FindEnumTypeByName(const string& name) const;
 
159
 
 
160
  // Looks up an enum value by name, among all enum types in this message.
 
161
  // Returns NULL if no such value exists.
 
162
  const EnumValueDescriptor* FindEnumValueByName(const string& name) const;
 
163
 
 
164
  // Extensions ------------------------------------------------------
 
165
 
 
166
  // A range of field numbers which are designated for third-party
 
167
  // extensions.
 
168
  struct ExtensionRange {
 
169
    int start;  // inclusive
 
170
    int end;    // exclusive
 
171
  };
 
172
 
 
173
  // The number of extension ranges in this message type.
 
174
  int extension_range_count() const;
 
175
  // Gets an extension range by index, where 0 <= index <
 
176
  // extension_range_count().
 
177
  const ExtensionRange* extension_range(int index) const;
 
178
 
 
179
  // Returns true if the number is in one of the extension ranges.
 
180
  bool IsExtensionNumber(int number) const;
 
181
 
 
182
  // The number of extensions -- extending *other* messages -- that were
 
183
  // defined nested within this message type's scope.
 
184
  int extension_count() const;
 
185
  // Get an extension by index, where 0 <= index < extension_count().
 
186
  const FieldDescriptor* extension(int index) const;
 
187
 
 
188
  // Looks up a named extension (which extends some *other* message type)
 
189
  // defined within this message type's scope.
 
190
  const FieldDescriptor* FindExtensionByName(const string& name) const;
 
191
 
 
192
 private:
 
193
  // Internal version of DebugString; controls the level of indenting for
 
194
  // correct depth
 
195
  void DebugString(int depth, string *contents) const;
 
196
 
 
197
  const string* name_;
 
198
  const string* full_name_;
 
199
  const FileDescriptor* file_;
 
200
  const Descriptor* containing_type_;
 
201
  const MessageOptions* options_;
 
202
  int field_count_;
 
203
  FieldDescriptor* fields_;
 
204
  int nested_type_count_;
 
205
  Descriptor* nested_types_;
 
206
  int enum_type_count_;
 
207
  EnumDescriptor* enum_types_;
 
208
  int extension_range_count_;
 
209
  ExtensionRange* extension_ranges_;
 
210
  int extension_count_;
 
211
  FieldDescriptor* extensions_;
 
212
 
 
213
  // Must be constructed using DescriptorPool.
 
214
  Descriptor() {}
 
215
  friend class DescriptorBuilder;
 
216
  friend class EnumDescriptor;
 
217
  friend class FieldDescriptor;
 
218
  friend class FileDescriptor;
 
219
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Descriptor);
 
220
};
 
221
 
 
222
// Describes a single field of a message.  To get the descriptor for a given
 
223
// field, first get the Descriptor for the message in which it is defined,
 
224
// then call Descriptor::FindFieldByName().  To get a FieldDescriptor for
 
225
// an extension, do one of the following:
 
226
// - Get the Descriptor or FileDescriptor for its containing scope, then
 
227
//   call Descriptor::FindExtensionByName() or
 
228
//   FileDescriptor::FindExtensionByName().
 
229
// - Given a DescriptorPool, call DescriptorPool::FindExtensionByNumber().
 
230
// - Given a Message::Reflection for a message object, call
 
231
//   Message::Reflection::FindKnownExtensionByName() or
 
232
//   Message::Reflection::FindKnownExtensionByNumber().
 
233
// Use DescriptorPool to construct your own descriptors.
 
234
class LIBPROTOBUF_EXPORT FieldDescriptor {
 
235
 public:
 
236
  // Identifies a field type.  0 is reserved for errors.  The order is weird
 
237
  // for historical reasons.  Types 12 and up are new in proto2.
 
238
  enum Type {
 
239
    TYPE_DOUBLE         = 1,   // double, exactly eight bytes on the wire.
 
240
    TYPE_FLOAT          = 2,   // float, exactly four bytes on the wire.
 
241
    TYPE_INT64          = 3,   // int64, varint on the wire.  Negative numbers
 
242
                               // take 10 bytes.  Use TYPE_SINT64 if negative
 
243
                               // values are likely.
 
244
    TYPE_UINT64         = 4,   // uint64, varint on the wire.
 
245
    TYPE_INT32          = 5,   // int32, varint on the wire.  Negative numbers
 
246
                               // take 10 bytes.  Use TYPE_SINT32 if negative
 
247
                               // values are likely.
 
248
    TYPE_FIXED64        = 6,   // uint64, exactly eight bytes on the wire.
 
249
    TYPE_FIXED32        = 7,   // uint32, exactly four bytes on the wire.
 
250
    TYPE_BOOL           = 8,   // bool, varint on the wire.
 
251
    TYPE_STRING         = 9,   // UTF-8 text.
 
252
    TYPE_GROUP          = 10,  // Tag-delimited message.  Deprecated.
 
253
    TYPE_MESSAGE        = 11,  // Length-delimited message.
 
254
 
 
255
    TYPE_BYTES          = 12,  // Arbitrary byte array.
 
256
    TYPE_UINT32         = 13,  // uint32, varint on the wire
 
257
    TYPE_ENUM           = 14,  // Enum, varint on the wire
 
258
    TYPE_SFIXED32       = 15,  // int32, exactly four bytes on the wire
 
259
    TYPE_SFIXED64       = 16,  // int64, exactly eight bytes on the wire
 
260
    TYPE_SINT32         = 17,  // int32, ZigZag-encoded varint on the wire
 
261
    TYPE_SINT64         = 18,  // int64, ZigZag-encoded varint on the wire
 
262
 
 
263
    MAX_TYPE            = 18,  // Constant useful for defining lookup tables
 
264
                               // indexed by Type.
 
265
  };
 
266
 
 
267
  // Specifies the C++ data type used to represent the field.  There is a
 
268
  // fixed mapping from Type to CppType where each Type maps to exactly one
 
269
  // CppType.  0 is reserved for errors.
 
270
  enum CppType {
 
271
    CPPTYPE_INT32       = 1,     // TYPE_INT32, TYPE_SINT32, TYPE_SFIXED32
 
272
    CPPTYPE_INT64       = 2,     // TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64
 
273
    CPPTYPE_UINT32      = 3,     // TYPE_UINT32, TYPE_FIXED32
 
274
    CPPTYPE_UINT64      = 4,     // TYPE_UINT64, TYPE_FIXED64
 
275
    CPPTYPE_DOUBLE      = 5,     // TYPE_DOUBLE
 
276
    CPPTYPE_FLOAT       = 6,     // TYPE_FLOAT
 
277
    CPPTYPE_BOOL        = 7,     // TYPE_BOOL
 
278
    CPPTYPE_ENUM        = 8,     // TYPE_ENUM
 
279
    CPPTYPE_STRING      = 9,     // TYPE_STRING, TYPE_BYTES
 
280
    CPPTYPE_MESSAGE     = 10,    // TYPE_MESSAGE, TYPE_GROUP
 
281
 
 
282
    MAX_CPPTYPE         = 10,    // Constant useful for defining lookup tables
 
283
                                 // indexed by CppType.
 
284
  };
 
285
 
 
286
  // Identifies whether the field is optional, required, or repeated.  0 is
 
287
  // reserved for errors.
 
288
  enum Label {
 
289
    LABEL_OPTIONAL      = 1,    // optional
 
290
    LABEL_REQUIRED      = 2,    // required
 
291
    LABEL_REPEATED      = 3,    // repeated
 
292
 
 
293
    MAX_LABEL           = 3,    // Constant useful for defining lookup tables
 
294
                                // indexed by Label.
 
295
  };
 
296
 
 
297
  // Valid field numbers are positive integers up to kMaxNumber.
 
298
  static const int kMaxNumber = (1 << 29) - 1;
 
299
 
 
300
  // First field number reserved for the protocol buffer library implementation.
 
301
  // Users may not declare fields that use reserved numbers.
 
302
  static const int kFirstReservedNumber = 19000;
 
303
  // Last field number reserved for the protocol buffer library implementation.
 
304
  // Users may not declare fields that use reserved numbers.
 
305
  static const int kLastReservedNumber  = 19999;
 
306
 
 
307
  const string& name() const;        // Name of this field within the message.
 
308
  const string& full_name() const;   // Fully-qualified name of the field.
 
309
  const FileDescriptor* file() const;// File in which this field was defined.
 
310
  bool is_extension() const;         // Is this an extension field?
 
311
  int number() const;                // Declared tag number.
 
312
 
 
313
  Type type() const;                 // Declared type of this field.
 
314
  CppType cpp_type() const;          // C++ type of this field.
 
315
  Label label() const;               // optional/required/repeated
 
316
 
 
317
  bool is_required() const;      // shorthand for label() == LABEL_REQUIRED
 
318
  bool is_optional() const;      // shorthand for label() == LABEL_OPTIONAL
 
319
  bool is_repeated() const;      // shorthand for label() == LABEL_REPEATED
 
320
 
 
321
  // Index of this field within the message's field array, or the file or
 
322
  // extension scope's extensions array.
 
323
  int index() const;
 
324
 
 
325
  // Does this field have an explicitly-declared default value?
 
326
  bool has_default_value() const;
 
327
 
 
328
  // Get the field default value if cpp_type() == CPPTYPE_INT32.  If no
 
329
  // explicit default was defined, the default is 0.
 
330
  int32 default_value_int32() const;
 
331
  // Get the field default value if cpp_type() == CPPTYPE_INT64.  If no
 
332
  // explicit default was defined, the default is 0.
 
333
  int64 default_value_int64() const;
 
334
  // Get the field default value if cpp_type() == CPPTYPE_UINT32.  If no
 
335
  // explicit default was defined, the default is 0.
 
336
  uint32 default_value_uint32() const;
 
337
  // Get the field default value if cpp_type() == CPPTYPE_UINT64.  If no
 
338
  // explicit default was defined, the default is 0.
 
339
  uint64 default_value_uint64() const;
 
340
  // Get the field default value if cpp_type() == CPPTYPE_FLOAT.  If no
 
341
  // explicit default was defined, the default is 0.0.
 
342
  float default_value_float() const;
 
343
  // Get the field default value if cpp_type() == CPPTYPE_DOUBLE.  If no
 
344
  // explicit default was defined, the default is 0.0.
 
345
  double default_value_double() const;
 
346
  // Get the field default value if cpp_type() == CPPTYPE_BOOL.  If no
 
347
  // explicit default was defined, the default is false.
 
348
  bool default_value_bool() const;
 
349
  // Get the field default value if cpp_type() == CPPTYPE_ENUM.  If no
 
350
  // explicit default was defined, the default is the first value defined
 
351
  // in the enum type (all enum types are required to have at least one value).
 
352
  // This never returns NULL.
 
353
  const EnumValueDescriptor* default_value_enum() const;
 
354
  // Get the field default value if cpp_type() == CPPTYPE_STRING.  If no
 
355
  // explicit default was defined, the default is the empty string.
 
356
  const string& default_value_string() const;
 
357
 
 
358
  // The Descriptor for the message of which this is a field.  For extensions,
 
359
  // this is the extended type.  Never NULL.
 
360
  const Descriptor* containing_type() const;
 
361
 
 
362
  // An extension may be declared within the scope of another message.  If this
 
363
  // field is an extension (is_extension() is true), then extension_scope()
 
364
  // returns that message, or NULL if the extension was declared at global
 
365
  // scope.  If this is not an extension, extension_scope() is undefined (may
 
366
  // assert-fail).
 
367
  const Descriptor* extension_scope() const;
 
368
 
 
369
  // If type is TYPE_MESSAGE or TYPE_GROUP, returns a descriptor for the
 
370
  // message or the group type.  Otherwise, undefined.
 
371
  const Descriptor* message_type() const;
 
372
  // If type is TYPE_ENUM, returns a descriptor for the enum.  Otherwise,
 
373
  // undefined.
 
374
  const EnumDescriptor* enum_type() const;
 
375
 
 
376
  // EXPERIMENTAL; DO NOT USE.
 
377
  // If this field is a map field, experimental_map_key() is the field
 
378
  // that is the key for this map.
 
379
  // experimental_map_key()->containing_type() is the same as message_type().
 
380
  const FieldDescriptor* experimental_map_key() const;
 
381
 
 
382
  // Get the FieldOptions for this field.  This includes things listed in
 
383
  // square brackets after the field definition.  E.g., the field:
 
384
  //   optional string text = 1 [ctype=CORD];
 
385
  // has the "ctype" option set.  FieldOptions is actually a protocol message,
 
386
  // which makes it easier to extend.
 
387
  const FieldOptions& options() const;
 
388
 
 
389
  // See Descriptor::CopyTo().
 
390
  void CopyTo(FieldDescriptorProto* proto) const;
 
391
 
 
392
  // See Descriptor::DebugString().
 
393
  string DebugString() const;
 
394
 private:
 
395
  // See Descriptor::DebugString().
 
396
  void DebugString(int depth, string *contents) const;
 
397
 
 
398
  // formats the default value appropriately and returns it as a string.
 
399
  // Must have a default value to call this. If quote_string_type is true, then
 
400
  // types of CPPTYPE_STRING whill be surrounded by quotes and CEscaped.
 
401
  string DefaultValueAsString(bool quote_string_type) const;
 
402
 
 
403
  const string* name_;
 
404
  const string* full_name_;
 
405
  const FileDescriptor* file_;
 
406
  int number_;
 
407
  Type type_;
 
408
  Label label_;
 
409
  bool is_extension_;
 
410
  const Descriptor* containing_type_;
 
411
  const Descriptor* extension_scope_;
 
412
  const Descriptor* message_type_;
 
413
  const EnumDescriptor* enum_type_;
 
414
  const FieldDescriptor* experimental_map_key_;
 
415
  const FieldOptions* options_;
 
416
 
 
417
  bool has_default_value_;
 
418
  union {
 
419
    int32  default_value_int32_;
 
420
    int64  default_value_int64_;
 
421
    uint32 default_value_uint32_;
 
422
    uint64 default_value_uint64_;
 
423
    float  default_value_float_;
 
424
    double default_value_double_;
 
425
    bool   default_value_bool_;
 
426
 
 
427
    const EnumValueDescriptor* default_value_enum_;
 
428
    const string* default_value_string_;
 
429
  };
 
430
 
 
431
  static const CppType kTypeToCppTypeMap[MAX_TYPE + 1];
 
432
 
 
433
  static const char * const kTypeToName[MAX_TYPE + 1];
 
434
 
 
435
  static const char * const kLabelToName[MAX_LABEL + 1];
 
436
 
 
437
  // Must be constructed using DescriptorPool.
 
438
  FieldDescriptor() {}
 
439
  friend class DescriptorBuilder;
 
440
  friend class FileDescriptor;
 
441
  friend class Descriptor;
 
442
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldDescriptor);
 
443
};
 
444
 
 
445
// Describes an enum type defined in a .proto file.  To get the EnumDescriptor
 
446
// for a generated enum type, call TypeName_descriptor().  Use DescriptorPool
 
447
// to construct your own descriptors.
 
448
class LIBPROTOBUF_EXPORT EnumDescriptor {
 
449
 public:
 
450
  // The name of this enum type in the containing scope.
 
451
  const string& name() const;
 
452
 
 
453
  // The fully-qualified name of the enum type, scope delimited by periods.
 
454
  const string& full_name() const;
 
455
 
 
456
  // Index of this enum within the file or containing message's enum array.
 
457
  int index() const;
 
458
 
 
459
  // The .proto file in which this enum type was defined.  Never NULL.
 
460
  const FileDescriptor* file() const;
 
461
 
 
462
  // The number of values for this EnumDescriptor.  Guaranteed to be greater
 
463
  // than zero.
 
464
  int value_count() const;
 
465
  // Gets a value by index, where 0 <= index < value_count().
 
466
  const EnumValueDescriptor* value(int index) const;
 
467
 
 
468
  // Looks up a value by name.  Returns NULL if no such value exists.
 
469
  const EnumValueDescriptor* FindValueByName(const string& name) const;
 
470
  // Looks up a value by number.  Returns NULL if no such value exists.  If
 
471
  // multiple values have this number, the first one defined is returned.
 
472
  const EnumValueDescriptor* FindValueByNumber(int number) const;
 
473
 
 
474
  // If this enum type is nested in a message type, this is that message type.
 
475
  // Otherwise, NULL.
 
476
  const Descriptor* containing_type() const;
 
477
 
 
478
  // Get options for this enum type.  These are specified in the .proto
 
479
  // file by placing lines like "option foo = 1234;" in the enum definition.
 
480
  // The exact set of known options is defined by EnumOptions in
 
481
  // google/protobuf/descriptor.proto.
 
482
  const EnumOptions& options() const;
 
483
 
 
484
  // See Descriptor::CopyTo().
 
485
  void CopyTo(EnumDescriptorProto* proto) const;
 
486
 
 
487
  // See Descriptor::DebugString().
 
488
  string DebugString() const;
 
489
 
 
490
 private:
 
491
  // See Descriptor::DebugString().
 
492
  void DebugString(int depth, string *contents) const;
 
493
 
 
494
  const string* name_;
 
495
  const string* full_name_;
 
496
  const FileDescriptor* file_;
 
497
  int value_count_;
 
498
  EnumValueDescriptor* values_;
 
499
  const Descriptor* containing_type_;
 
500
  const EnumOptions* options_;
 
501
 
 
502
  // Must be constructed using DescriptorPool.
 
503
  EnumDescriptor() {}
 
504
  friend class DescriptorBuilder;
 
505
  friend class Descriptor;
 
506
  friend class EnumValueDescriptor;
 
507
  friend class FileDescriptor;
 
508
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumDescriptor);
 
509
};
 
510
 
 
511
// Describes an individual enum constant of a particular type.  To get the
 
512
// EnumValueDescriptor for a given enum value, first get the EnumDescriptor
 
513
// for its type, then use EnumDescriptor::FindValueByName() or
 
514
// EnumDescriptor::FindValueByNumber().  Use DescriptorPool to construct
 
515
// your own descriptors.
 
516
class LIBPROTOBUF_EXPORT EnumValueDescriptor {
 
517
 public:
 
518
  const string& name() const;  // Name of this enum constant.
 
519
  int index() const;           // Index within the enums's Descriptor.
 
520
  int number() const;          // Numeric value of this enum constant.
 
521
 
 
522
  // The full_name of an enum value is a sibling symbol of the enum type.
 
523
  // e.g. the full name of FieldDescriptorProto::TYPE_INT32 is actually
 
524
  // "google.protobuf.FieldDescriptorProto.TYPE_INT32", NOT
 
525
  // "google.protobuf.FieldDescriptorProto.Type.TYPE_INT32".  This is to conform
 
526
  // with C++ scoping rules for enums.
 
527
  const string& full_name() const;
 
528
 
 
529
  // The type of this value.  Never NULL.
 
530
  const EnumDescriptor* type() const;
 
531
 
 
532
  // Get options for this enum value.  These are specified in the .proto
 
533
  // file by adding text like "[foo = 1234]" after an enum value definition.
 
534
  // The exact set of known options is defined by EnumValueOptions in
 
535
  // google/protobuf/descriptor.proto.
 
536
  const EnumValueOptions& options() const;
 
537
 
 
538
  // See Descriptor::CopyTo().
 
539
  void CopyTo(EnumValueDescriptorProto* proto) const;
 
540
 
 
541
  // See Descriptor::DebugString().
 
542
  string DebugString() const;
 
543
 
 
544
 private:
 
545
  // See Descriptor::DebugString().
 
546
  void DebugString(int depth, string *contents) const;
 
547
 
 
548
  const string* name_;
 
549
  const string* full_name_;
 
550
  int number_;
 
551
  const EnumDescriptor* type_;
 
552
  const EnumValueOptions* options_;
 
553
 
 
554
  // Must be constructed using DescriptorPool.
 
555
  EnumValueDescriptor() {}
 
556
  friend class DescriptorBuilder;
 
557
  friend class EnumDescriptor;
 
558
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumValueDescriptor);
 
559
};
 
560
 
 
561
// Describes an RPC service.  To get the ServiceDescriptor for a service,
 
562
// call Service::GetDescriptor().  Generated service classes also have a
 
563
// static method called descriptor() which returns the type's
 
564
// ServiceDescriptor.  Use DescriptorPool to construct your own descriptors.
 
565
class LIBPROTOBUF_EXPORT ServiceDescriptor {
 
566
 public:
 
567
  // The name of the service, not including its containing scope.
 
568
  const string& name() const;
 
569
  // The fully-qualified name of the service, scope delimited by periods.
 
570
  const string& full_name() const;
 
571
  // Index of this service within the file's services array.
 
572
  int index() const;
 
573
 
 
574
  // The .proto file in which this service was defined.  Never NULL.
 
575
  const FileDescriptor* file() const;
 
576
 
 
577
  // Get options for this service type.  These are specified in the .proto
 
578
  // file by placing lines like "option foo = 1234;" in the service definition.
 
579
  // The exact set of known options is defined by ServiceOptions in
 
580
  // google/protobuf/descriptor.proto.
 
581
  const ServiceOptions& options() const;
 
582
 
 
583
  // The number of methods this service defines.
 
584
  int method_count() const;
 
585
  // Gets a MethodDescriptor by index, where 0 <= index < method_count().
 
586
  const MethodDescriptor* method(int index) const;
 
587
 
 
588
  // Look up a MethodDescriptor by name.
 
589
  const MethodDescriptor* FindMethodByName(const string& name) const;
 
590
 
 
591
  // See Descriptor::CopyTo().
 
592
  void CopyTo(ServiceDescriptorProto* proto) const;
 
593
 
 
594
  // See Descriptor::DebugString().
 
595
  string DebugString() const;
 
596
 
 
597
 private:
 
598
  // See Descriptor::DebugString().
 
599
  void DebugString(string *contents) const;
 
600
 
 
601
  const string* name_;
 
602
  const string* full_name_;
 
603
  const FileDescriptor* file_;
 
604
  const ServiceOptions* options_;
 
605
  int method_count_;
 
606
  MethodDescriptor* methods_;
 
607
 
 
608
  // Must be constructed using DescriptorPool.
 
609
  ServiceDescriptor() {}
 
610
  friend class DescriptorBuilder;
 
611
  friend class FileDescriptor;
 
612
  friend class MethodDescriptor;
 
613
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ServiceDescriptor);
 
614
};
 
615
 
 
616
// Describes an individual service method.  To obtain a MethodDescriptor given
 
617
// a service, first get its ServiceDescriptor, then call
 
618
// ServiceDescriptor::FindMethodByName().  Use DescriptorPool to construct your
 
619
// own descriptors.
 
620
class LIBPROTOBUF_EXPORT MethodDescriptor {
 
621
 public:
 
622
  // Name of this method, not including containing scope.
 
623
  const string& name() const;
 
624
  // The fully-qualified name of the method, scope delimited by periods.
 
625
  const string& full_name() const;
 
626
  // Index within the service's Descriptor.
 
627
  int index() const;
 
628
 
 
629
  // Gets the service to which this method belongs.  Never NULL.
 
630
  const ServiceDescriptor* service() const;
 
631
 
 
632
  // Gets the type of protocol message which this method accepts as input.
 
633
  const Descriptor* input_type() const;
 
634
  // Gets the type of protocol message which this message produces as output.
 
635
  const Descriptor* output_type() const;
 
636
 
 
637
  // Get options for this method.  These are specified in the .proto
 
638
  // file by placing lines like "option foo = 1234;" in curly-braces after
 
639
  // a method declaration.  The exact set of known options is defined by
 
640
  // MethodOptions in google/protobuf/descriptor.proto.
 
641
  const MethodOptions& options() const;
 
642
 
 
643
  // See Descriptor::CopyTo().
 
644
  void CopyTo(MethodDescriptorProto* proto) const;
 
645
 
 
646
  // See Descriptor::DebugString().
 
647
  string DebugString() const;
 
648
 
 
649
 private:
 
650
  // See Descriptor::DebugString().
 
651
  void DebugString(int depth, string *contents) const;
 
652
 
 
653
  const string* name_;
 
654
  const string* full_name_;
 
655
  const ServiceDescriptor* service_;
 
656
  const Descriptor* input_type_;
 
657
  const Descriptor* output_type_;
 
658
  const MethodOptions* options_;
 
659
 
 
660
  // Must be constructed using DescriptorPool.
 
661
  MethodDescriptor() {}
 
662
  friend class DescriptorBuilder;
 
663
  friend class ServiceDescriptor;
 
664
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MethodDescriptor);
 
665
};
 
666
 
 
667
// Describes a whole .proto file.  To get the FileDescriptor for a compiled-in
 
668
// file, get the descriptor for something defined in that file and call
 
669
// descriptor->file().  Use DescriptorPool to construct your own descriptors.
 
670
class LIBPROTOBUF_EXPORT FileDescriptor {
 
671
 public:
 
672
  // The filename, relative to the source tree.
 
673
  // e.g. "google/protobuf/descriptor.proto"
 
674
  const string& name() const;
 
675
 
 
676
  // The package, e.g. "google.protobuf.compiler".
 
677
  const string& package() const;
 
678
 
 
679
  // The DescriptorPool in which this FileDescriptor and all its contents were
 
680
  // allocated.  Never NULL.
 
681
  const DescriptorPool* pool() const;
 
682
 
 
683
  // The number of files imported by this one.
 
684
  int dependency_count() const;
 
685
  // Gets an imported file by index, where 0 <= index < dependency_count().
 
686
  const FileDescriptor* dependency(int index) const;
 
687
 
 
688
  // Number of top-level message types defined in this file.  (This does not
 
689
  // include nested types.)
 
690
  int message_type_count() const;
 
691
  // Gets a top-level message type, where 0 <= index < message_type_count().
 
692
  const Descriptor* message_type(int index) const;
 
693
 
 
694
  // Number of top-level enum types defined in this file.  (This does not
 
695
  // include nested types.)
 
696
  int enum_type_count() const;
 
697
  // Gets a top-level enum type, where 0 <= index < enum_type_count().
 
698
  const EnumDescriptor* enum_type(int index) const;
 
699
 
 
700
  // Number of services defined in this file.
 
701
  int service_count() const;
 
702
  // Gets a service, where 0 <= index < service_count().
 
703
  const ServiceDescriptor* service(int index) const;
 
704
 
 
705
  // Number of extensions defined at file scope.  (This does not include
 
706
  // extensions nested within message types.)
 
707
  int extension_count() const;
 
708
  // Gets an extension's descriptor, where 0 <= index < extension_count().
 
709
  const FieldDescriptor* extension(int index) const;
 
710
 
 
711
  // Get options for this file.  These are specified in the .proto
 
712
  // file by placing lines like "option foo = 1234;" at the top level, outside
 
713
  // of any other definitions.  The exact set of known options is defined by
 
714
  // FileOptions in google/protobuf/descriptor.proto.
 
715
  const FileOptions& options() const;
 
716
 
 
717
  // Find a top-level message type by name.  Returns NULL if not found.
 
718
  const Descriptor* FindMessageTypeByName(const string& name) const;
 
719
  // Find a top-level enum type by name.  Returns NULL if not found.
 
720
  const EnumDescriptor* FindEnumTypeByName(const string& name) const;
 
721
  // Find an enum value defined in any top-level enum by name.  Returns NULL if
 
722
  // not found.
 
723
  const EnumValueDescriptor* FindEnumValueByName(const string& name) const;
 
724
  // Find a service definition by name.  Returns NULL if not found.
 
725
  const ServiceDescriptor* FindServiceByName(const string& name) const;
 
726
  // Find a top-level extension definition by name.  Returns NULL if not found.
 
727
  const FieldDescriptor* FindExtensionByName(const string& name) const;
 
728
 
 
729
  // See Descriptor::CopyTo().
 
730
  void CopyTo(FileDescriptorProto* proto) const;
 
731
 
 
732
  // See Descriptor::DebugString().
 
733
  string DebugString() const;
 
734
 
 
735
 private:
 
736
  const string* name_;
 
737
  const string* package_;
 
738
  const DescriptorPool* pool_;
 
739
  int dependency_count_;
 
740
  const FileDescriptor** dependencies_;
 
741
  int message_type_count_;
 
742
  Descriptor* message_types_;
 
743
  int enum_type_count_;
 
744
  EnumDescriptor* enum_types_;
 
745
  int service_count_;
 
746
  ServiceDescriptor* services_;
 
747
  int extension_count_;
 
748
  FieldDescriptor* extensions_;
 
749
  const FileOptions* options_;
 
750
 
 
751
  FileDescriptor() {}
 
752
  friend class DescriptorBuilder;
 
753
  friend class Descriptor;
 
754
  friend class FieldDescriptor;
 
755
  friend class EnumDescriptor;
 
756
  friend class ServiceDescriptor;
 
757
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileDescriptor);
 
758
};
 
759
 
 
760
// ===================================================================
 
761
 
 
762
// Used to construct descriptors.
 
763
//
 
764
// Normally you won't want to build your own descriptors.  Message classes
 
765
// constructed by the protocol compiler will provide them for you.  However,
 
766
// if you are implementing Message on your own, or if you are writing a
 
767
// program which can operate on totally arbitrary types and needs to load
 
768
// them from some sort of database, you might need to.
 
769
//
 
770
// Since Descriptors are composed of a whole lot of cross-linked bits of
 
771
// data that would be a pain to put together manually, the
 
772
// DescriptorPool class is provided to make the process easier.  It can
 
773
// take a FileDescriptorProto (defined in descriptor.proto), validate it,
 
774
// and convert it to a set of nicely cross-linked Descriptors.
 
775
//
 
776
// DescriptorPool also helps with memory management.  Descriptors are
 
777
// composed of many objects containing static data and pointers to each
 
778
// other.  In all likelihood, when it comes time to delete this data,
 
779
// you'll want to delete it all at once.  In fact, it is not uncommon to
 
780
// have a whole pool of descriptors all cross-linked with each other which
 
781
// you wish to delete all at once.  This class represents such a pool, and
 
782
// handles the memory management for you.
 
783
//
 
784
// You can also search for descriptors within a DescriptorPool by name, and
 
785
// extensions by number.
 
786
class LIBPROTOBUF_EXPORT DescriptorPool {
 
787
 public:
 
788
  // Create a normal, empty DescriptorPool.
 
789
  DescriptorPool();
 
790
 
 
791
  // Constructs a DescriptorPool that, when it can't find something among the
 
792
  // descriptors already in the pool, looks for it in the given
 
793
  // DescriptorDatabase.
 
794
  // Notes:
 
795
  // - If a DescriptorPool is constructed this way, its BuildFile*() methods
 
796
  //   must not be called (they will assert-fail).  The only way to populate
 
797
  //   the pool with descriptors is to call the Find*By*() methods.
 
798
  // - The Find*By*() methods may block the calling thread if the
 
799
  //   DescriptorDatabase blocks.  This in turn means that parsing messages
 
800
  //   may block if they need to look up extensions.
 
801
  // - The Find*By*() methods will use mutexes for thread-safety, thus making
 
802
  //   them slower even when they don't have to fall back to the database.
 
803
  //   In fact, even the Find*By*() methods of descriptor objects owned by
 
804
  //   this pool will be slower, since they will have to obtain locks too.
 
805
  // - An ErrorCollector may optionally be given to collect validation errors
 
806
  //   in files loaded from the database.  If not given, errors will be printed
 
807
  //   to GOOGLE_LOG(ERROR).  Remember that files are built on-demand, so this
 
808
  //   ErrorCollector may be called from any thread that calls one of the
 
809
  //   Find*By*() methods.
 
810
  class ErrorCollector;
 
811
  explicit DescriptorPool(DescriptorDatabase* fallback_database,
 
812
                          ErrorCollector* error_collector = NULL);
 
813
 
 
814
  ~DescriptorPool();
 
815
 
 
816
  // Get a pointer to the generated pool.  Generated protocol message classes
 
817
  // which are compiled into the binary will allocate their descriptors in
 
818
  // this pool.  Do not add your own descriptors to this pool.
 
819
  static const DescriptorPool* generated_pool();
 
820
 
 
821
  // Find a FileDescriptor in the pool by file name.  Returns NULL if not
 
822
  // found.
 
823
  const FileDescriptor* FindFileByName(const string& name) const;
 
824
 
 
825
  // Find the FileDescriptor in the pool which defines the given symbol.
 
826
  // If any of the Find*ByName() methods below would succeed, then this is
 
827
  // equivalent to calling that method and calling the result's file() method.
 
828
  // Otherwise this returns NULL.
 
829
  const FileDescriptor* FindFileContainingSymbol(
 
830
      const string& symbol_name) const;
 
831
 
 
832
  // Looking up descriptors ------------------------------------------
 
833
  // These find descriptors by fully-qualified name.  These will find both
 
834
  // top-level descriptors and nested descriptors.  They return NULL if not
 
835
  // found.
 
836
 
 
837
  const Descriptor* FindMessageTypeByName(const string& name) const;
 
838
  const FieldDescriptor* FindFieldByName(const string& name) const;
 
839
  const FieldDescriptor* FindExtensionByName(const string& name) const;
 
840
  const EnumDescriptor* FindEnumTypeByName(const string& name) const;
 
841
  const EnumValueDescriptor* FindEnumValueByName(const string& name) const;
 
842
  const ServiceDescriptor* FindServiceByName(const string& name) const;
 
843
  const MethodDescriptor* FindMethodByName(const string& name) const;
 
844
 
 
845
  // Finds an extension of the given type by number.  The extendee must be
 
846
  // a member of this DescriptorPool or one of its underlays.
 
847
  const FieldDescriptor* FindExtensionByNumber(const Descriptor* extendee,
 
848
                                               int number) const;
 
849
 
 
850
  // Building descriptors --------------------------------------------
 
851
 
 
852
  // When converting a FileDescriptorProto to a FileDescriptor, various
 
853
  // errors might be detected in the input.  The caller may handle these
 
854
  // programmatically by implementing an ErrorCollector.
 
855
  class LIBPROTOBUF_EXPORT ErrorCollector {
 
856
   public:
 
857
    inline ErrorCollector() {}
 
858
    virtual ~ErrorCollector();
 
859
 
 
860
    // These constants specify what exact part of the construct is broken.
 
861
    // This is useful e.g. for mapping the error back to an exact location
 
862
    // in a .proto file.
 
863
    enum ErrorLocation {
 
864
      NAME,              // the symbol name, or the package name for files
 
865
      NUMBER,            // field or extension range number
 
866
      TYPE,              // field type
 
867
      EXTENDEE,          // field extendee
 
868
      DEFAULT_VALUE,     // field default value
 
869
      INPUT_TYPE,        // method input type
 
870
      OUTPUT_TYPE,       // method output type
 
871
      OTHER              // some other problem
 
872
    };
 
873
 
 
874
    // Reports an error in the FileDescriptorProto.
 
875
    virtual void AddError(
 
876
      const string& filename,      // File name in which the error occurred.
 
877
      const string& element_name,  // Full name of the erroneous element.
 
878
      const Message* descriptor,   // Descriptor of the erroneous element.
 
879
      ErrorLocation location,      // One of the location constants, above.
 
880
      const string& message        // Human-readable error message.
 
881
      ) = 0;
 
882
 
 
883
   private:
 
884
    GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ErrorCollector);
 
885
  };
 
886
 
 
887
  // Convert the FileDescriptorProto to real descriptors and place them in
 
888
  // this DescriptorPool.  All dependencies of the file must already be in
 
889
  // the pool.  Returns the resulting FileDescriptor, or NULL if there were
 
890
  // problems with the input (e.g. the message was invalid, or dependencies
 
891
  // were missing).  Details about the errors are written to GOOGLE_LOG(ERROR).
 
892
  const FileDescriptor* BuildFile(const FileDescriptorProto& proto);
 
893
 
 
894
  // Same as BuildFile() except errors are sent to the given ErrorCollector.
 
895
  const FileDescriptor* BuildFileCollectingErrors(
 
896
    const FileDescriptorProto& proto,
 
897
    ErrorCollector* error_collector);
 
898
 
 
899
  // Internal stuff --------------------------------------------------
 
900
  // These methods MUST NOT be called from outside the proto2 library.
 
901
  // These methods may contain hidden pitfalls and may be removed in a
 
902
  // future library version.
 
903
 
 
904
  // DEPRECATED:  Use of underlays can lead to many subtle gotchas.  Instead,
 
905
  //   try to formulate what you want to do in terms of DescriptorDatabases.
 
906
  //   This constructor will be removed soon.
 
907
  //
 
908
  // Create a DescriptorPool which is overlaid on top of some other pool.
 
909
  // If you search for a descriptor in the overlay and it is not found, the
 
910
  // underlay will be searched as a backup.  If the underlay has its own
 
911
  // underlay, that will be searched next, and so on.  This also means that
 
912
  // files built in the overlay will be cross-linked with the underlay's
 
913
  // descriptors if necessary.  The underlay remains property of the caller;
 
914
  // it must remain valid for the lifetime of the newly-constructed pool.
 
915
  //
 
916
  // Example:  Say you want to parse a .proto file at runtime in order to use
 
917
  // its type with a DynamicMessage.  Say this .proto file has dependencies,
 
918
  // but you know that all the dependencies will be things that are already
 
919
  // compiled into the binary.  For ease of use, you'd like to load the types
 
920
  // right out of generated_pool() rather than have to parse redundant copies
 
921
  // of all these .protos and runtime.  But, you don't want to add the parsed
 
922
  // types directly into generated_pool(): this is not allowed, and would be
 
923
  // bad design anyway.  So, instead, you could use generated_pool() as an
 
924
  // underlay for a new DescriptorPool in which you add only the new file.
 
925
  explicit DescriptorPool(const DescriptorPool* underlay);
 
926
 
 
927
  // Called by generated classes at init time.  Do NOT call this in your
 
928
  // own code!
 
929
  const FileDescriptor* InternalBuildGeneratedFile(
 
930
    const void* data, int size);
 
931
 
 
932
  // For internal use only:  Gets a non-const pointer to the generated pool.
 
933
  // This is called at static-initialization time only, so thread-safety is
 
934
  // not a concern.  If both an underlay and a fallback database are present,
 
935
  // the fallback database takes precedence.
 
936
  static DescriptorPool* internal_generated_pool();
 
937
 
 
938
  // For internal use only:  Changes the behavior of BuildFile() such that it
 
939
  // allows the file to make reference to message types declared in other files
 
940
  // which it did not officially declare as dependencies.
 
941
  void InternalDontEnforceDependencies();
 
942
 
 
943
  // For internal use only.
 
944
  void internal_set_underlay(const DescriptorPool* underlay) {
 
945
    underlay_ = underlay;
 
946
  }
 
947
 
 
948
 private:
 
949
  friend class Descriptor;
 
950
  friend class FieldDescriptor;
 
951
  friend class EnumDescriptor;
 
952
  friend class ServiceDescriptor;
 
953
  friend class FileDescriptor;
 
954
  friend class DescriptorBuilder;
 
955
 
 
956
  // Tries to find something in the fallback database and link in the
 
957
  // corresponding proto file.  Returns true if successful, in which case
 
958
  // the caller should search for the thing again.  These are declared
 
959
  // const because they are called by (semantically) const methods.
 
960
  bool TryFindFileInFallbackDatabase(const string& name) const;
 
961
  bool TryFindSymbolInFallbackDatabase(const string& name) const;
 
962
  bool TryFindExtensionInFallbackDatabase(const Descriptor* containing_type,
 
963
                                          int field_number) const;
 
964
 
 
965
  // Like BuildFile() but called internally when the file has been loaded from
 
966
  // fallback_database_.  Declared const because it is called by (semantically)
 
967
  // const methods.
 
968
  const FileDescriptor* BuildFileFromDatabase(
 
969
    const FileDescriptorProto& proto) const;
 
970
 
 
971
  // If fallback_database_ is NULL, this is NULL.  Otherwise, this is a mutex
 
972
  // which must be locked while accessing tables_.
 
973
  Mutex* mutex_;
 
974
 
 
975
  // See constructor.
 
976
  DescriptorDatabase* fallback_database_;
 
977
  ErrorCollector* default_error_collector_;
 
978
  const DescriptorPool* underlay_;
 
979
 
 
980
  // This class contains a lot of hash maps with complicated types that
 
981
  // we'd like to keep out of the header.
 
982
  class Tables;
 
983
  scoped_ptr<Tables> tables_;
 
984
 
 
985
  bool enforce_dependencies_;
 
986
 
 
987
  // See InternalBuildGeneratedFile().
 
988
  const void* last_internal_build_generated_file_call_;
 
989
 
 
990
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorPool);
 
991
};
 
992
 
 
993
// inline methods ====================================================
 
994
 
 
995
// These macros makes this repetitive code more readable.
 
996
#define PROTOBUF_DEFINE_ACCESSOR(CLASS, FIELD, TYPE) \
 
997
  inline TYPE CLASS::FIELD() const { return FIELD##_; }
 
998
 
 
999
// Strings fields are stored as pointers but returned as const references.
 
1000
#define PROTOBUF_DEFINE_STRING_ACCESSOR(CLASS, FIELD) \
 
1001
  inline const string& CLASS::FIELD() const { return *FIELD##_; }
 
1002
 
 
1003
// Arrays take an index parameter, obviously.
 
1004
#define PROTOBUF_DEFINE_ARRAY_ACCESSOR(CLASS, FIELD, TYPE) \
 
1005
  inline TYPE CLASS::FIELD(int index) const { return FIELD##s_ + index; }
 
1006
 
 
1007
#define PROTOBUF_DEFINE_OPTIONS_ACCESSOR(CLASS, TYPE) \
 
1008
  inline const TYPE& CLASS::options() const { return *options_; }
 
1009
 
 
1010
PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, name)
 
1011
PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, full_name)
 
1012
PROTOBUF_DEFINE_ACCESSOR(Descriptor, file, const FileDescriptor*)
 
1013
PROTOBUF_DEFINE_ACCESSOR(Descriptor, containing_type, const Descriptor*)
 
1014
 
 
1015
PROTOBUF_DEFINE_ACCESSOR(Descriptor, field_count, int)
 
1016
PROTOBUF_DEFINE_ACCESSOR(Descriptor, nested_type_count, int)
 
1017
PROTOBUF_DEFINE_ACCESSOR(Descriptor, enum_type_count, int)
 
1018
 
 
1019
PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, field, const FieldDescriptor*)
 
1020
PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, nested_type, const Descriptor*)
 
1021
PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, enum_type, const EnumDescriptor*)
 
1022
 
 
1023
PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_range_count, int)
 
1024
PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_count, int)
 
1025
PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension_range,
 
1026
                               const Descriptor::ExtensionRange*)
 
1027
PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension,
 
1028
                               const FieldDescriptor*)
 
1029
PROTOBUF_DEFINE_OPTIONS_ACCESSOR(Descriptor, MessageOptions);
 
1030
 
 
1031
PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, name)
 
1032
PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, full_name)
 
1033
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, file, const FileDescriptor*)
 
1034
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, number, int)
 
1035
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, is_extension, bool)
 
1036
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, type, FieldDescriptor::Type)
 
1037
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, label, FieldDescriptor::Label)
 
1038
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, containing_type, const Descriptor*)
 
1039
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, extension_scope, const Descriptor*)
 
1040
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, message_type, const Descriptor*)
 
1041
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, enum_type, const EnumDescriptor*)
 
1042
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, experimental_map_key,
 
1043
                         const FieldDescriptor*)
 
1044
PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FieldDescriptor, FieldOptions);
 
1045
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_default_value, bool)
 
1046
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int32 , int32 )
 
1047
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int64 , int64 )
 
1048
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint32, uint32)
 
1049
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint64, uint64)
 
1050
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_float , float )
 
1051
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_double, double)
 
1052
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_bool  , bool  )
 
1053
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_enum,
 
1054
                         const EnumValueDescriptor*)
 
1055
PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, default_value_string)
 
1056
 
 
1057
PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, name)
 
1058
PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, full_name)
 
1059
PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, file, const FileDescriptor*)
 
1060
PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, containing_type, const Descriptor*)
 
1061
PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, value_count, int)
 
1062
PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, value,
 
1063
                               const EnumValueDescriptor*)
 
1064
PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumDescriptor, EnumOptions);
 
1065
 
 
1066
PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, name)
 
1067
PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, full_name)
 
1068
PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, number, int)
 
1069
PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, type, const EnumDescriptor*)
 
1070
PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumValueDescriptor, EnumValueOptions);
 
1071
 
 
1072
PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, name)
 
1073
PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, full_name)
 
1074
PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, file, const FileDescriptor*)
 
1075
PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, method_count, int)
 
1076
PROTOBUF_DEFINE_ARRAY_ACCESSOR(ServiceDescriptor, method,
 
1077
                               const MethodDescriptor*)
 
1078
PROTOBUF_DEFINE_OPTIONS_ACCESSOR(ServiceDescriptor, ServiceOptions);
 
1079
 
 
1080
PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, name)
 
1081
PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, full_name)
 
1082
PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, service, const ServiceDescriptor*)
 
1083
PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, input_type, const Descriptor*)
 
1084
PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, output_type, const Descriptor*)
 
1085
PROTOBUF_DEFINE_OPTIONS_ACCESSOR(MethodDescriptor, MethodOptions);
 
1086
 
 
1087
PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, name)
 
1088
PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, package)
 
1089
PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, pool, const DescriptorPool*)
 
1090
PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, dependency_count, int)
 
1091
PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, message_type_count, int)
 
1092
PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, enum_type_count, int)
 
1093
PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, service_count, int)
 
1094
PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, extension_count, int)
 
1095
PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FileDescriptor, FileOptions);
 
1096
 
 
1097
PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, message_type, const Descriptor*)
 
1098
PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, enum_type, const EnumDescriptor*)
 
1099
PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, service,
 
1100
                               const ServiceDescriptor*)
 
1101
PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, extension,
 
1102
                               const FieldDescriptor*)
 
1103
 
 
1104
#undef PROTOBUF_DEFINE_ACCESSOR
 
1105
#undef PROTOBUF_DEFINE_STRING_ACCESSOR
 
1106
#undef PROTOBUF_DEFINE_ARRAY_ACCESSOR
 
1107
 
 
1108
// A few accessors differ from the macros...
 
1109
 
 
1110
inline bool FieldDescriptor::is_required() const {
 
1111
  return label() == LABEL_REQUIRED;
 
1112
}
 
1113
 
 
1114
inline bool FieldDescriptor::is_optional() const {
 
1115
  return label() == LABEL_OPTIONAL;
 
1116
}
 
1117
 
 
1118
inline bool FieldDescriptor::is_repeated() const {
 
1119
  return label() == LABEL_REPEATED;
 
1120
}
 
1121
 
 
1122
// To save space, index() is computed by looking at the descriptor's position
 
1123
// in the parent's array of children.
 
1124
inline int FieldDescriptor::index() const {
 
1125
  if (!is_extension_) {
 
1126
    return this - containing_type_->fields_;
 
1127
  } else if (extension_scope_ != NULL) {
 
1128
    return this - extension_scope_->extensions_;
 
1129
  } else {
 
1130
    return this - file_->extensions_;
 
1131
  }
 
1132
}
 
1133
 
 
1134
inline int Descriptor::index() const {
 
1135
  if (containing_type_ == NULL) {
 
1136
    return this - file_->message_types_;
 
1137
  } else {
 
1138
    return this - containing_type_->nested_types_;
 
1139
  }
 
1140
}
 
1141
 
 
1142
inline int EnumDescriptor::index() const {
 
1143
  if (containing_type_ == NULL) {
 
1144
    return this - file_->enum_types_;
 
1145
  } else {
 
1146
    return this - containing_type_->enum_types_;
 
1147
  }
 
1148
}
 
1149
 
 
1150
inline int EnumValueDescriptor::index() const {
 
1151
  return this - type_->values_;
 
1152
}
 
1153
 
 
1154
inline int ServiceDescriptor::index() const {
 
1155
  return this - file_->services_;
 
1156
}
 
1157
 
 
1158
inline int MethodDescriptor::index() const {
 
1159
  return this - service_->methods_;
 
1160
}
 
1161
 
 
1162
inline FieldDescriptor::CppType FieldDescriptor::cpp_type() const {
 
1163
  return kTypeToCppTypeMap[type_];
 
1164
}
 
1165
 
 
1166
inline const FileDescriptor* FileDescriptor::dependency(int index) const {
 
1167
  return dependencies_[index];
 
1168
}
 
1169
 
 
1170
}  // namespace protobuf
 
1171
 
 
1172
}  // namespace google
 
1173
#endif  // GOOGLE_PROTOBUF_DESCRIPTOR_H__