1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/
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.
38 #include <google/protobuf/compiler/cpp/cpp_enum.h>
39 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
40 #include <google/protobuf/io/printer.h>
41 #include <google/protobuf/stubs/strutil.h>
48 EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor,
49 const Options& options)
50 : descriptor_(descriptor),
51 classname_(ClassName(descriptor, false)),
55 EnumGenerator::~EnumGenerator() {}
57 void EnumGenerator::GenerateDefinition(io::Printer* printer) {
58 map<string, string> vars;
59 vars["classname"] = classname_;
60 vars["short_name"] = descriptor_->name();
62 printer->Print(vars, "enum $classname$ {\n");
65 const EnumValueDescriptor* min_value = descriptor_->value(0);
66 const EnumValueDescriptor* max_value = descriptor_->value(0);
68 for (int i = 0; i < descriptor_->value_count(); i++) {
69 vars["name"] = descriptor_->value(i)->name();
70 vars["number"] = SimpleItoa(descriptor_->value(i)->number());
71 vars["prefix"] = (descriptor_->containing_type() == NULL) ?
72 "" : classname_ + "_";
74 if (i > 0) printer->Print(",\n");
75 printer->Print(vars, "$prefix$$name$ = $number$");
77 if (descriptor_->value(i)->number() < min_value->number()) {
78 min_value = descriptor_->value(i);
80 if (descriptor_->value(i)->number() > max_value->number()) {
81 max_value = descriptor_->value(i);
86 printer->Print("\n};\n");
88 vars["min_name"] = min_value->name();
89 vars["max_name"] = max_value->name();
91 if (options_.dllexport_decl.empty()) {
92 vars["dllexport"] = "";
94 vars["dllexport"] = options_.dllexport_decl + " ";
98 "$dllexport$bool $classname$_IsValid(int value);\n"
99 "const $classname$ $prefix$$short_name$_MIN = $prefix$$min_name$;\n"
100 "const $classname$ $prefix$$short_name$_MAX = $prefix$$max_name$;\n"
101 "const int $prefix$$short_name$_ARRAYSIZE = $prefix$$short_name$_MAX + 1;\n"
104 if (HasDescriptorMethods(descriptor_->file())) {
106 "$dllexport$const ::google::protobuf::EnumDescriptor* $classname$_descriptor();\n");
107 // The _Name and _Parse methods
109 "inline const ::std::string& $classname$_Name($classname$ value) {\n"
110 " return ::google::protobuf::internal::NameOfEnum(\n"
111 " $classname$_descriptor(), value);\n"
114 "inline bool $classname$_Parse(\n"
115 " const ::std::string& name, $classname$* value) {\n"
116 " return ::google::protobuf::internal::ParseNamedEnum<$classname$>(\n"
117 " $classname$_descriptor(), name, value);\n"
123 GenerateGetEnumDescriptorSpecializations(io::Printer* printer) {
124 if (HasDescriptorMethods(descriptor_->file())) {
127 "inline const EnumDescriptor* GetEnumDescriptor< $classname$>() {\n"
128 " return $classname$_descriptor();\n"
130 "classname", ClassName(descriptor_, true));
134 void EnumGenerator::GenerateSymbolImports(io::Printer* printer) {
135 map<string, string> vars;
136 vars["nested_name"] = descriptor_->name();
137 vars["classname"] = classname_;
138 printer->Print(vars, "typedef $classname$ $nested_name$;\n");
140 for (int j = 0; j < descriptor_->value_count(); j++) {
141 vars["tag"] = descriptor_->value(j)->name();
143 "static const $nested_name$ $tag$ = $classname$_$tag$;\n");
147 "static inline bool $nested_name$_IsValid(int value) {\n"
148 " return $classname$_IsValid(value);\n"
150 "static const $nested_name$ $nested_name$_MIN =\n"
151 " $classname$_$nested_name$_MIN;\n"
152 "static const $nested_name$ $nested_name$_MAX =\n"
153 " $classname$_$nested_name$_MAX;\n"
154 "static const int $nested_name$_ARRAYSIZE =\n"
155 " $classname$_$nested_name$_ARRAYSIZE;\n");
157 if (HasDescriptorMethods(descriptor_->file())) {
159 "static inline const ::google::protobuf::EnumDescriptor*\n"
160 "$nested_name$_descriptor() {\n"
161 " return $classname$_descriptor();\n"
164 "static inline const ::std::string& $nested_name$_Name($nested_name$ value) {\n"
165 " return $classname$_Name(value);\n"
168 "static inline bool $nested_name$_Parse(const ::std::string& name,\n"
169 " $nested_name$* value) {\n"
170 " return $classname$_Parse(name, value);\n"
175 void EnumGenerator::GenerateDescriptorInitializer(
176 io::Printer* printer, int index) {
177 map<string, string> vars;
178 vars["classname"] = classname_;
179 vars["index"] = SimpleItoa(index);
181 if (descriptor_->containing_type() == NULL) {
183 "$classname$_descriptor_ = file->enum_type($index$);\n");
185 vars["parent"] = ClassName(descriptor_->containing_type(), false);
187 "$classname$_descriptor_ = $parent$_descriptor_->enum_type($index$);\n");
191 void EnumGenerator::GenerateMethods(io::Printer* printer) {
192 map<string, string> vars;
193 vars["classname"] = classname_;
195 if (HasDescriptorMethods(descriptor_->file())) {
197 "const ::google::protobuf::EnumDescriptor* $classname$_descriptor() {\n"
198 " protobuf_AssignDescriptorsOnce();\n"
199 " return $classname$_descriptor_;\n"
204 "bool $classname$_IsValid(int value) {\n"
205 " switch(value) {\n");
207 // Multiple values may have the same number. Make sure we only cover
208 // each number once by first constructing a set containing all valid
209 // numbers, then printing a case statement for each element.
212 for (int j = 0; j < descriptor_->value_count(); j++) {
213 const EnumValueDescriptor* value = descriptor_->value(j);
214 numbers.insert(value->number());
217 for (set<int>::iterator iter = numbers.begin();
218 iter != numbers.end(); ++iter) {
221 "number", SimpleItoa(*iter));
232 if (descriptor_->containing_type() != NULL) {
233 // We need to "define" the static constants which were declared in the
234 // header, to give the linker a place to put them. Or at least the C++
235 // standard says we have to. MSVC actually insists tha we do _not_ define
236 // them again in the .cc file.
237 printer->Print("#ifndef _MSC_VER\n");
239 vars["parent"] = ClassName(descriptor_->containing_type(), false);
240 vars["nested_name"] = descriptor_->name();
241 for (int i = 0; i < descriptor_->value_count(); i++) {
242 vars["value"] = descriptor_->value(i)->name();
244 "const $classname$ $parent$::$value$;\n");
247 "const $classname$ $parent$::$nested_name$_MIN;\n"
248 "const $classname$ $parent$::$nested_name$_MAX;\n"
249 "const int $parent$::$nested_name$_ARRAYSIZE;\n");
251 printer->Print("#endif // _MSC_VER\n");
256 } // namespace compiler
257 } // namespace protobuf
258 } // namespace google