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 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34
35 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__
36 #define GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__
37
38 #include <map>
39 #include <memory>
40 #include <string>
41
42 #include <google/protobuf/stubs/logging.h>
43 #include <google/protobuf/stubs/common.h>
44 #include <google/protobuf/descriptor.h>
45
46 namespace google {
47 namespace protobuf {
48 namespace compiler {
49 namespace java {
50 class Context; // context.h
51 class ClassNameResolver; // name_resolver.h
52 } // namespace java
53 } // namespace compiler
54 namespace io {
55 class Printer; // printer.h
56 }
57 } // namespace protobuf
58 } // namespace google
59
60 namespace google {
61 namespace protobuf {
62 namespace compiler {
63 namespace java {
64
65 class ImmutableFieldGenerator {
66 public:
ImmutableFieldGenerator()67 ImmutableFieldGenerator() {}
68 virtual ~ImmutableFieldGenerator();
69
70 virtual int GetNumBitsForMessage() const = 0;
71 virtual int GetNumBitsForBuilder() const = 0;
72 virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0;
73 virtual void GenerateMembers(io::Printer* printer) const = 0;
74 virtual void GenerateBuilderMembers(io::Printer* printer) const = 0;
75 virtual void GenerateInitializationCode(io::Printer* printer) const = 0;
76 virtual void GenerateBuilderClearCode(io::Printer* printer) const = 0;
77 virtual void GenerateMergingCode(io::Printer* printer) const = 0;
78 virtual void GenerateBuildingCode(io::Printer* printer) const = 0;
79 virtual void GenerateBuilderParsingCode(io::Printer* printer) const = 0;
80 virtual void GenerateBuilderParsingCodeFromPacked(io::Printer* printer) const;
81 virtual void GenerateSerializationCode(io::Printer* printer) const = 0;
82 virtual void GenerateSerializedSizeCode(io::Printer* printer) const = 0;
83 virtual void GenerateFieldBuilderInitializationCode(
84 io::Printer* printer) const = 0;
85
86 virtual void GenerateEqualsCode(io::Printer* printer) const = 0;
87 virtual void GenerateHashCode(io::Printer* printer) const = 0;
88
89 virtual std::string GetBoxedType() const = 0;
90
91 private:
92 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableFieldGenerator);
93 };
94
95 class ImmutableFieldLiteGenerator {
96 public:
ImmutableFieldLiteGenerator()97 ImmutableFieldLiteGenerator() {}
98 virtual ~ImmutableFieldLiteGenerator();
99
100 virtual int GetNumBitsForMessage() const = 0;
101 virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0;
102 virtual void GenerateMembers(io::Printer* printer) const = 0;
103 virtual void GenerateBuilderMembers(io::Printer* printer) const = 0;
104 virtual void GenerateInitializationCode(io::Printer* printer) const = 0;
105 virtual void GenerateFieldInfo(io::Printer* printer,
106 std::vector<uint16>* output) const = 0;
107
108 virtual std::string GetBoxedType() const = 0;
109
110 private:
111 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableFieldLiteGenerator);
112 };
113
114
115 // Convenience class which constructs FieldGenerators for a Descriptor.
116 template <typename FieldGeneratorType>
117 class FieldGeneratorMap {
118 public:
119 explicit FieldGeneratorMap(const Descriptor* descriptor, Context* context);
120 ~FieldGeneratorMap();
121
122 const FieldGeneratorType& get(const FieldDescriptor* field) const;
123
124 private:
125 const Descriptor* descriptor_;
126 std::vector<std::unique_ptr<FieldGeneratorType>> field_generators_;
127
128 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap);
129 };
130
131 template <typename FieldGeneratorType>
get(const FieldDescriptor * field)132 inline const FieldGeneratorType& FieldGeneratorMap<FieldGeneratorType>::get(
133 const FieldDescriptor* field) const {
134 GOOGLE_CHECK_EQ(field->containing_type(), descriptor_);
135 return *field_generators_[field->index()];
136 }
137
138 // Instantiate template for mutable and immutable maps.
139 template <>
140 FieldGeneratorMap<ImmutableFieldGenerator>::FieldGeneratorMap(
141 const Descriptor* descriptor, Context* context);
142
143 template <>
144 FieldGeneratorMap<ImmutableFieldGenerator>::~FieldGeneratorMap();
145
146
147 template <>
148 FieldGeneratorMap<ImmutableFieldLiteGenerator>::FieldGeneratorMap(
149 const Descriptor* descriptor, Context* context);
150
151 template <>
152 FieldGeneratorMap<ImmutableFieldLiteGenerator>::~FieldGeneratorMap();
153
154
155 // Field information used in FieldGeneartors.
156 struct FieldGeneratorInfo {
157 std::string name;
158 std::string capitalized_name;
159 std::string disambiguated_reason;
160 };
161
162 // Oneof information used in OneofFieldGenerators.
163 struct OneofGeneratorInfo {
164 std::string name;
165 std::string capitalized_name;
166 };
167
168 // Set some common variables used in variable FieldGenerators.
169 void SetCommonFieldVariables(const FieldDescriptor* descriptor,
170 const FieldGeneratorInfo* info,
171 std::map<std::string, std::string>* variables);
172
173 // Set some common oneof variables used in OneofFieldGenerators.
174 void SetCommonOneofVariables(const FieldDescriptor* descriptor,
175 const OneofGeneratorInfo* info,
176 std::map<std::string, std::string>* variables);
177
178 // Print useful comments before a field's accessors.
179 void PrintExtraFieldInfo(const std::map<std::string, std::string>& variables,
180 io::Printer* printer);
181
182 } // namespace java
183 } // namespace compiler
184 } // namespace protobuf
185 } // namespace google
186
187 #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__
188