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 | #ifndef GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__ |
32 | #define GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__ |
33 | |
34 | #include <map> |
35 | #include <string> |
36 | #include <google/protobuf/descriptor.h> |
37 | #include <google/protobuf/io/printer.h> |
38 | |
39 | namespace google { |
40 | namespace protobuf { |
41 | namespace compiler { |
42 | namespace objectivec { |
43 | |
44 | class FieldGenerator { |
45 | public: |
46 | static FieldGenerator* Make(const FieldDescriptor* field); |
47 | |
48 | virtual ~FieldGenerator(); |
49 | |
50 | FieldGenerator(const FieldGenerator&) = delete; |
51 | FieldGenerator& operator=(const FieldGenerator&) = delete; |
52 | |
53 | // Exposed for subclasses to fill in. |
54 | virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const = 0; |
55 | virtual void GeneratePropertyDeclaration(io::Printer* printer) const = 0; |
56 | virtual void GeneratePropertyImplementation(io::Printer* printer) const = 0; |
57 | |
58 | // Called by GenerateFieldDescription, exposed for classes that need custom |
59 | // generation. |
60 | |
61 | // Exposed for subclasses to extend, base does nothing. |
62 | virtual void GenerateCFunctionDeclarations(io::Printer* printer) const; |
63 | virtual void GenerateCFunctionImplementations(io::Printer* printer) const; |
64 | |
65 | // Exposed for subclasses, should always call it on the parent class also. |
66 | virtual void DetermineForwardDeclarations( |
67 | std::set<std::string>* fwd_decls, |
68 | bool include_external_types) const; |
69 | virtual void DetermineObjectiveCClassDefinitions( |
70 | std::set<std::string>* fwd_decls) const; |
71 | |
72 | // Used during generation, not intended to be extended by subclasses. |
73 | void GenerateFieldDescription( |
74 | io::Printer* printer, bool include_default) const; |
75 | void GenerateFieldNumberConstant(io::Printer* printer) const; |
76 | |
77 | // Exposed to get and set the has bits information. |
78 | virtual bool RuntimeUsesHasBit(void) const = 0; |
79 | void SetRuntimeHasBit(int has_index); |
80 | void SetNoHasBit(void); |
81 | virtual int (void) const; |
82 | virtual void (int index_base); |
83 | void SetOneofIndexBase(int index_base); |
84 | |
85 | std::string variable(const char* key) const { |
86 | return variables_.find(x: key)->second; |
87 | } |
88 | |
89 | bool needs_textformat_name_support() const { |
90 | const std::string& field_flags = variable(key: "fieldflags" ); |
91 | return field_flags.find(s: "GPBFieldTextFormatNameCustom" ) != |
92 | std::string::npos; |
93 | } |
94 | std::string generated_objc_name() const { return variable(key: "name" ); } |
95 | std::string raw_field_name() const { return variable(key: "raw_field_name" ); } |
96 | |
97 | protected: |
98 | explicit FieldGenerator(const FieldDescriptor* descriptor); |
99 | |
100 | virtual void FinishInitialization(void); |
101 | bool WantsHasProperty(void) const; |
102 | |
103 | const FieldDescriptor* descriptor_; |
104 | std::map<std::string, std::string> variables_; |
105 | }; |
106 | |
107 | class SingleFieldGenerator : public FieldGenerator { |
108 | public: |
109 | virtual ~SingleFieldGenerator(); |
110 | |
111 | SingleFieldGenerator(const SingleFieldGenerator&) = delete; |
112 | SingleFieldGenerator& operator=(const SingleFieldGenerator&) = delete; |
113 | |
114 | virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const override; |
115 | virtual void GeneratePropertyDeclaration(io::Printer* printer) const override; |
116 | |
117 | virtual void GeneratePropertyImplementation(io::Printer* printer) const override; |
118 | |
119 | virtual bool RuntimeUsesHasBit(void) const override; |
120 | |
121 | protected: |
122 | explicit SingleFieldGenerator(const FieldDescriptor* descriptor); |
123 | }; |
124 | |
125 | // Subclass with common support for when the field ends up as an ObjC Object. |
126 | class ObjCObjFieldGenerator : public SingleFieldGenerator { |
127 | public: |
128 | virtual ~ObjCObjFieldGenerator(); |
129 | |
130 | ObjCObjFieldGenerator(const ObjCObjFieldGenerator&) = delete; |
131 | ObjCObjFieldGenerator& operator=(const ObjCObjFieldGenerator&) = delete; |
132 | |
133 | virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const override; |
134 | virtual void GeneratePropertyDeclaration(io::Printer* printer) const override; |
135 | |
136 | protected: |
137 | explicit ObjCObjFieldGenerator(const FieldDescriptor* descriptor); |
138 | }; |
139 | |
140 | class RepeatedFieldGenerator : public ObjCObjFieldGenerator { |
141 | public: |
142 | virtual ~RepeatedFieldGenerator(); |
143 | |
144 | RepeatedFieldGenerator(const RepeatedFieldGenerator&) = delete; |
145 | RepeatedFieldGenerator& operator=(const RepeatedFieldGenerator&) = delete; |
146 | |
147 | virtual void GenerateFieldStorageDeclaration(io::Printer* printer) const override; |
148 | virtual void GeneratePropertyDeclaration(io::Printer* printer) const override; |
149 | |
150 | virtual void GeneratePropertyImplementation(io::Printer* printer) const override; |
151 | |
152 | virtual bool RuntimeUsesHasBit(void) const override; |
153 | |
154 | protected: |
155 | explicit RepeatedFieldGenerator(const FieldDescriptor* descriptor); |
156 | virtual void FinishInitialization(void) override; |
157 | }; |
158 | |
159 | // Convenience class which constructs FieldGenerators for a Descriptor. |
160 | class FieldGeneratorMap { |
161 | public: |
162 | explicit FieldGeneratorMap(const Descriptor* descriptor); |
163 | ~FieldGeneratorMap(); |
164 | |
165 | FieldGeneratorMap(const FieldGeneratorMap&) = delete; |
166 | FieldGeneratorMap& operator=(const FieldGeneratorMap&) = delete; |
167 | |
168 | const FieldGenerator& get(const FieldDescriptor* field) const; |
169 | const FieldGenerator& get_extension(int index) const; |
170 | |
171 | // Assigns the has bits and returns the number of bits needed. |
172 | int CalculateHasBits(void); |
173 | |
174 | void SetOneofIndexBase(int index_base); |
175 | |
176 | // Check if any field of this message has a non zero default. |
177 | bool DoesAnyFieldHaveNonZeroDefault(void) const; |
178 | |
179 | private: |
180 | const Descriptor* descriptor_; |
181 | std::vector<std::unique_ptr<FieldGenerator>> field_generators_; |
182 | std::vector<std::unique_ptr<FieldGenerator>> extension_generators_; |
183 | }; |
184 | |
185 | } // namespace objectivec |
186 | } // namespace compiler |
187 | } // namespace protobuf |
188 | } // namespace google |
189 | |
190 | #endif // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FIELD_H__ |
191 | |