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