OLD | NEW |
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 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
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. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 #include <vector> | 43 #include <vector> |
44 #include <utility> | 44 #include <utility> |
45 | 45 |
46 namespace google { | 46 namespace google { |
47 namespace protobuf { | 47 namespace protobuf { |
48 | 48 |
49 namespace io { class ZeroCopyOutputStream; } | 49 namespace io { class ZeroCopyOutputStream; } |
50 class FileDescriptor; | 50 class FileDescriptor; |
51 | 51 |
52 namespace compiler { | 52 namespace compiler { |
| 53 class Version; |
53 | 54 |
54 // Defined in this file. | 55 // Defined in this file. |
55 class CodeGenerator; | 56 class CodeGenerator; |
56 class GeneratorContext; | 57 class GeneratorContext; |
57 | 58 |
58 // The abstract interface to a class which generates code implementing a | 59 // The abstract interface to a class which generates code implementing a |
59 // particular proto file in a particular language. A number of these may | 60 // particular proto file in a particular language. A number of these may |
60 // be registered with CommandLineInterface to support various languages. | 61 // be registered with CommandLineInterface to support various languages. |
61 class LIBPROTOC_EXPORT CodeGenerator { | 62 class LIBPROTOC_EXPORT CodeGenerator { |
62 public: | 63 public: |
63 inline CodeGenerator() {} | 64 inline CodeGenerator() {} |
64 virtual ~CodeGenerator(); | 65 virtual ~CodeGenerator(); |
65 | 66 |
66 // Generates code for the given proto file, generating one or more files in | 67 // Generates code for the given proto file, generating one or more files in |
67 // the given output directory. | 68 // the given output directory. |
68 // | 69 // |
69 // A parameter to be passed to the generator can be specified on the | 70 // A parameter to be passed to the generator can be specified on the command |
70 // command line. This is intended to be used by Java and similar languages | 71 // line. This is intended to be used to pass generator specific parameters. |
71 // to specify which specific class from the proto file is to be generated, | 72 // It is empty if no parameter was given. ParseGeneratorParameter (below), |
72 // though it could have other uses as well. It is empty if no parameter was | 73 // can be used to accept multiple parameters within the single parameter |
73 // given. | 74 // command line flag. |
74 // | 75 // |
75 // Returns true if successful. Otherwise, sets *error to a description of | 76 // Returns true if successful. Otherwise, sets *error to a description of |
76 // the problem (e.g. "invalid parameter") and returns false. | 77 // the problem (e.g. "invalid parameter") and returns false. |
77 virtual bool Generate(const FileDescriptor* file, | 78 virtual bool Generate(const FileDescriptor* file, |
78 const string& parameter, | 79 const string& parameter, |
79 GeneratorContext* generator_context, | 80 GeneratorContext* generator_context, |
80 string* error) const = 0; | 81 string* error) const = 0; |
81 | 82 |
82 // Generates code for all given proto files, generating one or more files in | 83 // Generates code for all given proto files. |
83 // the given output directory. | |
84 // | 84 // |
85 // This method should be called instead of |Generate()| when | 85 // WARNING: The canonical code generator design produces one or two output |
86 // |HasGenerateAll()| returns |true|. It is used to emulate legacy semantics | 86 // files per input .proto file, and we do not wish to encourage alternate |
87 // when more than one `.proto` file is specified on one compiler invocation. | 87 // designs. |
88 // | |
89 // WARNING: Please do not use unless legacy semantics force the code generator | |
90 // to produce a single output file for all input files, or otherwise require | |
91 // an examination of all input files first. The canonical code generator | |
92 // design produces one output file per input .proto file, and we do not wish | |
93 // to encourage alternate designs. | |
94 // | 88 // |
95 // A parameter is given as passed on the command line, as in |Generate()| | 89 // A parameter is given as passed on the command line, as in |Generate()| |
96 // above. | 90 // above. |
97 // | 91 // |
98 // Returns true if successful. Otherwise, sets *error to a description of | 92 // Returns true if successful. Otherwise, sets *error to a description of |
99 // the problem (e.g. "invalid parameter") and returns false. | 93 // the problem (e.g. "invalid parameter") and returns false. |
100 virtual bool GenerateAll(const vector<const FileDescriptor*>& files, | 94 virtual bool GenerateAll(const std::vector<const FileDescriptor*>& files, |
101 const string& parameter, | 95 const string& parameter, |
102 GeneratorContext* generator_context, | 96 GeneratorContext* generator_context, |
103 string* error) const { | 97 string* error) const; |
104 *error = "Unimplemented GenerateAll() method."; | |
105 return false; | |
106 } | |
107 | 98 |
108 // Returns true if the code generator expects to receive all FileDescriptors | 99 // This is no longer used, but this class is part of the opensource protobuf |
109 // at once (via |GenerateAll()|), rather than one at a time (via | 100 // library, so it has to remain to keep vtables the same for the current |
110 // |Generate()|). This is required to implement legacy semantics. | 101 // version of the library. When protobufs does a api breaking change, the |
111 virtual bool HasGenerateAll() const { return false; } | 102 // method can be removed. |
| 103 virtual bool HasGenerateAll() const { return true; } |
112 | 104 |
113 private: | 105 private: |
114 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodeGenerator); | 106 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodeGenerator); |
115 }; | 107 }; |
116 | 108 |
117 // CodeGenerators generate one or more files in a given directory. This | 109 // CodeGenerators generate one or more files in a given directory. This |
118 // abstract interface represents the directory to which the CodeGenerator is | 110 // abstract interface represents the directory to which the CodeGenerator is |
119 // to write and other information about the context in which the Generator | 111 // to write and other information about the context in which the Generator |
120 // runs. | 112 // runs. |
121 class LIBPROTOC_EXPORT GeneratorContext { | 113 class LIBPROTOC_EXPORT GeneratorContext { |
(...skipping 21 matching lines...) Expand all Loading... |
143 // information on insertion points. The default implementation | 135 // information on insertion points. The default implementation |
144 // assert-fails -- it exists only for backwards-compatibility. | 136 // assert-fails -- it exists only for backwards-compatibility. |
145 // | 137 // |
146 // WARNING: This feature is currently EXPERIMENTAL and is subject to change. | 138 // WARNING: This feature is currently EXPERIMENTAL and is subject to change. |
147 virtual io::ZeroCopyOutputStream* OpenForInsert( | 139 virtual io::ZeroCopyOutputStream* OpenForInsert( |
148 const string& filename, const string& insertion_point); | 140 const string& filename, const string& insertion_point); |
149 | 141 |
150 // Returns a vector of FileDescriptors for all the files being compiled | 142 // Returns a vector of FileDescriptors for all the files being compiled |
151 // in this run. Useful for languages, such as Go, that treat files | 143 // in this run. Useful for languages, such as Go, that treat files |
152 // differently when compiled as a set rather than individually. | 144 // differently when compiled as a set rather than individually. |
153 virtual void ListParsedFiles(vector<const FileDescriptor*>* output); | 145 virtual void ListParsedFiles(std::vector<const FileDescriptor*>* output); |
| 146 |
| 147 // Retrieves the version number of the protocol compiler associated with |
| 148 // this GeneratorContext. |
| 149 virtual void GetCompilerVersion(Version* version) const; |
154 | 150 |
155 private: | 151 private: |
156 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GeneratorContext); | 152 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GeneratorContext); |
157 }; | 153 }; |
158 | 154 |
159 // The type GeneratorContext was once called OutputDirectory. This typedef | 155 // The type GeneratorContext was once called OutputDirectory. This typedef |
160 // provides backward compatibility. | 156 // provides backward compatibility. |
161 typedef GeneratorContext OutputDirectory; | 157 typedef GeneratorContext OutputDirectory; |
162 | 158 |
163 // Several code generators treat the parameter argument as holding a | 159 // Several code generators treat the parameter argument as holding a |
164 // list of options separated by commas. This helper function parses | 160 // list of options separated by commas. This helper function parses |
165 // a set of comma-delimited name/value pairs: e.g., | 161 // a set of comma-delimited name/value pairs: e.g., |
166 // "foo=bar,baz,qux=corge" | 162 // "foo=bar,baz,qux=corge" |
167 // parses to the pairs: | 163 // parses to the pairs: |
168 // ("foo", "bar"), ("baz", ""), ("qux", "corge") | 164 // ("foo", "bar"), ("baz", ""), ("qux", "corge") |
169 extern void ParseGeneratorParameter(const string&, | 165 extern void ParseGeneratorParameter(const string&, |
170 vector<pair<string, string> >*); | 166 std::vector<std::pair<string, string> >*); |
171 | 167 |
172 } // namespace compiler | 168 } // namespace compiler |
173 } // namespace protobuf | 169 } // namespace protobuf |
174 | 170 |
175 } // namespace google | 171 } // namespace google |
176 #endif // GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__ | 172 #endif // GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__ |
OLD | NEW |