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_CPP_FIELD_H__
36#define GOOGLE_PROTOBUF_COMPILER_CPP_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/descriptor.h>
46#include <google/protobuf/compiler/cpp/cpp_options.h>
47
48namespace google {
49namespace protobuf {
50  namespace io {
51    class Printer;             // printer.h
52  }
53}
54
55namespace protobuf {
56namespace compiler {
57namespace cpp {
58
59// 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,
64                             map<string, string>* variables,
65                             const Options& options);
66
67void SetCommonOneofFieldVariables(const FieldDescriptor* descriptor,
68                                  map<string, string>* variables);
69
70class FieldGenerator {
71 public:
72  explicit FieldGenerator(const Options& options) : options_(options) {}
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
80  // 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.
83  virtual void GenerateStaticMembers(io::Printer* /*printer*/) const {}
84
85  // Generate prototypes for accessors that will manipulate imported
86  // messages inline.  These are for .proto.h headers.
87  //
88  // In .proto.h mode, the headers of imports are not #included. However,
89  // functions that manipulate the imported message types need access to
90  // the class definition of the imported message, meaning that the headers
91  // must be #included. To get around this, functions that manipulate
92  // imported message objects are defined as dependent functions in a base
93  // template class. By making them dependent template functions, the
94  // function templates will not be instantiated until they are called, so
95  // we can defer to those translation units to #include the necessary
96  // generated headers.
97  //
98  // See:
99  // http://en.cppreference.com/w/cpp/language/class_template#Implicit_instantiation
100  //
101  // Most field types don't need this, so the default implementation is empty.
102  virtual void GenerateDependentAccessorDeclarations(
103      io::Printer* printer) const {}
104
105  // Generate prototypes for all of the accessor functions related to this
106  // field.  These are placed inside the class definition.
107  virtual void GenerateAccessorDeclarations(io::Printer* printer) const = 0;
108
109  // Generate inline definitions of depenent accessor functions for this field.
110  // These are placed inside the header after all class definitions.
111  virtual void GenerateDependentInlineAccessorDefinitions(
112    io::Printer* printer) const {}
113
114  // Generate inline definitions of accessor functions for this field.
115  // These are placed inside the header after all class definitions.
116  // In non-.proto.h mode, this generates dependent accessor functions as well.
117  virtual void GenerateInlineAccessorDefinitions(
118    io::Printer* printer, bool is_inline) const = 0;
119
120  // Generate definitions of accessors that aren't inlined.  These are
121  // placed somewhere in the .cc file.
122  // Most field types don't need this, so the default implementation is empty.
123  virtual void GenerateNonInlineAccessorDefinitions(
124    io::Printer* /*printer*/) const {}
125
126  // Generate lines of code (statements, not declarations) which clear the
127  // field.  This is used to define the clear_$name$() method as well as
128  // the Clear() method for the whole message.
129  virtual void GenerateClearingCode(io::Printer* printer) const = 0;
130
131  // Generate lines of code (statements, not declarations) which merges the
132  // contents of the field from the current message to the target message,
133  // which is stored in the generated code variable "from".
134  // This is used to fill in the MergeFrom method for the whole message.
135  // Details of this usage can be found in message.cc under the
136  // GenerateMergeFrom method.
137  virtual void GenerateMergingCode(io::Printer* printer) const = 0;
138
139  // Generate lines of code (statements, not declarations) which swaps
140  // this field and the corresponding field of another message, which
141  // is stored in the generated code variable "other". This is used to
142  // define the Swap method. Details of usage can be found in
143  // message.cc under the GenerateSwap method.
144  virtual void GenerateSwappingCode(io::Printer* printer) const = 0;
145
146  // Generate initialization code for private members declared by
147  // GeneratePrivateMembers(). These go into the message class's SharedCtor()
148  // method, invoked by each of the generated constructors.
149  virtual void GenerateConstructorCode(io::Printer* printer) const = 0;
150
151  // Generate any code that needs to go in the class's SharedDtor() method,
152  // invoked by the destructor.
153  // Most field types don't need this, so the default implementation is empty.
154  virtual void GenerateDestructorCode(io::Printer* /*printer*/) const {}
155
156  // Generate a manual destructor invocation for use when the message is on an
157  // arena. The code that this method generates will be executed inside a
158  // shared-for-the-whole-message-class method registered with OwnDestructor().
159  // The method should return |true| if it generated any code that requires a
160  // call; this allows the message generator to eliminate the OwnDestructor()
161  // registration if no fields require it.
162  virtual bool GenerateArenaDestructorCode(io::Printer* printer) const {
163    return false;
164  }
165
166  // Generate code that allocates the fields's default instance.
167  virtual void GenerateDefaultInstanceAllocator(io::Printer* /*printer*/)
168      const {}
169
170  // Generate code that should be run when ShutdownProtobufLibrary() is called,
171  // to delete all dynamically-allocated objects.
172  virtual void GenerateShutdownCode(io::Printer* /*printer*/) const {}
173
174  // Generate lines to decode this field, which will be placed inside the
175  // message's MergeFromCodedStream() method.
176  virtual void GenerateMergeFromCodedStream(io::Printer* printer) const = 0;
177
178  // Generate lines to decode this field from a packed value, which will be
179  // placed inside the message's MergeFromCodedStream() method.
180  virtual void GenerateMergeFromCodedStreamWithPacking(io::Printer* printer)
181      const;
182
183  // Generate lines to serialize this field, which are placed within the
184  // message's SerializeWithCachedSizes() method.
185  virtual void GenerateSerializeWithCachedSizes(io::Printer* printer) const = 0;
186
187  // Generate lines to serialize this field directly to the array "target",
188  // which are placed within the message's SerializeWithCachedSizesToArray()
189  // method. This must also advance "target" past the written bytes.
190  virtual void GenerateSerializeWithCachedSizesToArray(
191      io::Printer* printer) const = 0;
192
193  // Generate lines to compute the serialized size of this field, which
194  // are placed in the message's ByteSize() method.
195  virtual void GenerateByteSize(io::Printer* printer) const = 0;
196
197 protected:
198  const Options& options_;
199
200 private:
201  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator);
202};
203
204// Convenience class which constructs FieldGenerators for a Descriptor.
205class FieldGeneratorMap {
206 public:
207  FieldGeneratorMap(const Descriptor* descriptor, const Options& options);
208  ~FieldGeneratorMap();
209
210  const FieldGenerator& get(const FieldDescriptor* field) const;
211
212 private:
213  const Descriptor* descriptor_;
214  const Options& options_;
215  google::protobuf::scoped_array<google::protobuf::scoped_ptr<FieldGenerator> > field_generators_;
216
217  static FieldGenerator* MakeGenerator(const FieldDescriptor* field,
218                                       const Options& options);
219
220  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap);
221};
222
223
224}  // namespace cpp
225}  // namespace compiler
226}  // namespace protobuf
227
228}  // namespace google
229#endif  // GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
230