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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 const Options& options) | 63 const Options& options) |
64 : descriptor_(descriptor), | 64 : descriptor_(descriptor), |
65 classname_(ClassName(descriptor, false)), | 65 classname_(ClassName(descriptor, false)), |
66 options_(options), | 66 options_(options), |
67 generate_array_size_(ShouldGenerateArraySize(descriptor)) { | 67 generate_array_size_(ShouldGenerateArraySize(descriptor)) { |
68 } | 68 } |
69 | 69 |
70 EnumGenerator::~EnumGenerator() {} | 70 EnumGenerator::~EnumGenerator() {} |
71 | 71 |
72 void EnumGenerator::FillForwardDeclaration( | 72 void EnumGenerator::FillForwardDeclaration( |
73 std::map<string, const EnumDescriptor*>* enum_names) { | 73 map<string, const EnumDescriptor*>* enum_names) { |
74 if (!options_.proto_h) { | 74 if (!options_.proto_h) { |
75 return; | 75 return; |
76 } | 76 } |
77 (*enum_names)[classname_] = descriptor_; | 77 (*enum_names)[classname_] = descriptor_; |
78 } | 78 } |
79 | 79 |
80 void EnumGenerator::GenerateDefinition(io::Printer* printer) { | 80 void EnumGenerator::GenerateDefinition(io::Printer* printer) { |
81 std::map<string, string> vars; | 81 map<string, string> vars; |
82 vars["classname"] = classname_; | 82 vars["classname"] = classname_; |
83 vars["short_name"] = descriptor_->name(); | 83 vars["short_name"] = descriptor_->name(); |
84 vars["enumbase"] = classname_ + (options_.proto_h ? " : int" : ""); | 84 vars["enumbase"] = classname_ + (options_.proto_h ? " : int" : ""); |
85 | 85 |
86 printer->Print(vars, "enum $enumbase$ {\n"); | 86 printer->Print(vars, "enum $enumbase$ {\n"); |
87 printer->Annotate("enumbase", descriptor_); | 87 printer->Annotate("enumbase", descriptor_); |
88 printer->Indent(); | 88 printer->Indent(); |
89 | 89 |
90 const EnumValueDescriptor* min_value = descriptor_->value(0); | 90 const EnumValueDescriptor* min_value = descriptor_->value(0); |
91 const EnumValueDescriptor* max_value = descriptor_->value(0); | 91 const EnumValueDescriptor* max_value = descriptor_->value(0); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 printer->Print( | 173 printer->Print( |
174 "template <>\n" | 174 "template <>\n" |
175 "inline const EnumDescriptor* GetEnumDescriptor< $classname$>() {\n" | 175 "inline const EnumDescriptor* GetEnumDescriptor< $classname$>() {\n" |
176 " return $classname$_descriptor();\n" | 176 " return $classname$_descriptor();\n" |
177 "}\n", | 177 "}\n", |
178 "classname", ClassName(descriptor_, true)); | 178 "classname", ClassName(descriptor_, true)); |
179 } | 179 } |
180 } | 180 } |
181 | 181 |
182 void EnumGenerator::GenerateSymbolImports(io::Printer* printer) { | 182 void EnumGenerator::GenerateSymbolImports(io::Printer* printer) { |
183 std::map<string, string> vars; | 183 map<string, string> vars; |
184 vars["nested_name"] = descriptor_->name(); | 184 vars["nested_name"] = descriptor_->name(); |
185 vars["classname"] = classname_; | 185 vars["classname"] = classname_; |
186 vars["constexpr"] = options_.proto_h ? "constexpr " : ""; | 186 vars["constexpr"] = options_.proto_h ? "constexpr " : ""; |
187 printer->Print(vars, "typedef $classname$ $nested_name$;\n"); | 187 printer->Print(vars, "typedef $classname$ $nested_name$;\n"); |
188 | 188 |
189 for (int j = 0; j < descriptor_->value_count(); j++) { | 189 for (int j = 0; j < descriptor_->value_count(); j++) { |
190 vars["tag"] = EnumValueName(descriptor_->value(j)); | 190 vars["tag"] = EnumValueName(descriptor_->value(j)); |
191 vars["deprecated_attr"] = descriptor_->value(j)->options().deprecated() ? | 191 vars["deprecated_attr"] = descriptor_->value(j)->options().deprecated() ? |
192 "GOOGLE_PROTOBUF_DEPRECATED_ATTR " : ""; | 192 "PROTOBUF_DEPRECATED_ATTR " : ""; |
193 printer->Print(vars, | 193 printer->Print(vars, |
194 "$deprecated_attr$static $constexpr$const $nested_name$ $tag$ =\n" | 194 "$deprecated_attr$static $constexpr$const $nested_name$ $tag$ =\n" |
195 " $classname$_$tag$;\n"); | 195 " $classname$_$tag$;\n"); |
196 } | 196 } |
197 | 197 |
198 printer->Print(vars, | 198 printer->Print(vars, |
199 "static inline bool $nested_name$_IsValid(int value) {\n" | 199 "static inline bool $nested_name$_IsValid(int value) {\n" |
200 " return $classname$_IsValid(value);\n" | 200 " return $classname$_IsValid(value);\n" |
201 "}\n" | 201 "}\n" |
202 "static const $nested_name$ $nested_name$_MIN =\n" | 202 "static const $nested_name$ $nested_name$_MIN =\n" |
(...skipping 19 matching lines...) Expand all Loading... |
222 " return $classname$_Name(value);\n" | 222 " return $classname$_Name(value);\n" |
223 "}\n"); | 223 "}\n"); |
224 printer->Print(vars, | 224 printer->Print(vars, |
225 "static inline bool $nested_name$_Parse(const ::std::string& name,\n" | 225 "static inline bool $nested_name$_Parse(const ::std::string& name,\n" |
226 " $nested_name$* value) {\n" | 226 " $nested_name$* value) {\n" |
227 " return $classname$_Parse(name, value);\n" | 227 " return $classname$_Parse(name, value);\n" |
228 "}\n"); | 228 "}\n"); |
229 } | 229 } |
230 } | 230 } |
231 | 231 |
232 void EnumGenerator::GenerateDescriptorInitializer(io::Printer* printer) { | 232 void EnumGenerator::GenerateDescriptorInitializer( |
233 std::map<string, string> vars; | 233 io::Printer* printer, int index) { |
234 vars["index"] = SimpleItoa(descriptor_->index()); | 234 map<string, string> vars; |
235 vars["index_in_metadata"] = SimpleItoa(index_in_metadata_); | 235 vars["classname"] = classname_; |
| 236 vars["index"] = SimpleItoa(index); |
236 | 237 |
237 if (descriptor_->containing_type() == NULL) { | 238 if (descriptor_->containing_type() == NULL) { |
238 printer->Print(vars, | 239 printer->Print(vars, |
239 "file_level_enum_descriptors[$index_in_metadata$] = " | 240 "$classname$_descriptor_ = file->enum_type($index$);\n"); |
240 "file->enum_type($index$);\n"); | |
241 } else { | 241 } else { |
242 vars["parent"] = ClassName(descriptor_->containing_type(), false); | 242 vars["parent"] = ClassName(descriptor_->containing_type(), false); |
243 printer->Print(vars, | 243 printer->Print(vars, |
244 "file_level_enum_descriptors[$index_in_metadata$] = " | 244 "$classname$_descriptor_ = $parent$_descriptor_->enum_type($index$);\n"); |
245 "$parent$_descriptor->enum_type($index$);\n"); | |
246 } | 245 } |
247 } | 246 } |
248 | 247 |
249 void EnumGenerator::GenerateMethods(io::Printer* printer) { | 248 void EnumGenerator::GenerateMethods(io::Printer* printer) { |
250 std::map<string, string> vars; | 249 map<string, string> vars; |
251 vars["classname"] = classname_; | 250 vars["classname"] = classname_; |
252 vars["index_in_metadata"] = SimpleItoa(index_in_metadata_); | |
253 vars["constexpr"] = options_.proto_h ? "constexpr " : ""; | 251 vars["constexpr"] = options_.proto_h ? "constexpr " : ""; |
254 | 252 |
255 if (HasDescriptorMethods(descriptor_->file(), options_)) { | 253 if (HasDescriptorMethods(descriptor_->file(), options_)) { |
256 printer->Print( | 254 printer->Print(vars, |
257 vars, | 255 "const ::google::protobuf::EnumDescriptor* $classname$_descriptor() {\n" |
258 "const ::google::protobuf::EnumDescriptor* $classname$_descriptor() {\n" | 256 " protobuf_AssignDescriptorsOnce();\n" |
259 " protobuf_AssignDescriptorsOnce();\n" | 257 " return $classname$_descriptor_;\n" |
260 " return file_level_enum_descriptors[$index_in_metadata$];\n" | 258 "}\n"); |
261 "}\n"); | |
262 } | 259 } |
263 | 260 |
264 printer->Print(vars, | 261 printer->Print(vars, |
265 "bool $classname$_IsValid(int value) {\n" | 262 "bool $classname$_IsValid(int value) {\n" |
266 " switch (value) {\n"); | 263 " switch(value) {\n"); |
267 | 264 |
268 // Multiple values may have the same number. Make sure we only cover | 265 // Multiple values may have the same number. Make sure we only cover |
269 // each number once by first constructing a set containing all valid | 266 // each number once by first constructing a set containing all valid |
270 // numbers, then printing a case statement for each element. | 267 // numbers, then printing a case statement for each element. |
271 | 268 |
272 std::set<int> numbers; | 269 set<int> numbers; |
273 for (int j = 0; j < descriptor_->value_count(); j++) { | 270 for (int j = 0; j < descriptor_->value_count(); j++) { |
274 const EnumValueDescriptor* value = descriptor_->value(j); | 271 const EnumValueDescriptor* value = descriptor_->value(j); |
275 numbers.insert(value->number()); | 272 numbers.insert(value->number()); |
276 } | 273 } |
277 | 274 |
278 for (std::set<int>::iterator iter = numbers.begin(); | 275 for (set<int>::iterator iter = numbers.begin(); |
279 iter != numbers.end(); ++iter) { | 276 iter != numbers.end(); ++iter) { |
280 printer->Print( | 277 printer->Print( |
281 " case $number$:\n", | 278 " case $number$:\n", |
282 "number", Int32ToString(*iter)); | 279 "number", Int32ToString(*iter)); |
283 } | 280 } |
284 | 281 |
285 printer->Print(vars, | 282 printer->Print(vars, |
286 " return true;\n" | 283 " return true;\n" |
287 " default:\n" | 284 " default:\n" |
288 " return false;\n" | 285 " return false;\n" |
(...skipping 24 matching lines...) Expand all Loading... |
313 } | 310 } |
314 | 311 |
315 printer->Print("#endif // !defined(_MSC_VER) || _MSC_VER >= 1900\n"); | 312 printer->Print("#endif // !defined(_MSC_VER) || _MSC_VER >= 1900\n"); |
316 } | 313 } |
317 } | 314 } |
318 | 315 |
319 } // namespace cpp | 316 } // namespace cpp |
320 } // namespace compiler | 317 } // namespace compiler |
321 } // namespace protobuf | 318 } // namespace protobuf |
322 } // namespace google | 319 } // namespace google |
OLD | NEW |