| 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 26 matching lines...) Expand all Loading... |
| 37 #include <google/protobuf/io/printer.h> | 37 #include <google/protobuf/io/printer.h> |
| 38 #include <google/protobuf/wire_format.h> | 38 #include <google/protobuf/wire_format.h> |
| 39 #include <google/protobuf/stubs/strutil.h> | 39 #include <google/protobuf/stubs/strutil.h> |
| 40 | 40 |
| 41 namespace google { | 41 namespace google { |
| 42 namespace protobuf { | 42 namespace protobuf { |
| 43 namespace compiler { | 43 namespace compiler { |
| 44 namespace objectivec { | 44 namespace objectivec { |
| 45 | 45 |
| 46 namespace { | 46 namespace { |
| 47 |
| 47 void SetEnumVariables(const FieldDescriptor* descriptor, | 48 void SetEnumVariables(const FieldDescriptor* descriptor, |
| 48 map<string, string>* variables) { | 49 map<string, string>* variables) { |
| 49 string type = EnumName(descriptor->enum_type()); | 50 string type = EnumName(descriptor->enum_type()); |
| 50 (*variables)["storage_type"] = type; | 51 (*variables)["storage_type"] = type; |
| 51 // For non repeated fields, if it was defined in a different file, the | 52 // For non repeated fields, if it was defined in a different file, the |
| 52 // property decls need to use "enum NAME" rather than just "NAME" to support | 53 // property decls need to use "enum NAME" rather than just "NAME" to support |
| 53 // the forward declaration of the enums. | 54 // the forward declaration of the enums. |
| 54 if (!descriptor->is_repeated() && | 55 if (!descriptor->is_repeated() && |
| 55 (descriptor->file() != descriptor->enum_type()->file())) { | 56 (descriptor->file() != descriptor->enum_type()->file())) { |
| 56 (*variables)["property_type"] = "enum " + type; | 57 (*variables)["property_type"] = "enum " + type; |
| 57 } | 58 } |
| 58 (*variables)["enum_verifier"] = type + "_IsValidValue"; | 59 (*variables)["enum_verifier"] = type + "_IsValidValue"; |
| 59 (*variables)["enum_desc_func"] = type + "_EnumDescriptor"; | 60 (*variables)["enum_desc_func"] = type + "_EnumDescriptor"; |
| 60 | 61 |
| 62 (*variables)["dataTypeSpecific_name"] = "enumDescFunc"; |
| 63 (*variables)["dataTypeSpecific_value"] = (*variables)["enum_desc_func"]; |
| 64 |
| 61 const Descriptor* msg_descriptor = descriptor->containing_type(); | 65 const Descriptor* msg_descriptor = descriptor->containing_type(); |
| 62 (*variables)["owning_message_class"] = ClassName(msg_descriptor); | 66 (*variables)["owning_message_class"] = ClassName(msg_descriptor); |
| 63 } | 67 } |
| 64 } // namespace | 68 } // namespace |
| 65 | 69 |
| 66 EnumFieldGenerator::EnumFieldGenerator(const FieldDescriptor* descriptor) | 70 EnumFieldGenerator::EnumFieldGenerator(const FieldDescriptor* descriptor, |
| 67 : SingleFieldGenerator(descriptor) { | 71 const Options& options) |
| 72 : SingleFieldGenerator(descriptor, options) { |
| 68 SetEnumVariables(descriptor, &variables_); | 73 SetEnumVariables(descriptor, &variables_); |
| 69 } | 74 } |
| 70 | 75 |
| 71 EnumFieldGenerator::~EnumFieldGenerator() {} | 76 EnumFieldGenerator::~EnumFieldGenerator() {} |
| 72 | 77 |
| 73 void EnumFieldGenerator::GenerateFieldDescriptionTypeSpecific( | |
| 74 io::Printer* printer) const { | |
| 75 printer->Print( | |
| 76 variables_, | |
| 77 " .dataTypeSpecific.enumDescFunc = $enum_desc_func$,\n"); | |
| 78 } | |
| 79 | |
| 80 void EnumFieldGenerator::GenerateCFunctionDeclarations( | 78 void EnumFieldGenerator::GenerateCFunctionDeclarations( |
| 81 io::Printer* printer) const { | 79 io::Printer* printer) const { |
| 82 if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { | 80 if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { |
| 83 return; | 81 return; |
| 84 } | 82 } |
| 85 | 83 |
| 86 printer->Print( | 84 printer->Print( |
| 87 variables_, | 85 variables_, |
| 86 "/// Fetches the raw value of a @c $owning_message_class$'s @c $name$ prop
erty, even\n" |
| 87 "/// if the value was not defined by the enum at the time the code was gen
erated.\n" |
| 88 "int32_t $owning_message_class$_$capitalized_name$_RawValue($owning_messag
e_class$ *message);\n" | 88 "int32_t $owning_message_class$_$capitalized_name$_RawValue($owning_messag
e_class$ *message);\n" |
| 89 "/// Sets the raw value of an @c $owning_message_class$'s @c $name$ proper
ty, allowing\n" |
| 90 "/// it to be set to a value that was not defined by the enum at the time
the code\n" |
| 91 "/// was generated.\n" |
| 89 "void Set$owning_message_class$_$capitalized_name$_RawValue($owning_messag
e_class$ *message, int32_t value);\n" | 92 "void Set$owning_message_class$_$capitalized_name$_RawValue($owning_messag
e_class$ *message, int32_t value);\n" |
| 90 "\n"); | 93 "\n"); |
| 91 } | 94 } |
| 92 | 95 |
| 93 void EnumFieldGenerator::GenerateCFunctionImplementations( | 96 void EnumFieldGenerator::GenerateCFunctionImplementations( |
| 94 io::Printer* printer) const { | 97 io::Printer* printer) const { |
| 95 if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) return; | 98 if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) return; |
| 96 | 99 |
| 97 printer->Print( | 100 printer->Print( |
| 98 variables_, | 101 variables_, |
| 99 "int32_t $owning_message_class$_$capitalized_name$_RawValue($owning_messag
e_class$ *message) {\n" | 102 "int32_t $owning_message_class$_$capitalized_name$_RawValue($owning_messag
e_class$ *message) {\n" |
| 100 " GPBDescriptor *descriptor = [$owning_message_class$ descriptor];\n" | 103 " GPBDescriptor *descriptor = [$owning_message_class$ descriptor];\n" |
| 101 " GPBFieldDescriptor *field = [descriptor fieldWithNumber:$field_number_n
ame$];\n" | 104 " GPBFieldDescriptor *field = [descriptor fieldWithNumber:$field_number_n
ame$];\n" |
| 102 " return GPBGetMessageInt32Field(message, field);\n" | 105 " return GPBGetMessageInt32Field(message, field);\n" |
| 103 "}\n" | 106 "}\n" |
| 104 "\n" | 107 "\n" |
| 105 "void Set$owning_message_class$_$capitalized_name$_RawValue($owning_messag
e_class$ *message, int32_t value) {\n" | 108 "void Set$owning_message_class$_$capitalized_name$_RawValue($owning_messag
e_class$ *message, int32_t value) {\n" |
| 106 " GPBDescriptor *descriptor = [$owning_message_class$ descriptor];\n" | 109 " GPBDescriptor *descriptor = [$owning_message_class$ descriptor];\n" |
| 107 " GPBFieldDescriptor *field = [descriptor fieldWithNumber:$field_number_n
ame$];\n" | 110 " GPBFieldDescriptor *field = [descriptor fieldWithNumber:$field_number_n
ame$];\n" |
| 108 " GPBSetInt32IvarWithFieldInternal(message, field, value, descriptor.file
.syntax);\n" | 111 " GPBSetInt32IvarWithFieldInternal(message, field, value, descriptor.file
.syntax);\n" |
| 109 "}\n" | 112 "}\n" |
| 110 "\n"); | 113 "\n"); |
| 111 } | 114 } |
| 112 | 115 |
| 113 void EnumFieldGenerator::DetermineForwardDeclarations( | 116 void EnumFieldGenerator::DetermineForwardDeclarations( |
| 114 set<string>* fwd_decls) const { | 117 set<string>* fwd_decls) const { |
| 118 SingleFieldGenerator::DetermineForwardDeclarations(fwd_decls); |
| 115 // If it is an enum defined in a different file, then we'll need a forward | 119 // If it is an enum defined in a different file, then we'll need a forward |
| 116 // declaration for it. When it is in our file, all the enums are output | 120 // declaration for it. When it is in our file, all the enums are output |
| 117 // before the message, so it will be declared before it is needed. | 121 // before the message, so it will be declared before it is needed. |
| 118 if (descriptor_->file() != descriptor_->enum_type()->file()) { | 122 if (descriptor_->file() != descriptor_->enum_type()->file()) { |
| 119 // Enum name is already in "storage_type". | 123 // Enum name is already in "storage_type". |
| 120 const string& name = variable("storage_type"); | 124 const string& name = variable("storage_type"); |
| 121 fwd_decls->insert("GPB_ENUM_FWD_DECLARE(" + name + ")"); | 125 fwd_decls->insert("GPB_ENUM_FWD_DECLARE(" + name + ")"); |
| 122 } | 126 } |
| 123 } | 127 } |
| 124 | 128 |
| 125 RepeatedEnumFieldGenerator::RepeatedEnumFieldGenerator( | 129 RepeatedEnumFieldGenerator::RepeatedEnumFieldGenerator( |
| 126 const FieldDescriptor* descriptor) | 130 const FieldDescriptor* descriptor, const Options& options) |
| 127 : RepeatedFieldGenerator(descriptor) { | 131 : RepeatedFieldGenerator(descriptor, options) { |
| 128 SetEnumVariables(descriptor, &variables_); | 132 SetEnumVariables(descriptor, &variables_); |
| 129 variables_["array_storage_type"] = "GPBEnumArray"; | 133 variables_["array_storage_type"] = "GPBEnumArray"; |
| 130 } | 134 } |
| 131 | 135 |
| 132 RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() {} | 136 RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() {} |
| 133 | 137 |
| 134 void RepeatedEnumFieldGenerator::GenerateFieldDescriptionTypeSpecific( | 138 void RepeatedEnumFieldGenerator::FinishInitialization(void) { |
| 135 io::Printer* printer) const { | 139 RepeatedFieldGenerator::FinishInitialization(); |
| 136 printer->Print( | 140 variables_["array_comment"] = |
| 137 variables_, | 141 "// |" + variables_["name"] + "| contains |" + variables_["storage_type"]
+ "|\n"; |
| 138 " .dataTypeSpecific.enumDescFunc = $enum_desc_func$,\n"); | |
| 139 } | 142 } |
| 140 | 143 |
| 141 } // namespace objectivec | 144 } // namespace objectivec |
| 142 } // namespace compiler | 145 } // namespace compiler |
| 143 } // namespace protobuf | 146 } // namespace protobuf |
| 144 } // namespace google | 147 } // namespace google |
| OLD | NEW |