My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
Changes to /trunk/src/google/protobuf/compiler/python/python_generator.h
r349 vs. r425 Compare: vs.  Format:
Revision r425
Go to: 
Project members, sign in to write a code review
/trunk/src/google/protobuf/compiler/python/python_generator.h   r349 /trunk/src/google/protobuf/compiler/python/python_generator.h   r425
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/ 3 // http://code.google.com/p/protobuf/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above 11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer 12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the 13 // in the documentation and/or other materials provided with the
14 // distribution. 14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its 15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from 16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission. 17 // this software without specific prior written permission.
18 // 18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 30
31 // Author: robinson@google.com (Will Robinson) 31 // Author: robinson@google.com (Will Robinson)
32 // 32 //
33 // Generates Python code for a given .proto file. 33 // Generates Python code for a given .proto file.
34 34
35 #ifndef GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__ 35 #ifndef GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__
36 #define GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__ 36 #define GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__
37 37
38 #include <string> 38 #include <string>
39 39
40 #include <google/protobuf/compiler/code_generator.h> 40 #include <google/protobuf/compiler/code_generator.h>
41 #include <google/protobuf/stubs/common.h> 41 #include <google/protobuf/stubs/common.h>
42 42
43 namespace google { 43 namespace google {
44 namespace protobuf { 44 namespace protobuf {
45 45
46 class Descriptor; 46 class Descriptor;
47 class EnumDescriptor; 47 class EnumDescriptor;
48 class EnumValueDescriptor; 48 class EnumValueDescriptor;
49 class FieldDescriptor; 49 class FieldDescriptor;
50 class ServiceDescriptor; 50 class ServiceDescriptor;
51 51
52 namespace io { class Printer; } 52 namespace io { class Printer; }
53 53
54 namespace compiler { 54 namespace compiler {
55 namespace python { 55 namespace python {
56 56
57 // CodeGenerator implementation for generated Python protocol buffer classes. 57 // CodeGenerator implementation for generated Python protocol buffer classes.
58 // If you create your own protocol compiler binary and you want it to support 58 // If you create your own protocol compiler binary and you want it to support
59 // Python output, you can do so by registering an instance of this 59 // Python output, you can do so by registering an instance of this
60 // CodeGenerator with the CommandLineInterface in your main() function. 60 // CodeGenerator with the CommandLineInterface in your main() function.
61 class LIBPROTOC_EXPORT Generator : public CodeGenerator { 61 class LIBPROTOC_EXPORT Generator : public CodeGenerator {
62 public: 62 public:
63 Generator(); 63 Generator();
64 virtual ~Generator(); 64 virtual ~Generator();
65 65
66 // CodeGenerator methods. 66 // CodeGenerator methods.
67 virtual bool Generate(const FileDescriptor* file, 67 virtual bool Generate(const FileDescriptor* file,
68 const string& parameter, 68 const string& parameter,
69 GeneratorContext* generator_context, 69 GeneratorContext* generator_context,
70 string* error) const; 70 string* error) const;
71 71
72 private: 72 private:
73 void PrintImports() const; 73 void PrintImports() const;
74 void PrintFileDescriptor() const; 74 void PrintFileDescriptor() const;
75 void PrintTopLevelEnums() const; 75 void PrintTopLevelEnums() const;
76 void PrintAllNestedEnumsInFile() const; 76 void PrintAllNestedEnumsInFile() const;
77 void PrintNestedEnums(const Descriptor& descriptor) const; 77 void PrintNestedEnums(const Descriptor& descriptor) const;
78 void PrintEnum(const EnumDescriptor& enum_descriptor) const; 78 void PrintEnum(const EnumDescriptor& enum_descriptor) const;
79 79
80 void PrintTopLevelExtensions() const; 80 void PrintTopLevelExtensions() const;
81 81
82 void PrintFieldDescriptor( 82 void PrintFieldDescriptor(
83 const FieldDescriptor& field, bool is_extension) const; 83 const FieldDescriptor& field, bool is_extension) const;
84 void PrintFieldDescriptorsInDescriptor( 84 void PrintFieldDescriptorsInDescriptor(
85 const Descriptor& message_descriptor, 85 const Descriptor& message_descriptor,
86 bool is_extension, 86 bool is_extension,
87 const string& list_variable_name, 87 const string& list_variable_name,
88 int (Descriptor::*CountFn)() const, 88 int (Descriptor::*CountFn)() const,
89 const FieldDescriptor* (Descriptor::*GetterFn)(int) const) const; 89 const FieldDescriptor* (Descriptor::*GetterFn)(int) const) const;
90 void PrintFieldsInDescriptor(const Descriptor& message_descriptor) const; 90 void PrintFieldsInDescriptor(const Descriptor& message_descriptor) const;
91 void PrintExtensionsInDescriptor(const Descriptor& message_descriptor) const; 91 void PrintExtensionsInDescriptor(const Descriptor& message_descriptor) const;
92 void PrintMessageDescriptors() const; 92 void PrintMessageDescriptors() const;
93 void PrintDescriptor(const Descriptor& message_descriptor) const; 93 void PrintDescriptor(const Descriptor& message_descriptor) const;
94 void PrintNestedDescriptors(const Descriptor& containing_descriptor) const; 94 void PrintNestedDescriptors(const Descriptor& containing_descriptor) const;
95 95
96 void PrintMessages() const; 96 void PrintMessages() const;
97 void PrintMessage(const Descriptor& message_descriptor) const; 97 void PrintMessage(const Descriptor& message_descriptor) const;
98 void PrintNestedMessages(const Descriptor& containing_descriptor) const; 98 void PrintNestedMessages(const Descriptor& containing_descriptor) const;
99 99
100 void FixForeignFieldsInDescriptors() const; 100 void FixForeignFieldsInDescriptors() const;
101 void FixForeignFieldsInDescriptor( 101 void FixForeignFieldsInDescriptor(
102 const Descriptor& descriptor, 102 const Descriptor& descriptor,
103 const Descriptor* containing_descriptor) const; 103 const Descriptor* containing_descriptor) const;
104 void FixForeignFieldsInField(const Descriptor* containing_type, 104 void FixForeignFieldsInField(const Descriptor* containing_type,
105 const FieldDescriptor& field, 105 const FieldDescriptor& field,
106 const string& python_dict_name) const; 106 const string& python_dict_name) const;
107 void AddMessageToFileDescriptor(const Descriptor& descriptor) const; 107 void AddMessageToFileDescriptor(const Descriptor& descriptor) const;
108 string FieldReferencingExpression(const Descriptor* containing_type, 108 string FieldReferencingExpression(const Descriptor* containing_type,
109 const FieldDescriptor& field, 109 const FieldDescriptor& field,
110 const string& python_dict_name) const; 110 const string& python_dict_name) const;
111 template <typename DescriptorT> 111 template <typename DescriptorT>
112 void FixContainingTypeInDescriptor( 112 void FixContainingTypeInDescriptor(
113 const DescriptorT& descriptor, 113 const DescriptorT& descriptor,
114 const Descriptor* containing_descriptor) const; 114 const Descriptor* containing_descriptor) const;
115 115
116 void FixForeignFieldsInExtensions() const; 116 void FixForeignFieldsInExtensions() const;
117 void FixForeignFieldsInExtension( 117 void FixForeignFieldsInExtension(
118 const FieldDescriptor& extension_field) const; 118 const FieldDescriptor& extension_field) const;
119 void FixForeignFieldsInNestedExtensions(const Descriptor& descriptor) const; 119 void FixForeignFieldsInNestedExtensions(const Descriptor& descriptor) const;
120 120
121 void PrintServices() const; 121 void PrintServices() const;
122 void PrintServiceDescriptor(const ServiceDescriptor& descriptor) const; 122 void PrintServiceDescriptor(const ServiceDescriptor& descriptor) const;
123 void PrintServiceClass(const ServiceDescriptor& descriptor) const; 123 void PrintServiceClass(const ServiceDescriptor& descriptor) const;
124 void PrintServiceStub(const ServiceDescriptor& descriptor) const; 124 void PrintServiceStub(const ServiceDescriptor& descriptor) const;
125 125
126 void PrintEnumValueDescriptor(const EnumValueDescriptor& descriptor) const; 126 void PrintEnumValueDescriptor(const EnumValueDescriptor& descriptor) const;
127 string OptionsValue(const string& class_name, 127 string OptionsValue(const string& class_name,
128 const string& serialized_options) const; 128 const string& serialized_options) const;
129 bool GeneratingDescriptorProto() const; 129 bool GeneratingDescriptorProto() const;
130 130
131 template <typename DescriptorT> 131 template <typename DescriptorT>
132 string ModuleLevelDescriptorName(const DescriptorT& descriptor) const; 132 string ModuleLevelDescriptorName(const DescriptorT& descriptor) const;
133 string ModuleLevelMessageName(const Descriptor& descriptor) const; 133 string ModuleLevelMessageName(const Descriptor& descriptor) const;
134 string ModuleLevelServiceDescriptorName( 134 string ModuleLevelServiceDescriptorName(
135 const ServiceDescriptor& descriptor) const; 135 const ServiceDescriptor& descriptor) const;
136 136
137 template <typename DescriptorT, typename DescriptorProtoT> 137 template <typename DescriptorT, typename DescriptorProtoT>
138 void PrintSerializedPbInterval( 138 void PrintSerializedPbInterval(
139 const DescriptorT& descriptor, DescriptorProtoT& proto) const; 139 const DescriptorT& descriptor, DescriptorProtoT& proto) const;
140 140
141 void FixAllDescriptorOptions() const;
142 void FixOptionsForField(const FieldDescriptor& field) const;
143 void FixOptionsForEnum(const EnumDescriptor& descriptor) const;
144 void FixOptionsForMessage(const Descriptor& descriptor) const;
145
141 // Very coarse-grained lock to ensure that Generate() is reentrant. 146 // Very coarse-grained lock to ensure that Generate() is reentrant.
142 // Guards file_, printer_ and file_descriptor_serialized_. 147 // Guards file_, printer_ and file_descriptor_serialized_.
143 mutable Mutex mutex_; 148 mutable Mutex mutex_;
144 mutable const FileDescriptor* file_; // Set in Generate(). Under mutex_. 149 mutable const FileDescriptor* file_; // Set in Generate(). Under mutex_.
145 mutable string file_descriptor_serialized_; 150 mutable string file_descriptor_serialized_;
146 mutable io::Printer* printer_; // Set in Generate(). Under mutex_. 151 mutable io::Printer* printer_; // Set in Generate(). Under mutex_.
147 152
148 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Generator); 153 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Generator);
149 }; 154 };
150 155
151 } // namespace python 156 } // namespace python
152 } // namespace compiler 157 } // namespace compiler
153 } // namespace protobuf 158 } // namespace protobuf
154 159
155 } // namespace google 160 } // namespace google
156 #endif // GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__ 161 #endif // GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__
Powered by Google Project Hosting