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