blob: cd2b6b75be418a51ebe674ac95e5ac0456b4b4c8 [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_CPP_FIELD_H__
36#define GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
37
kenton@google.com80b1d622009-07-29 01:13:20 +000038#include <map>
jieluo@google.com4de8f552014-07-18 00:47:59 +000039#include <memory>
Feng Xiao6ef984a2014-11-10 17:34:54 -080040#ifndef _SHARED_PTR_H
41#include <google/protobuf/stubs/shared_ptr.h>
42#endif
kenton@google.com80b1d622009-07-29 01:13:20 +000043#include <string>
44
temporal40ee5512008-07-10 02:12:20 +000045#include <google/protobuf/descriptor.h>
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000046#include <google/protobuf/compiler/cpp/cpp_options.h>
temporal40ee5512008-07-10 02:12:20 +000047
48namespace google {
49namespace protobuf {
50 namespace io {
51 class Printer; // printer.h
52 }
53}
54
55namespace protobuf {
56namespace compiler {
57namespace cpp {
58
kenton@google.com80b1d622009-07-29 01:13:20 +000059// Helper function: set variables in the map that are the same for all
60// field code generators.
61// ['name', 'index', 'number', 'classname', 'declared_type', 'tag_size',
62// 'deprecation'].
63void SetCommonFieldVariables(const FieldDescriptor* descriptor,
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000064 map<string, string>* variables,
65 const Options& options);
kenton@google.com80b1d622009-07-29 01:13:20 +000066
jieluo@google.com4de8f552014-07-18 00:47:59 +000067void SetCommonOneofFieldVariables(const FieldDescriptor* descriptor,
68 map<string, string>* variables);
69
temporal40ee5512008-07-10 02:12:20 +000070class FieldGenerator {
71 public:
72 FieldGenerator() {}
73 virtual ~FieldGenerator();
74
75 // Generate lines of code declaring members fields of the message class
76 // needed to represent this field. These are placed inside the message
77 // class.
78 virtual void GeneratePrivateMembers(io::Printer* printer) const = 0;
79
jieluo@google.com4de8f552014-07-18 00:47:59 +000080 // Generate static default variable for this field. These are placed inside
81 // the message class. Most field types don't need this, so the default
82 // implementation is empty.
Austin Schuhec71d5d2014-10-31 19:15:54 -070083 virtual void GenerateStaticMembers(io::Printer* /*printer*/) const {}
jieluo@google.com4de8f552014-07-18 00:47:59 +000084
temporal40ee5512008-07-10 02:12:20 +000085 // Generate prototypes for all of the accessor functions related to this
86 // field. These are placed inside the class definition.
87 virtual void GenerateAccessorDeclarations(io::Printer* printer) const = 0;
88
89 // Generate inline definitions of accessor functions for this field.
90 // These are placed inside the header after all class definitions.
91 virtual void GenerateInlineAccessorDefinitions(
Jisi Liu885b6122015-02-28 14:51:22 -080092 io::Printer* printer, bool is_inline) const = 0;
temporal40ee5512008-07-10 02:12:20 +000093
94 // Generate definitions of accessors that aren't inlined. These are
95 // placed somewhere in the .cc file.
96 // Most field types don't need this, so the default implementation is empty.
97 virtual void GenerateNonInlineAccessorDefinitions(
Austin Schuhec71d5d2014-10-31 19:15:54 -070098 io::Printer* /*printer*/) const {}
temporal40ee5512008-07-10 02:12:20 +000099
100 // Generate lines of code (statements, not declarations) which clear the
101 // field. This is used to define the clear_$name$() method as well as
102 // the Clear() method for the whole message.
103 virtual void GenerateClearingCode(io::Printer* printer) const = 0;
104
105 // Generate lines of code (statements, not declarations) which merges the
106 // contents of the field from the current message to the target message,
107 // which is stored in the generated code variable "from".
108 // This is used to fill in the MergeFrom method for the whole message.
109 // Details of this usage can be found in message.cc under the
110 // GenerateMergeFrom method.
111 virtual void GenerateMergingCode(io::Printer* printer) const = 0;
112
kenton@google.com26bd9ee2008-11-21 00:06:27 +0000113 // Generate lines of code (statements, not declarations) which swaps
114 // this field and the corresponding field of another message, which
115 // is stored in the generated code variable "other". This is used to
116 // define the Swap method. Details of usage can be found in
117 // message.cc under the GenerateSwap method.
118 virtual void GenerateSwappingCode(io::Printer* printer) const = 0;
119
kenton@google.comd37d46d2009-04-25 02:53:47 +0000120 // Generate initialization code for private members declared by
121 // GeneratePrivateMembers(). These go into the message class's SharedCtor()
122 // method, invoked by each of the generated constructors.
123 virtual void GenerateConstructorCode(io::Printer* printer) const = 0;
temporal40ee5512008-07-10 02:12:20 +0000124
kenton@google.comd37d46d2009-04-25 02:53:47 +0000125 // Generate any code that needs to go in the class's SharedDtor() method,
126 // invoked by the destructor.
temporal40ee5512008-07-10 02:12:20 +0000127 // Most field types don't need this, so the default implementation is empty.
Austin Schuhec71d5d2014-10-31 19:15:54 -0700128 virtual void GenerateDestructorCode(io::Printer* /*printer*/) const {}
temporal40ee5512008-07-10 02:12:20 +0000129
Feng Xiao6ef984a2014-11-10 17:34:54 -0800130 // Generate a manual destructor invocation for use when the message is on an
131 // arena. The code that this method generates will be executed inside a
132 // shared-for-the-whole-message-class method registered with OwnDestructor().
133 // The method should return |true| if it generated any code that requires a
134 // call; this allows the message generator to eliminate the OwnDestructor()
135 // registration if no fields require it.
136 virtual bool GenerateArenaDestructorCode(io::Printer* printer) const {
137 return false;
138 }
139
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000140 // Generate code that allocates the fields's default instance.
Austin Schuhec71d5d2014-10-31 19:15:54 -0700141 virtual void GenerateDefaultInstanceAllocator(io::Printer* /*printer*/)
142 const {}
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000143
144 // Generate code that should be run when ShutdownProtobufLibrary() is called,
145 // to delete all dynamically-allocated objects.
Austin Schuhec71d5d2014-10-31 19:15:54 -0700146 virtual void GenerateShutdownCode(io::Printer* /*printer*/) const {}
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000147
temporal40ee5512008-07-10 02:12:20 +0000148 // Generate lines to decode this field, which will be placed inside the
149 // message's MergeFromCodedStream() method.
150 virtual void GenerateMergeFromCodedStream(io::Printer* printer) const = 0;
151
kenton@google.comfccb1462009-12-18 02:11:36 +0000152 // Generate lines to decode this field from a packed value, which will be
153 // placed inside the message's MergeFromCodedStream() method.
154 virtual void GenerateMergeFromCodedStreamWithPacking(io::Printer* printer)
155 const;
156
temporal40ee5512008-07-10 02:12:20 +0000157 // Generate lines to serialize this field, which are placed within the
158 // message's SerializeWithCachedSizes() method.
159 virtual void GenerateSerializeWithCachedSizes(io::Printer* printer) const = 0;
160
kenton@google.comd37d46d2009-04-25 02:53:47 +0000161 // Generate lines to serialize this field directly to the array "target",
162 // which are placed within the message's SerializeWithCachedSizesToArray()
163 // method. This must also advance "target" past the written bytes.
164 virtual void GenerateSerializeWithCachedSizesToArray(
165 io::Printer* printer) const = 0;
166
temporal40ee5512008-07-10 02:12:20 +0000167 // Generate lines to compute the serialized size of this field, which
168 // are placed in the message's ByteSize() method.
169 virtual void GenerateByteSize(io::Printer* printer) const = 0;
170
171 private:
172 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator);
173};
174
175// Convenience class which constructs FieldGenerators for a Descriptor.
176class FieldGeneratorMap {
177 public:
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000178 explicit FieldGeneratorMap(const Descriptor* descriptor, const Options& options);
temporal40ee5512008-07-10 02:12:20 +0000179 ~FieldGeneratorMap();
180
181 const FieldGenerator& get(const FieldDescriptor* field) const;
182
183 private:
184 const Descriptor* descriptor_;
Feng Xiaof157a562014-11-14 11:50:31 -0800185 google::protobuf::scoped_array<google::protobuf::scoped_ptr<FieldGenerator> > field_generators_;
temporal40ee5512008-07-10 02:12:20 +0000186
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000187 static FieldGenerator* MakeGenerator(const FieldDescriptor* field,
188 const Options& options);
temporal40ee5512008-07-10 02:12:20 +0000189
190 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap);
191};
192
kenton@google.comfccb1462009-12-18 02:11:36 +0000193
temporal40ee5512008-07-10 02:12:20 +0000194} // namespace cpp
195} // namespace compiler
196} // namespace protobuf
197
198} // namespace google
199#endif // GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__