objectivec_enum_field.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
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
14 // distribution.
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.
18 //
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.
30 
31 #include <map>
32 #include <string>
33 
38 
39 namespace google {
40 namespace protobuf {
41 namespace compiler {
42 namespace objectivec {
43 
44 namespace {
45 
46 void SetEnumVariables(const FieldDescriptor* descriptor,
47  std::map<string, string>* variables) {
48  string type = EnumName(descriptor->enum_type());
49  (*variables)["storage_type"] = type;
50  // For non repeated fields, if it was defined in a different file, the
51  // property decls need to use "enum NAME" rather than just "NAME" to support
52  // the forward declaration of the enums.
53  if (!descriptor->is_repeated() &&
54  (descriptor->file() != descriptor->enum_type()->file())) {
55  (*variables)["property_type"] = "enum " + type;
56  }
57  (*variables)["enum_verifier"] = type + "_IsValidValue";
58  (*variables)["enum_desc_func"] = type + "_EnumDescriptor";
59 
60  (*variables)["dataTypeSpecific_name"] = "enumDescFunc";
61  (*variables)["dataTypeSpecific_value"] = (*variables)["enum_desc_func"];
62 
63  const Descriptor* msg_descriptor = descriptor->containing_type();
64  (*variables)["owning_message_class"] = ClassName(msg_descriptor);
65 }
66 } // namespace
67 
69  const Options& options)
71  SetEnumVariables(descriptor, &variables_);
72 }
73 
75 
77  io::Printer* printer) const {
79  return;
80  }
81 
82  printer->Print(
83  variables_,
84  "/**\n"
85  " * Fetches the raw value of a @c $owning_message_class$'s @c $name$ property, even\n"
86  " * if the value was not defined by the enum at the time the code was generated.\n"
87  " **/\n"
88  "int32_t $owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message);\n"
89  "/**\n"
90  " * Sets the raw value of an @c $owning_message_class$'s @c $name$ property, allowing\n"
91  " * it to be set to a value that was not defined by the enum at the time the code\n"
92  " * was generated.\n"
93  " **/\n"
94  "void Set$owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message, int32_t value);\n"
95  "\n");
96 }
97 
99  io::Printer* printer) const {
101 
102  printer->Print(
103  variables_,
104  "int32_t $owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message) {\n"
105  " GPBDescriptor *descriptor = [$owning_message_class$ descriptor];\n"
106  " GPBFieldDescriptor *field = [descriptor fieldWithNumber:$field_number_name$];\n"
107  " return GPBGetMessageInt32Field(message, field);\n"
108  "}\n"
109  "\n"
110  "void Set$owning_message_class$_$capitalized_name$_RawValue($owning_message_class$ *message, int32_t value) {\n"
111  " GPBDescriptor *descriptor = [$owning_message_class$ descriptor];\n"
112  " GPBFieldDescriptor *field = [descriptor fieldWithNumber:$field_number_name$];\n"
113  " GPBSetInt32IvarWithFieldInternal(message, field, value, descriptor.file.syntax);\n"
114  "}\n"
115  "\n");
116 }
117 
119  std::set<string>* fwd_decls) const {
121  // If it is an enum defined in a different file, then we'll need a forward
122  // declaration for it. When it is in our file, all the enums are output
123  // before the message, so it will be declared before it is needed.
124  if (descriptor_->file() != descriptor_->enum_type()->file()) {
125  // Enum name is already in "storage_type".
126  const string& name = variable("storage_type");
127  fwd_decls->insert("GPB_ENUM_FWD_DECLARE(" + name + ")");
128  }
129 }
130 
132  const FieldDescriptor* descriptor, const Options& options)
134  SetEnumVariables(descriptor, &variables_);
135  variables_["array_storage_type"] = "GPBEnumArray";
136 }
137 
139 
142  variables_["array_comment"] =
143  "// |" + variables_["name"] + "| contains |" + variables_["storage_type"] + "|\n";
144 }
145 
146 } // namespace objectivec
147 } // namespace compiler
148 } // namespace protobuf
149 } // namespace google
google::protobuf::io::Printer::Print
void Print(const std::map< std::string, std::string > &variables, const char *text)
Definition: printer.cc:112
google::protobuf::compiler::objectivec::RepeatedEnumFieldGenerator::RepeatedEnumFieldGenerator
RepeatedEnumFieldGenerator(const FieldDescriptor *descriptor, const Options &options)
Definition: objectivec_enum_field.cc:131
name
GLuint const GLchar * name
Definition: glcorearb.h:3055
google::protobuf::FieldDescriptor::enum_type
const EnumDescriptor * enum_type() const
Definition: src/google/protobuf/descriptor.cc:7235
google::protobuf::FieldDescriptor
Definition: src/google/protobuf/descriptor.h:515
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
google::protobuf::compiler::objectivec::HasPreservingUnknownEnumSemantics
bool HasPreservingUnknownEnumSemantics(const FileDescriptor *file)
Definition: objectivec_helpers.h:125
google::protobuf::compiler::objectivec::Options
Definition: objectivec_helpers.h:50
objectivec_helpers.h
google::protobuf::compiler::objectivec::FieldGenerator::variable
string variable(const char *key) const
Definition: objectivec_field.h:83
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::compiler::objectivec::EnumFieldGenerator::GenerateCFunctionDeclarations
virtual void GenerateCFunctionDeclarations(io::Printer *printer) const
Definition: objectivec_enum_field.cc:76
Descriptor
Definition: ruby/ext/google/protobuf_c/protobuf.h:113
google::protobuf::compiler::objectivec::RepeatedFieldGenerator::FinishInitialization
virtual void FinishInitialization(void)
Definition: objectivec_field.cc:358
objectivec_enum_field.h
FieldDescriptor
Definition: ruby/ext/google/protobuf_c/protobuf.h:129
google::protobuf::compiler::objectivec::RepeatedEnumFieldGenerator::FinishInitialization
virtual void FinishInitialization()
Definition: objectivec_enum_field.cc:140
google::protobuf::FieldDescriptor::file
const FileDescriptor * file() const
google::protobuf::EnumDescriptor::file
const FileDescriptor * file() const
google::protobuf::compiler::objectivec::ClassName
string ClassName(const Descriptor *descriptor)
Definition: objectivec_helpers.cc:460
printer.h
google::protobuf::compiler::objectivec::RepeatedFieldGenerator
Definition: objectivec_field.h:140
google::protobuf::io::Printer
Definition: printer.h:181
google::protobuf::compiler::objectivec::EnumFieldGenerator::DetermineForwardDeclarations
virtual void DetermineForwardDeclarations(std::set< string > *fwd_decls) const
Definition: objectivec_enum_field.cc:118
google::protobuf::compiler::objectivec::RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator
virtual ~RepeatedEnumFieldGenerator()
Definition: objectivec_enum_field.cc:138
google::protobuf::compiler::objectivec::EnumFieldGenerator::GenerateCFunctionImplementations
virtual void GenerateCFunctionImplementations(io::Printer *printer) const
Definition: objectivec_enum_field.cc:98
google::protobuf::compiler::objectivec::EnumFieldGenerator::~EnumFieldGenerator
virtual ~EnumFieldGenerator()
Definition: objectivec_enum_field.cc:74
type
GLenum type
Definition: glcorearb.h:2695
google::protobuf::compiler::objectivec::FieldGenerator::DetermineForwardDeclarations
virtual void DetermineForwardDeclarations(std::set< string > *fwd_decls) const
Definition: objectivec_field.cc:179
wire_format.h
google::protobuf::compiler::objectivec::FieldGenerator::variables_
std::map< string, string > variables_
Definition: objectivec_field.h:101
google::protobuf::compiler::objectivec::FieldGenerator::descriptor_
const FieldDescriptor * descriptor_
Definition: objectivec_field.h:100
google::protobuf::compiler::objectivec::EnumFieldGenerator::EnumFieldGenerator
EnumFieldGenerator(const EnumFieldGenerator &)=delete
google::protobuf::compiler::objectivec::EnumName
string EnumName(const EnumDescriptor *descriptor)
Definition: objectivec_helpers.cc:472
google::protobuf::compiler::objectivec::SingleFieldGenerator
Definition: objectivec_field.h:104
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:57