temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2 | // Copyright 2008 Google Inc. All rights reserved. |
Feng Xiao | e428862 | 2014-10-01 16:26:23 -0700 | [diff] [blame] | 3 | // https://developers.google.com/protocol-buffers/ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 4 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 8 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 18 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 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 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 38 | #include <map> |
| 39 | #include <memory> |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 40 | #ifndef _SHARED_PTR_H |
| 41 | #include <google/protobuf/stubs/shared_ptr.h> |
| 42 | #endif |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 43 | #include <string> |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 44 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 45 | #include <google/protobuf/stubs/common.h> |
| 46 | #include <google/protobuf/descriptor.h> |
| 47 | |
| 48 | namespace google { |
| 49 | namespace protobuf { |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 50 | namespace compiler { |
| 51 | namespace java { |
| 52 | class Context; // context.h |
| 53 | class ClassNameResolver; // name_resolver.h |
| 54 | } |
| 55 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 56 | namespace io { |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 57 | class Printer; // printer.h |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | namespace protobuf { |
| 62 | namespace compiler { |
| 63 | namespace java { |
| 64 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 65 | class ImmutableFieldGenerator { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 66 | public: |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 67 | ImmutableFieldGenerator() {} |
| 68 | virtual ~ImmutableFieldGenerator(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 69 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 70 | virtual int GetNumBitsForMessage() const = 0; |
| 71 | virtual int GetNumBitsForBuilder() const = 0; |
| 72 | virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 73 | virtual void GenerateMembers(io::Printer* printer) const = 0; |
| 74 | virtual void GenerateBuilderMembers(io::Printer* printer) const = 0; |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 75 | virtual void GenerateInitializationCode(io::Printer* printer) const = 0; |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 76 | virtual void GenerateBuilderClearCode(io::Printer* printer) const = 0; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 77 | 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.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 80 | virtual void GenerateParsingCodeFromPacked(io::Printer* printer) const; |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 81 | virtual void GenerateParsingDoneCode(io::Printer* printer) const = 0; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 82 | virtual void GenerateSerializationCode(io::Printer* printer) const = 0; |
| 83 | virtual void GenerateSerializedSizeCode(io::Printer* printer) const = 0; |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 84 | virtual void GenerateFieldBuilderInitializationCode(io::Printer* printer) |
| 85 | const = 0; |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 86 | virtual void GenerateStaticInitializationCode(io::Printer* printer) const {} |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 87 | |
| 88 | virtual void GenerateEqualsCode(io::Printer* printer) const = 0; |
| 89 | virtual void GenerateHashCode(io::Printer* printer) const = 0; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 90 | |
| 91 | virtual string GetBoxedType() const = 0; |
| 92 | |
| 93 | private: |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 94 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableFieldGenerator); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 97 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 98 | // Convenience class which constructs FieldGenerators for a Descriptor. |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 99 | template<typename FieldGeneratorType> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 100 | class FieldGeneratorMap { |
| 101 | public: |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 102 | explicit FieldGeneratorMap(const Descriptor* descriptor, |
| 103 | Context* context); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 104 | ~FieldGeneratorMap(); |
| 105 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 106 | const FieldGeneratorType& get(const FieldDescriptor* field) const; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 107 | |
| 108 | private: |
| 109 | const Descriptor* descriptor_; |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 110 | Context* context_; |
| 111 | ClassNameResolver* name_resolver_; |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 112 | google::protobuf::scoped_array<google::protobuf::scoped_ptr<FieldGeneratorType> > field_generators_; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 113 | |
| 114 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap); |
| 115 | }; |
| 116 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 117 | template<typename FieldGeneratorType> |
| 118 | inline const FieldGeneratorType& |
| 119 | FieldGeneratorMap<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. |
| 125 | template<> |
| 126 | FieldGeneratorMap<ImmutableFieldGenerator>:: |
| 127 | FieldGeneratorMap(const Descriptor* descriptor, |
| 128 | Context* context); |
| 129 | |
| 130 | template<> |
| 131 | FieldGeneratorMap<ImmutableFieldGenerator>::~FieldGeneratorMap(); |
| 132 | |
| 133 | |
| 134 | // Field information used in FieldGeneartors. |
| 135 | struct FieldGeneratorInfo { |
| 136 | string name; |
| 137 | string capitalized_name; |
| 138 | string disambiguated_reason; |
| 139 | }; |
| 140 | |
| 141 | // Oneof information used in OneofFieldGeneartors. |
| 142 | struct OneofGeneratorInfo { |
| 143 | string name; |
| 144 | string capitalized_name; |
| 145 | }; |
| 146 | |
| 147 | // Set some common variables used in variable FieldGenerators. |
| 148 | void SetCommonFieldVariables(const FieldDescriptor* descriptor, |
| 149 | const FieldGeneratorInfo* info, |
| 150 | map<string, string>* variables); |
| 151 | |
| 152 | // Set some common oneof variables used in OneofFieldGenerators. |
| 153 | void SetCommonOneofVariables(const FieldDescriptor* descriptor, |
| 154 | const OneofGeneratorInfo* info, |
| 155 | map<string, string>* variables); |
| 156 | |
| 157 | // Print useful comments before a field's accessors. |
| 158 | void PrintExtraFieldInfo(const map<string, string>& variables, |
| 159 | io::Printer* printer); |
| 160 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 161 | } // namespace java |
| 162 | } // namespace compiler |
| 163 | } // namespace protobuf |
| 164 | |
| 165 | } // namespace google |
| 166 | #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__ |