protobuf/src/google/protobuf/compiler/objectivec/objectivec_oneof.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 
34 #include <google/protobuf/compiler/objectivec/objectivec_oneof.h>
35 #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
36 #include <google/protobuf/io/printer.h>
37 #include <google/protobuf/stubs/strutil.h>
38 
39 namespace google {
40 namespace protobuf {
41 namespace compiler {
42 namespace objectivec {
43 
46  variables_["enum_name"] = OneofEnumName(descriptor_);
47  variables_["name"] = OneofName(descriptor_);
48  variables_["capitalized_name"] = OneofNameCapitalized(descriptor_);
49  variables_["raw_index"] = StrCat(descriptor_->index());
50  const Descriptor* msg_descriptor = descriptor_->containing_type();
51  variables_["owning_message_class"] = ClassName(msg_descriptor);
52 
53  std::string comments;
54  SourceLocation location;
55  if (descriptor_->GetSourceLocation(&location)) {
56  comments = BuildCommentsString(location, true);
57  } else {
58  comments = "";
59  }
60  variables_["comments"] = comments;
61 }
62 
64 
65 void OneofGenerator::SetOneofIndexBase(int index_base) {
66  int index = descriptor_->index() + index_base;
67  // Flip the sign to mark it as a oneof.
68  variables_["index"] = StrCat(-index);
69 }
70 
72  printer->Print(
73  variables_,
74  "typedef GPB_ENUM($enum_name$) {\n");
75  printer->Indent();
76  printer->Print(
77  variables_,
78  "$enum_name$_GPBUnsetOneOfCase = 0,\n");
79  std::string enum_name = variables_["enum_name"];
80  for (int j = 0; j < descriptor_->field_count(); j++) {
83  printer->Print(
84  "$enum_name$_$field_name$ = $field_number$,\n",
85  "enum_name", enum_name,
86  "field_name", field_name,
87  "field_number", StrCat(field->number()));
88  }
89  printer->Outdent();
90  printer->Print(
91  "};\n"
92  "\n");
93 }
94 
96  io::Printer* printer) {
97  printer->Print(
98  variables_,
99  "$comments$"
100  "@property(nonatomic, readonly) $enum_name$ $name$OneOfCase;\n"
101  "\n");
102 }
103 
105  printer->Print(
106  variables_,
107  "/**\n"
108  " * Clears whatever value was set for the oneof '$name$'.\n"
109  " **/\n"
110  "void $owning_message_class$_Clear$capitalized_name$OneOfCase($owning_message_class$ *message);\n");
111 }
112 
114  printer->Print(
115  variables_,
116  "@dynamic $name$OneOfCase;\n");
117 }
118 
120  printer->Print(
121  variables_,
122  "void $owning_message_class$_Clear$capitalized_name$OneOfCase($owning_message_class$ *message) {\n"
123  " GPBDescriptor *descriptor = [$owning_message_class$ descriptor];\n"
124  " GPBOneofDescriptor *oneof = [descriptor.oneofs objectAtIndex:$raw_index$];\n"
125  " GPBClearOneof(message, oneof);\n"
126  "}\n");
127 }
128 
130  return variables_.find("name")->second;
131 }
132 
134  return variables_.find("index")->second;
135 }
136 
137 } // namespace objectivec
138 } // namespace compiler
139 } // namespace protobuf
140 } // namespace google
descriptor_
string_view descriptor_
Definition: elf.cc:154
absl::str_format_internal::LengthMod::j
@ j
google::protobuf::compiler::objectivec::OneofEnumName
string OneofEnumName(const OneofDescriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:563
google::protobuf::compiler::objectivec::OneofGenerator::descriptor_
const OneofDescriptor * descriptor_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_oneof.h:67
google::protobuf::compiler::objectivec::ClassName
string ClassName(const Descriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:460
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf::compiler::objectivec::OneofGenerator::GenerateClearFunctionDeclaration
void GenerateClearFunctionDeclaration(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc:104
grpc::protobuf::io::Printer
GRPC_CUSTOM_PRINTER Printer
Definition: src/compiler/config.h:54
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf::compiler::objectivec::OneofGenerator::GeneratePropertyImplementation
void GeneratePropertyImplementation(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc:113
google::protobuf::compiler::objectivec::OneofGenerator::~OneofGenerator
~OneofGenerator()
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc:63
google::protobuf::compiler::objectivec::OneofGenerator::HasIndexAsString
string HasIndexAsString(void) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc:133
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:1482
google::protobuf::OneofDescriptor::field
const FieldDescriptor * field(int index) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:2179
Descriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:121
FieldDescriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:133
google::protobuf::OneofDescriptor::GetSourceLocation
bool GetSourceLocation(SourceLocation *out_location) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:2984
google::protobuf::compiler::objectivec::OneofGenerator::OneofGenerator
OneofGenerator(const OneofDescriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc:44
google::protobuf::compiler::objectivec::OneofName
string OneofName(const OneofDescriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:571
google::protobuf::compiler::objectivec::OneofNameCapitalized
string OneofNameCapitalized(const OneofDescriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:578
google::protobuf::compiler::objectivec::FieldNameCapitalized
string FieldNameCapitalized(const FieldDescriptor *field)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:553
field
const FieldDescriptor * field
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/parser_unittest.cc:2692
google::protobuf::compiler::objectivec::OneofGenerator::DescriptorName
string DescriptorName(void) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc:129
OneofDescriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:138
index
int index
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1184
google::protobuf::compiler::objectivec::OneofGenerator::variables_
std::map< string, string > variables_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_oneof.h:68
google::protobuf::OneofDescriptor::index
int index() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:2103
google::protobuf::compiler::objectivec::OneofGenerator::SetOneofIndexBase
void SetOneofIndexBase(int index_base)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc:65
google::protobuf::compiler::objectivec::OneofGenerator::GenerateCaseEnum
void GenerateCaseEnum(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc:71
google::protobuf::compiler::objectivec::OneofGenerator::GenerateClearFunctionImplementation
void GenerateClearFunctionImplementation(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc:119
google::protobuf::compiler::objectivec::OneofGenerator::GeneratePublicCasePropertyDeclaration
void GeneratePublicCasePropertyDeclaration(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc:95
grpc::protobuf::SourceLocation
GRPC_CUSTOM_SOURCELOCATION SourceLocation
Definition: include/grpcpp/impl/codegen/config_protobuf.h:90
google::protobuf::OneofDescriptor::field_count
int field_count() const
descriptor
static const char descriptor[1336]
Definition: certs.upbdefs.c:16
google::protobuf::OneofDescriptor::containing_type
const Descriptor * containing_type() const
compiler
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/plugin.pb.cc:21
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
google::protobuf::compiler::objectivec::BuildCommentsString
string BuildCommentsString(const SourceLocation &location, bool prefer_single_line)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc:928


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:34