Line data Source code
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 :
44 0 : OneofGenerator::OneofGenerator(const OneofDescriptor* descriptor)
45 0 : : descriptor_(descriptor) {
46 0 : variables_["enum_name"] = OneofEnumName(descriptor_);
47 0 : variables_["name"] = OneofName(descriptor_);
48 0 : variables_["capitalized_name"] = OneofNameCapitalized(descriptor_);
49 0 : variables_["raw_index"] = SimpleItoa(descriptor_->index());
50 0 : const Descriptor* msg_descriptor = descriptor_->containing_type();
51 0 : variables_["owning_message_class"] = ClassName(msg_descriptor);
52 :
53 : string comments;
54 0 : SourceLocation location;
55 0 : if (descriptor_->GetSourceLocation(&location)) {
56 0 : comments = BuildCommentsString(location);
57 : } else {
58 : comments = "";
59 : }
60 0 : variables_["comments"] = comments;
61 0 : }
62 :
63 0 : OneofGenerator::~OneofGenerator() {}
64 :
65 0 : void OneofGenerator::SetOneofIndexBase(int index_base) {
66 0 : int index = descriptor_->index() + index_base;
67 : // Flip the sign to mark it as a oneof.
68 0 : variables_["index"] = SimpleItoa(-index);
69 0 : }
70 :
71 0 : void OneofGenerator::GenerateCaseEnum(io::Printer* printer) {
72 : printer->Print(
73 : variables_,
74 0 : "typedef GPB_ENUM($enum_name$) {\n");
75 0 : printer->Indent();
76 : printer->Print(
77 : variables_,
78 0 : "$enum_name$_GPBUnsetOneOfCase = 0,\n");
79 0 : string enum_name = variables_["enum_name"];
80 0 : for (int j = 0; j < descriptor_->field_count(); j++) {
81 0 : const FieldDescriptor* field = descriptor_->field(j);
82 0 : string field_name = FieldNameCapitalized(field);
83 : printer->Print(
84 : "$enum_name$_$field_name$ = $field_number$,\n",
85 : "enum_name", enum_name,
86 : "field_name", field_name,
87 0 : "field_number", SimpleItoa(field->number()));
88 : }
89 0 : printer->Outdent();
90 : printer->Print(
91 : "};\n"
92 0 : "\n");
93 0 : }
94 :
95 0 : void OneofGenerator::GeneratePublicCasePropertyDeclaration(
96 : io::Printer* printer) {
97 : printer->Print(
98 : variables_,
99 : "$comments$"
100 : "@property(nonatomic, readonly) $enum_name$ $name$OneOfCase;\n"
101 0 : "\n");
102 0 : }
103 :
104 0 : void OneofGenerator::GenerateClearFunctionDeclaration(io::Printer* printer) {
105 : printer->Print(
106 : variables_,
107 0 : "void $owning_message_class$_Clear$capitalized_name$OneOfCase($owning_message_class$ *message);\n");
108 0 : }
109 :
110 0 : void OneofGenerator::GeneratePropertyImplementation(io::Printer* printer) {
111 : printer->Print(
112 : variables_,
113 0 : "@dynamic $name$OneOfCase;\n");
114 0 : }
115 :
116 0 : void OneofGenerator::GenerateClearFunctionImplementation(io::Printer* printer) {
117 : printer->Print(
118 : variables_,
119 : "void $owning_message_class$_Clear$capitalized_name$OneOfCase($owning_message_class$ *message) {\n"
120 : " GPBDescriptor *descriptor = [message descriptor];\n"
121 : " GPBOneofDescriptor *oneof = descriptor->oneofs_[$raw_index$];\n"
122 : " GPBMaybeClearOneof(message, oneof, 0);\n"
123 0 : "}\n");
124 0 : }
125 :
126 0 : void OneofGenerator::GenerateDescription(io::Printer* printer) {
127 : printer->Print(
128 : variables_,
129 : "{\n"
130 : " .name = \"$name$\",\n"
131 : " .index = $index$,\n"
132 0 : "},\n");
133 0 : }
134 :
135 : } // namespace objectivec
136 : } // namespace compiler
137 : } // namespace protobuf
138 : } // namespace google
|