1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
35 // Defines the abstract interface implemented by each of the language-specific
38 #ifndef GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__
39 #define GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__
44 #include <google/protobuf/stubs/common.h>
46 #include <google/protobuf/port_def.inc>
52 class ZeroCopyOutputStream;
61 // Defined in this file.
63 class GeneratorContext;
65 // The abstract interface to a class which generates code implementing a
66 // particular proto file in a particular language. A number of these may
67 // be registered with CommandLineInterface to support various languages.
68 class PROTOC_EXPORT CodeGenerator {
70 inline CodeGenerator() {}
71 virtual ~CodeGenerator();
73 // Generates code for the given proto file, generating one or more files in
74 // the given output directory.
76 // A parameter to be passed to the generator can be specified on the command
77 // line. This is intended to be used to pass generator specific parameters.
78 // It is empty if no parameter was given. ParseGeneratorParameter (below),
79 // can be used to accept multiple parameters within the single parameter
82 // Returns true if successful. Otherwise, sets *error to a description of
83 // the problem (e.g. "invalid parameter") and returns false.
84 virtual bool Generate(const FileDescriptor* file,
85 const std::string& parameter,
86 GeneratorContext* generator_context,
87 std::string* error) const = 0;
89 // Generates code for all given proto files.
91 // WARNING: The canonical code generator design produces one or two output
92 // files per input .proto file, and we do not wish to encourage alternate
95 // A parameter is given as passed on the command line, as in |Generate()|
98 // Returns true if successful. Otherwise, sets *error to a description of
99 // the problem (e.g. "invalid parameter") and returns false.
100 virtual bool GenerateAll(const std::vector<const FileDescriptor*>& files,
101 const std::string& parameter,
102 GeneratorContext* generator_context,
103 std::string* error) const;
105 // This is no longer used, but this class is part of the opensource protobuf
106 // library, so it has to remain to keep vtables the same for the current
107 // version of the library. When protobufs does a api breaking change, the
108 // method can be removed.
109 virtual bool HasGenerateAll() const { return true; }
112 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodeGenerator);
115 // CodeGenerators generate one or more files in a given directory. This
116 // abstract interface represents the directory to which the CodeGenerator is
117 // to write and other information about the context in which the Generator
119 class PROTOC_EXPORT GeneratorContext {
121 inline GeneratorContext() {
123 virtual ~GeneratorContext();
125 // Opens the given file, truncating it if it exists, and returns a
126 // ZeroCopyOutputStream that writes to the file. The caller takes ownership
127 // of the returned object. This method never fails (a dummy stream will be
128 // returned instead).
130 // The filename given should be relative to the root of the source tree.
131 // E.g. the C++ generator, when generating code for "foo/bar.proto", will
132 // generate the files "foo/bar.pb.h" and "foo/bar.pb.cc"; note that
133 // "foo/" is included in these filenames. The filename is not allowed to
134 // contain "." or ".." components.
135 virtual io::ZeroCopyOutputStream* Open(const std::string& filename) = 0;
137 // Similar to Open() but the output will be appended to the file if exists
138 virtual io::ZeroCopyOutputStream* OpenForAppend(const std::string& filename);
140 // Creates a ZeroCopyOutputStream which will insert code into the given file
141 // at the given insertion point. See plugin.proto (plugin.pb.h) for more
142 // information on insertion points. The default implementation
143 // assert-fails -- it exists only for backwards-compatibility.
145 // WARNING: This feature is currently EXPERIMENTAL and is subject to change.
146 virtual io::ZeroCopyOutputStream* OpenForInsert(
147 const std::string& filename, const std::string& insertion_point);
149 // Returns a vector of FileDescriptors for all the files being compiled
150 // in this run. Useful for languages, such as Go, that treat files
151 // differently when compiled as a set rather than individually.
152 virtual void ListParsedFiles(std::vector<const FileDescriptor*>* output);
154 // Retrieves the version number of the protocol compiler associated with
155 // this GeneratorContext.
156 virtual void GetCompilerVersion(Version* version) const;
160 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GeneratorContext);
163 // The type GeneratorContext was once called OutputDirectory. This typedef
164 // provides backward compatibility.
165 typedef GeneratorContext OutputDirectory;
167 // Several code generators treat the parameter argument as holding a
168 // list of options separated by commas. This helper function parses
169 // a set of comma-delimited name/value pairs: e.g.,
170 // "foo=bar,baz,qux=corge"
171 // parses to the pairs:
172 // ("foo", "bar"), ("baz", ""), ("qux", "corge")
173 PROTOC_EXPORT void ParseGeneratorParameter(
174 const std::string&, std::vector<std::pair<std::string, std::string> >*);
176 } // namespace compiler
177 } // namespace protobuf
178 } // namespace google
180 #include <google/protobuf/port_undef.inc>
182 #endif // GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__