webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
cpp_field.h
Go to the documentation of this file.
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
42 #endif
43 #include <string>
44 
47 
48 namespace google {
49 namespace protobuf {
50  namespace io {
51  class Printer; // printer.h
52  }
53 }
54 
55 namespace protobuf {
56 namespace compiler {
57 namespace 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'].
64  map<string, string>* variables,
65  const Options& options);
66 
68  map<string, string>* variables);
69 
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.
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.
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.
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.
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:
199 
200  private:
202 };
203 
204 // Convenience class which constructs FieldGenerators for a Descriptor.
206  public:
209 
210  const FieldGenerator& get(const FieldDescriptor* field) const;
211 
212  private:
213  const Descriptor* descriptor_;
214  const Options& options_;
216 
217  static FieldGenerator* MakeGenerator(const FieldDescriptor* field,
218  const Options& options);
219 
221 };
222 
223 
224 } // namespace cpp
225 } // namespace compiler
226 } // namespace protobuf
227 
228 } // namespace google
229 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
Definition: upb.c:6604
FieldGenerator(const Options &options)
Definition: cpp_field.h:72
virtual void GenerateNonInlineAccessorDefinitions(io::Printer *) const
Definition: cpp_field.h:123
Definition: printer.h:164
const FieldDescriptor * field
Definition: parser_unittest.cc:2279
Definition: cpp_options.h:45
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
Definition: macros.h:40
options
Definition: CodeGeneratorReplayInputs.py:1046
const Descriptor * descriptor_
Definition: field_comparator_test.cc:58
const Descriptor * descriptor
Definition: descriptor.cc:271
const Options & options_
Definition: cpp_field.h:198
Definition: descriptor.h:172
Definition: descriptor.h:439
Definition: protobuf.h:85
virtual void GenerateShutdownCode(io::Printer *) const
Definition: cpp_field.h:172
Definition: scoped_ptr.h:49
Definition: __init__.py:1
virtual void GenerateDependentInlineAccessorDefinitions(io::Printer *printer) const
Definition: cpp_field.h:111
virtual void GenerateDefaultInstanceAllocator(io::Printer *) const
Definition: cpp_field.h:167
virtual void GenerateStaticMembers(io::Printer *) const
Definition: cpp_field.h:83
void SetCommonOneofFieldVariables(const FieldDescriptor *descriptor, map< string, string > *variables)
Definition: cpp_field.cc:100
void SetCommonFieldVariables(const FieldDescriptor *descriptor, map< string, string > *variables, const Options &options)
Definition: cpp_field.cc:61
virtual void GenerateDestructorCode(io::Printer *) const
Definition: cpp_field.h:154
virtual bool GenerateArenaDestructorCode(io::Printer *printer) const
Definition: cpp_field.h:162
Definition: gflags_completions.h:115
virtual void GenerateDependentAccessorDeclarations(io::Printer *printer) const
Definition: cpp_field.h:102