blob: 1cf360c111008d8d8adb6a4ba9a4e82884aafc5a [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
temporal40ee5512008-07-10 02:12:20 +00004//
kenton@google.com24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
kenton@google.com24bf56f2008-09-24 20:31:01 +00009// * 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.
temporal40ee5512008-07-10 02:12:20 +000018//
kenton@google.com24bf56f2008-09-24 20:31:01 +000019// 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.
temporal40ee5512008-07-10 02:12:20 +000030
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
jieluo@google.com4de8f552014-07-18 00:47:59 +000038#include <map>
39#include <memory>
Feng Xiao6ef984a2014-11-10 17:34:54 -080040#ifndef _SHARED_PTR_H
41#include <google/protobuf/stubs/shared_ptr.h>
42#endif
temporal40ee5512008-07-10 02:12:20 +000043#include <string>
jieluo@google.com4de8f552014-07-18 00:47:59 +000044
temporal40ee5512008-07-10 02:12:20 +000045#include <google/protobuf/stubs/common.h>
46#include <google/protobuf/descriptor.h>
47
48namespace google {
49namespace protobuf {
jieluo@google.com4de8f552014-07-18 00:47:59 +000050 namespace compiler {
51 namespace java {
52 class Context; // context.h
53 class ClassNameResolver; // name_resolver.h
54 }
55 }
temporal40ee5512008-07-10 02:12:20 +000056 namespace io {
jieluo@google.com4de8f552014-07-18 00:47:59 +000057 class Printer; // printer.h
temporal40ee5512008-07-10 02:12:20 +000058 }
59}
60
61namespace protobuf {
62namespace compiler {
63namespace java {
64
jieluo@google.com4de8f552014-07-18 00:47:59 +000065class ImmutableFieldGenerator {
temporal40ee5512008-07-10 02:12:20 +000066 public:
jieluo@google.com4de8f552014-07-18 00:47:59 +000067 ImmutableFieldGenerator() {}
68 virtual ~ImmutableFieldGenerator();
temporal40ee5512008-07-10 02:12:20 +000069
liujisi@google.com33165fe2010-11-02 13:14:58 +000070 virtual int GetNumBitsForMessage() const = 0;
71 virtual int GetNumBitsForBuilder() const = 0;
72 virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0;
temporal40ee5512008-07-10 02:12:20 +000073 virtual void GenerateMembers(io::Printer* printer) const = 0;
74 virtual void GenerateBuilderMembers(io::Printer* printer) const = 0;
kenton@google.comfccb1462009-12-18 02:11:36 +000075 virtual void GenerateInitializationCode(io::Printer* printer) const = 0;
liujisi@google.com33165fe2010-11-02 13:14:58 +000076 virtual void GenerateBuilderClearCode(io::Printer* printer) const = 0;
temporal40ee5512008-07-10 02:12:20 +000077 virtual void GenerateMergingCode(io::Printer* printer) const = 0;
78 virtual void GenerateBuildingCode(io::Printer* printer) const = 0;
79 virtual void GenerateParsingCode(io::Printer* printer) const = 0;
kenton@google.comfccb1462009-12-18 02:11:36 +000080 virtual void GenerateParsingCodeFromPacked(io::Printer* printer) const;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000081 virtual void GenerateParsingDoneCode(io::Printer* printer) const = 0;
temporal40ee5512008-07-10 02:12:20 +000082 virtual void GenerateSerializationCode(io::Printer* printer) const = 0;
83 virtual void GenerateSerializedSizeCode(io::Printer* printer) const = 0;
liujisi@google.com33165fe2010-11-02 13:14:58 +000084 virtual void GenerateFieldBuilderInitializationCode(io::Printer* printer)
85 const = 0;
Jisi Liu885b6122015-02-28 14:51:22 -080086 virtual void GenerateStaticInitializationCode(io::Printer* printer) const {}
liujisi@google.com33165fe2010-11-02 13:14:58 +000087
88 virtual void GenerateEqualsCode(io::Printer* printer) const = 0;
89 virtual void GenerateHashCode(io::Printer* printer) const = 0;
temporal40ee5512008-07-10 02:12:20 +000090
91 virtual string GetBoxedType() const = 0;
92
93 private:
jieluo@google.com4de8f552014-07-18 00:47:59 +000094 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableFieldGenerator);
temporal40ee5512008-07-10 02:12:20 +000095};
96
jieluo@google.com4de8f552014-07-18 00:47:59 +000097
temporal40ee5512008-07-10 02:12:20 +000098// Convenience class which constructs FieldGenerators for a Descriptor.
jieluo@google.com4de8f552014-07-18 00:47:59 +000099template<typename FieldGeneratorType>
temporal40ee5512008-07-10 02:12:20 +0000100class FieldGeneratorMap {
101 public:
jieluo@google.com4de8f552014-07-18 00:47:59 +0000102 explicit FieldGeneratorMap(const Descriptor* descriptor,
103 Context* context);
temporal40ee5512008-07-10 02:12:20 +0000104 ~FieldGeneratorMap();
105
jieluo@google.com4de8f552014-07-18 00:47:59 +0000106 const FieldGeneratorType& get(const FieldDescriptor* field) const;
temporal40ee5512008-07-10 02:12:20 +0000107
108 private:
109 const Descriptor* descriptor_;
jieluo@google.com4de8f552014-07-18 00:47:59 +0000110 Context* context_;
111 ClassNameResolver* name_resolver_;
Feng Xiaof157a562014-11-14 11:50:31 -0800112 google::protobuf::scoped_array<google::protobuf::scoped_ptr<FieldGeneratorType> > field_generators_;
temporal40ee5512008-07-10 02:12:20 +0000113
114 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap);
115};
116
jieluo@google.com4de8f552014-07-18 00:47:59 +0000117template<typename FieldGeneratorType>
118inline const FieldGeneratorType&
119FieldGeneratorMap<FieldGeneratorType>::get(const FieldDescriptor* field) const {
120 GOOGLE_CHECK_EQ(field->containing_type(), descriptor_);
121 return *field_generators_[field->index()];
122}
123
124// Instantiate template for mutable and immutable maps.
125template<>
126FieldGeneratorMap<ImmutableFieldGenerator>::
127FieldGeneratorMap(const Descriptor* descriptor,
128 Context* context);
129
130template<>
131FieldGeneratorMap<ImmutableFieldGenerator>::~FieldGeneratorMap();
132
133
134// Field information used in FieldGeneartors.
135struct FieldGeneratorInfo {
136 string name;
137 string capitalized_name;
138 string disambiguated_reason;
139};
140
141// Oneof information used in OneofFieldGeneartors.
142struct OneofGeneratorInfo {
143 string name;
144 string capitalized_name;
145};
146
147// Set some common variables used in variable FieldGenerators.
148void SetCommonFieldVariables(const FieldDescriptor* descriptor,
149 const FieldGeneratorInfo* info,
150 map<string, string>* variables);
151
152// Set some common oneof variables used in OneofFieldGenerators.
153void SetCommonOneofVariables(const FieldDescriptor* descriptor,
154 const OneofGeneratorInfo* info,
155 map<string, string>* variables);
156
157// Print useful comments before a field's accessors.
158void PrintExtraFieldInfo(const map<string, string>& variables,
159 io::Printer* printer);
160
temporal40ee5512008-07-10 02:12:20 +0000161} // namespace java
162} // namespace compiler
163} // namespace protobuf
164
165} // namespace google
166#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__