BWAPI
Undermind/proxy/cpp/include/google/protobuf/compiler/python/python_generator.h
Go to the documentation of this file.
00001 // Protocol Buffers - Google's data interchange format
00002 // Copyright 2008 Google Inc.  All rights reserved.
00003 // http://code.google.com/p/protobuf/
00004 //
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are
00007 // met:
00008 //
00009 //     * Redistributions of source code must retain the above copyright
00010 // notice, this list of conditions and the following disclaimer.
00011 //     * Redistributions in binary form must reproduce the above
00012 // copyright notice, this list of conditions and the following disclaimer
00013 // in the documentation and/or other materials provided with the
00014 // distribution.
00015 //     * Neither the name of Google Inc. nor the names of its
00016 // contributors may be used to endorse or promote products derived from
00017 // this software without specific prior written permission.
00018 //
00019 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00022 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00023 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00025 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00026 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00027 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00029 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 
00031 // Author: robinson@google.com (Will Robinson)
00032 //
00033 // Generates Python code for a given .proto file.
00034 
00035 #ifndef GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__
00036 #define GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__
00037 
00038 #include <string>
00039 
00040 #include <google/protobuf/compiler/code_generator.h>
00041 #include <google/protobuf/stubs/common.h>
00042 
00043 namespace google {
00044 namespace protobuf {
00045 
00046 class Descriptor;
00047 class EnumDescriptor;
00048 class EnumValueDescriptor;
00049 class FieldDescriptor;
00050 class ServiceDescriptor;
00051 
00052 namespace io { class Printer; }
00053 
00054 namespace compiler {
00055 namespace python {
00056 
00057 // CodeGenerator implementation for generated Python protocol buffer classes.
00058 // If you create your own protocol compiler binary and you want it to support
00059 // Python output, you can do so by registering an instance of this
00060 // CodeGenerator with the CommandLineInterface in your main() function.
00061 class LIBPROTOC_EXPORT Generator : public CodeGenerator {
00062  public:
00063   Generator();
00064   virtual ~Generator();
00065 
00066   // CodeGenerator methods.
00067   virtual bool Generate(const FileDescriptor* file,
00068                         const string& parameter,
00069                         OutputDirectory* output_directory,
00070                         string* error) const;
00071 
00072  private:
00073   void PrintImports() const;
00074   void PrintFileDescriptor() const;
00075   void PrintTopLevelEnums() const;
00076   void PrintAllNestedEnumsInFile() const;
00077   void PrintNestedEnums(const Descriptor& descriptor) const;
00078   void PrintEnum(const EnumDescriptor& enum_descriptor) const;
00079 
00080   void PrintTopLevelExtensions() const;
00081 
00082   void PrintFieldDescriptor(
00083       const FieldDescriptor& field, bool is_extension) const;
00084   void PrintFieldDescriptorsInDescriptor(
00085       const Descriptor& message_descriptor,
00086       bool is_extension,
00087       const string& list_variable_name,
00088       int (Descriptor::*CountFn)() const,
00089       const FieldDescriptor* (Descriptor::*GetterFn)(int) const) const;
00090   void PrintFieldsInDescriptor(const Descriptor& message_descriptor) const;
00091   void PrintExtensionsInDescriptor(const Descriptor& message_descriptor) const;
00092   void PrintMessageDescriptors() const;
00093   void PrintDescriptor(const Descriptor& message_descriptor) const;
00094   void PrintNestedDescriptors(const Descriptor& containing_descriptor) const;
00095 
00096   void PrintMessages() const;
00097   void PrintMessage(const Descriptor& message_descriptor) const;
00098   void PrintNestedMessages(const Descriptor& containing_descriptor) const;
00099 
00100   void FixForeignFieldsInDescriptors() const;
00101   void FixForeignFieldsInDescriptor(
00102       const Descriptor& descriptor,
00103       const Descriptor* containing_descriptor) const;
00104   void FixForeignFieldsInField(const Descriptor* containing_type,
00105                                const FieldDescriptor& field,
00106                                const string& python_dict_name) const;
00107   string FieldReferencingExpression(const Descriptor* containing_type,
00108                                     const FieldDescriptor& field,
00109                                     const string& python_dict_name) const;
00110   template <typename DescriptorT>
00111   void FixContainingTypeInDescriptor(
00112       const DescriptorT& descriptor,
00113       const Descriptor* containing_descriptor) const;
00114 
00115   void FixForeignFieldsInExtensions() const;
00116   void FixForeignFieldsInExtension(
00117       const FieldDescriptor& extension_field) const;
00118   void FixForeignFieldsInNestedExtensions(const Descriptor& descriptor) const;
00119 
00120   void PrintServices() const;
00121   void PrintServiceDescriptor(const ServiceDescriptor& descriptor) const;
00122   void PrintServiceClass(const ServiceDescriptor& descriptor) const;
00123   void PrintServiceStub(const ServiceDescriptor& descriptor) const;
00124 
00125   void PrintEnumValueDescriptor(const EnumValueDescriptor& descriptor) const;
00126   string OptionsValue(const string& class_name,
00127                       const string& serialized_options) const;
00128   bool GeneratingDescriptorProto() const;
00129 
00130   template <typename DescriptorT>
00131   string ModuleLevelDescriptorName(const DescriptorT& descriptor) const;
00132   string ModuleLevelMessageName(const Descriptor& descriptor) const;
00133   string ModuleLevelServiceDescriptorName(
00134       const ServiceDescriptor& descriptor) const;
00135 
00136   template <typename DescriptorT, typename DescriptorProtoT>
00137   void PrintSerializedPbInterval(
00138       const DescriptorT& descriptor, DescriptorProtoT& proto) const;
00139 
00140   // Very coarse-grained lock to ensure that Generate() is reentrant.
00141   // Guards file_, printer_ and file_descriptor_serialized_.
00142   mutable Mutex mutex_;
00143   mutable const FileDescriptor* file_;  // Set in Generate().  Under mutex_.
00144   mutable string file_descriptor_serialized_;
00145   mutable io::Printer* printer_;  // Set in Generate().  Under mutex_.
00146 
00147   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Generator);
00148 };
00149 
00150 }  // namespace python
00151 }  // namespace compiler
00152 }  // namespace protobuf
00153 
00154 }  // namespace google
00155 #endif  // GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines