| 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 map<string, const EnumDescriptor*>* enum_names) { | 73 std::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 map<string, string> vars; | 81 std::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 map<string, string> vars; | 183 std::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 "PROTOBUF_DEPRECATED_ATTR " : ""; | 192 "GOOGLE_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( | 232 void EnumGenerator::GenerateDescriptorInitializer(io::Printer* printer) { |
| 233 io::Printer* printer, int index) { | 233 std::map<string, string> vars; |
| 234 map<string, string> vars; | 234 vars["index"] = SimpleItoa(descriptor_->index()); |
| 235 vars["classname"] = classname_; | 235 vars["index_in_metadata"] = SimpleItoa(index_in_metadata_); |
| 236 vars["index"] = SimpleItoa(index); | |
| 237 | 236 |
| 238 if (descriptor_->containing_type() == NULL) { | 237 if (descriptor_->containing_type() == NULL) { |
| 239 printer->Print(vars, | 238 printer->Print(vars, |
| 240 "$classname$_descriptor_ = file->enum_type($index$);\n"); | 239 "file_level_enum_descriptors[$index_in_metadata$] = " |
| 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 "$classname$_descriptor_ = $parent$_descriptor_->enum_type($index$);\n"); | 244 "file_level_enum_descriptors[$index_in_metadata$] = " |
| 245 "$parent$_descriptor->enum_type($index$);\n"); |
| 245 } | 246 } |
| 246 } | 247 } |
| 247 | 248 |
| 248 void EnumGenerator::GenerateMethods(io::Printer* printer) { | 249 void EnumGenerator::GenerateMethods(io::Printer* printer) { |
| 249 map<string, string> vars; | 250 std::map<string, string> vars; |
| 250 vars["classname"] = classname_; | 251 vars["classname"] = classname_; |
| 252 vars["index_in_metadata"] = SimpleItoa(index_in_metadata_); |
| 251 vars["constexpr"] = options_.proto_h ? "constexpr " : ""; | 253 vars["constexpr"] = options_.proto_h ? "constexpr " : ""; |
| 252 | 254 |
| 253 if (HasDescriptorMethods(descriptor_->file(), options_)) { | 255 if (HasDescriptorMethods(descriptor_->file(), options_)) { |
| 254 printer->Print(vars, | 256 printer->Print( |
| 255 "const ::google::protobuf::EnumDescriptor* $classname$_descriptor() {\n" | 257 vars, |
| 256 " protobuf_AssignDescriptorsOnce();\n" | 258 "const ::google::protobuf::EnumDescriptor* $classname$_descriptor() {\n" |
| 257 " return $classname$_descriptor_;\n" | 259 " protobuf_AssignDescriptorsOnce();\n" |
| 258 "}\n"); | 260 " return file_level_enum_descriptors[$index_in_metadata$];\n" |
| 261 "}\n"); |
| 259 } | 262 } |
| 260 | 263 |
| 261 printer->Print(vars, | 264 printer->Print(vars, |
| 262 "bool $classname$_IsValid(int value) {\n" | 265 "bool $classname$_IsValid(int value) {\n" |
| 263 " switch(value) {\n"); | 266 " switch (value) {\n"); |
| 264 | 267 |
| 265 // Multiple values may have the same number. Make sure we only cover | 268 // Multiple values may have the same number. Make sure we only cover |
| 266 // each number once by first constructing a set containing all valid | 269 // each number once by first constructing a set containing all valid |
| 267 // numbers, then printing a case statement for each element. | 270 // numbers, then printing a case statement for each element. |
| 268 | 271 |
| 269 set<int> numbers; | 272 std::set<int> numbers; |
| 270 for (int j = 0; j < descriptor_->value_count(); j++) { | 273 for (int j = 0; j < descriptor_->value_count(); j++) { |
| 271 const EnumValueDescriptor* value = descriptor_->value(j); | 274 const EnumValueDescriptor* value = descriptor_->value(j); |
| 272 numbers.insert(value->number()); | 275 numbers.insert(value->number()); |
| 273 } | 276 } |
| 274 | 277 |
| 275 for (set<int>::iterator iter = numbers.begin(); | 278 for (std::set<int>::iterator iter = numbers.begin(); |
| 276 iter != numbers.end(); ++iter) { | 279 iter != numbers.end(); ++iter) { |
| 277 printer->Print( | 280 printer->Print( |
| 278 " case $number$:\n", | 281 " case $number$:\n", |
| 279 "number", Int32ToString(*iter)); | 282 "number", Int32ToString(*iter)); |
| 280 } | 283 } |
| 281 | 284 |
| 282 printer->Print(vars, | 285 printer->Print(vars, |
| 283 " return true;\n" | 286 " return true;\n" |
| 284 " default:\n" | 287 " default:\n" |
| 285 " return false;\n" | 288 " return false;\n" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 310 } | 313 } |
| 311 | 314 |
| 312 printer->Print("#endif // !defined(_MSC_VER) || _MSC_VER >= 1900\n"); | 315 printer->Print("#endif // !defined(_MSC_VER) || _MSC_VER >= 1900\n"); |
| 313 } | 316 } |
| 314 } | 317 } |
| 315 | 318 |
| 316 } // namespace cpp | 319 } // namespace cpp |
| 317 } // namespace compiler | 320 } // namespace compiler |
| 318 } // namespace protobuf | 321 } // namespace protobuf |
| 319 } // namespace google | 322 } // namespace google |
| OLD | NEW |