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 #include <string>
41 
45 
46 namespace google {
47 namespace protobuf {
48 namespace io {
49 class Printer; // printer.h
50 }
51 } // namespace protobuf
52 } // namespace google
53 
54 namespace google {
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  std::map<std::string, std::string>* variables,
65  const Options& options);
66 
69  std::map<std::string, std::string>* variables);
70 
72  public:
74  const Options& options)
76  virtual ~FieldGenerator();
77 
78  // Generate lines of code declaring members fields of the message class
79  // needed to represent this field. These are placed inside the message
80  // class.
81  virtual void GeneratePrivateMembers(io::Printer* printer) const = 0;
82 
83  // Generate static default variable for this field. These are placed inside
84  // the message class. Most field types don't need this, so the default
85  // implementation is empty.
86  virtual void GenerateStaticMembers(io::Printer* /*printer*/) const {}
87 
88  // Generate prototypes for all of the accessor functions related to this
89  // field. These are placed inside the class definition.
90  virtual void GenerateAccessorDeclarations(io::Printer* printer) const = 0;
91 
92  // Generate inline definitions of accessor functions for this field.
93  // These are placed inside the header after all class definitions.
95  io::Printer* printer) const = 0;
96 
97  // Generate definitions of accessors that aren't inlined. These are
98  // placed somewhere in the .cc file.
99  // Most field types don't need this, so the default implementation is empty.
101  io::Printer* /*printer*/) const {}
102 
103  // Generate declarations of accessors that are for internal purposes only.
104  // Most field types don't need this, so the default implementation is empty.
106  io::Printer* /*printer*/) const {}
107 
108  // Generate definitions of accessors that are for internal purposes only.
109  // Most field types don't need this, so the default implementation is empty.
111  io::Printer* /*printer*/) const {}
112 
113  // Generate lines of code (statements, not declarations) which clear the
114  // field. This is used to define the clear_$name$() method
115  virtual void GenerateClearingCode(io::Printer* printer) const = 0;
116 
117  // Generate lines of code (statements, not declarations) which clear the
118  // field as part of the Clear() method for the whole message. For message
119  // types which have field presence bits, MessageGenerator::GenerateClear
120  // will have already checked the presence bits.
121  //
122  // Since most field types can re-use GenerateClearingCode, this method is
123  // not pure virtual.
124  virtual void GenerateMessageClearingCode(io::Printer* printer) const {
125  GenerateClearingCode(printer);
126  }
127 
128  // Generate lines of code (statements, not declarations) which merges the
129  // contents of the field from the current message to the target message,
130  // which is stored in the generated code variable "from".
131  // This is used to fill in the MergeFrom method for the whole message.
132  // Details of this usage can be found in message.cc under the
133  // GenerateMergeFrom method.
134  virtual void GenerateMergingCode(io::Printer* printer) const = 0;
135 
136  // Generates a copy constructor
137  virtual void GenerateCopyConstructorCode(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
159  // OwnDestructor(). The method should return |true| if it generated any code
160  // that requires a call; this allows the message generator to eliminate the
161  // OwnDestructor() 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  io::Printer* /*printer*/) const {}
169 
170  // Generate lines to decode this field, which will be placed inside the
171  // message's MergeFromCodedStream() method.
172  virtual void GenerateMergeFromCodedStream(io::Printer* printer) const = 0;
173 
174  // Returns true if this field's "MergeFromCodedStream" code needs the arena
175  // to be defined as a variable.
176  virtual bool MergeFromCodedStreamNeedsArena() const { return false; }
177 
178  // Generate lines to decode this field from a packed value, which will be
179  // placed inside the message's MergeFromCodedStream() method.
181  io::Printer* printer) 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.
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  // Any tags about field layout decisions (such as inlining) to embed in the
198  // offset.
199  virtual uint32 CalculateFieldTag() const { return 0; }
200  virtual bool IsInlined() const { return false; }
201 
202  void SetHasBitIndex(int32 has_bit_index);
203 
204  protected:
207  std::map<std::string, std::string> variables_;
208 
209  private:
211 };
212 
213 // Convenience class which constructs FieldGenerators for a Descriptor.
215  public:
217  MessageSCCAnalyzer* scc_analyzer);
219 
220  const FieldGenerator& get(const FieldDescriptor* field) const;
221 
222  void SetHasBitIndices(const std::vector<int>& has_bit_indices_) {
223  for (int i = 0; i < descriptor_->field_count(); ++i) {
224  field_generators_[i]->SetHasBitIndex(has_bit_indices_[i]);
225  }
226  }
227 
228  private:
230  std::vector<std::unique_ptr<FieldGenerator>> field_generators_;
231 
233  const FieldDescriptor* field, const Options& options,
234  MessageSCCAnalyzer* scc_analyzer);
236  const Options& options,
237  MessageSCCAnalyzer* scc_analyzer);
238 
240 };
241 
242 } // namespace cpp
243 } // namespace compiler
244 } // namespace protobuf
245 } // namespace google
246 
247 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
google::protobuf::compiler::cpp::FieldGenerator::GenerateByteSize
virtual void GenerateByteSize(io::Printer *printer) const =0
google::protobuf::FieldDescriptor
Definition: src/google/protobuf/descriptor.h:515
google::protobuf::compiler::cpp::FieldGenerator::GenerateInternalAccessorDefinitions
virtual void GenerateInternalAccessorDefinitions(io::Printer *) const
Definition: cpp_field.h:105
google::protobuf::compiler::cpp::FieldGenerator::GenerateSwappingCode
virtual void GenerateSwappingCode(io::Printer *printer) const =0
google::protobuf::compiler::cpp::FieldGenerator::SetHasBitIndex
void SetHasBitIndex(int32 has_bit_index)
Definition: cpp_field.cc:93
google::protobuf::compiler::cpp::FieldGeneratorMap::MakeGenerator
static FieldGenerator * MakeGenerator(const FieldDescriptor *field, const Options &options, MessageSCCAnalyzer *scc_analyzer)
Definition: cpp_field.cc:145
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
google::protobuf::compiler::cpp::SetCommonFieldVariables
void SetCommonFieldVariables(const FieldDescriptor *descriptor, std::map< std::string, std::string > *variables, const Options &options)
Definition: cpp_field.cc:59
google::protobuf::compiler::cpp::FieldGeneratorMap::descriptor_
const Descriptor * descriptor_
Definition: cpp_field.h:229
google::protobuf::compiler::cpp::FieldGenerator::GenerateDestructorCode
virtual void GenerateDestructorCode(io::Printer *) const
Definition: cpp_field.h:154
google::protobuf::uint32
uint32_t uint32
Definition: protobuf/src/google/protobuf/stubs/port.h:155
google::protobuf::compiler::cpp::FieldGenerator::CalculateFieldTag
virtual uint32 CalculateFieldTag() const
Definition: cpp_field.h:199
google::protobuf::compiler::cpp::FieldGenerator::options_
const Options & options_
Definition: cpp_field.h:206
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::compiler::cpp::FieldGenerator::GenerateStaticMembers
virtual void GenerateStaticMembers(io::Printer *) const
Definition: cpp_field.h:86
google::protobuf::compiler::cpp::FieldGenerator::descriptor_
const FieldDescriptor * descriptor_
Definition: cpp_field.h:205
google::protobuf::compiler::cpp::FieldGeneratorMap::MakeGoogleInternalGenerator
static FieldGenerator * MakeGoogleInternalGenerator(const FieldDescriptor *field, const Options &options, MessageSCCAnalyzer *scc_analyzer)
Definition: cpp_field.cc:138
cpp_helpers.h
google::protobuf::compiler::cpp::FieldGenerator::variables_
std::map< std::string, std::string > variables_
Definition: cpp_field.h:207
google::protobuf::compiler::cpp::FieldGenerator::GenerateNonInlineAccessorDefinitions
virtual void GenerateNonInlineAccessorDefinitions(io::Printer *) const
Definition: cpp_field.h:100
google::protobuf::compiler::cpp::FieldGenerator::GenerateMergeFromCodedStreamWithPacking
virtual void GenerateMergeFromCodedStreamWithPacking(io::Printer *printer) const
Definition: cpp_field.cc:116
FieldDescriptor
Definition: ruby/ext/google/protobuf_c/protobuf.h:129
google::protobuf::compiler::cpp::FieldGenerator::GenerateAccessorDeclarations
virtual void GenerateAccessorDeclarations(io::Printer *printer) const =0
google::protobuf::compiler::cpp::FieldGenerator::IsInlined
virtual bool IsInlined() const
Definition: cpp_field.h:200
google::protobuf::compiler::cpp::FieldGeneratorMap::~FieldGeneratorMap
~FieldGeneratorMap()
Definition: cpp_field.cc:195
google::protobuf::int32
int32_t int32
Definition: protobuf/src/google/protobuf/stubs/port.h:150
google::protobuf::compiler::cpp::FieldGenerator::GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator)
google::protobuf::compiler::cpp::FieldGenerator
Definition: cpp_field.h:71
google::protobuf::compiler::cpp::FieldGenerator::GenerateMergeFromCodedStream
virtual void GenerateMergeFromCodedStream(io::Printer *printer) const =0
google::protobuf::compiler::cpp::FieldGeneratorMap::get
const FieldGenerator & get(const FieldDescriptor *field) const
Definition: cpp_field.cc:197
google::protobuf::compiler::cpp::FieldGeneratorMap::FieldGeneratorMap
FieldGeneratorMap(const Descriptor *descriptor, const Options &options, MessageSCCAnalyzer *scc_analyzer)
Definition: cpp_field.cc:127
google::protobuf::compiler::cpp::FieldGeneratorMap
Definition: cpp_field.h:214
google::protobuf::compiler::cpp::FieldGenerator::GenerateCopyConstructorCode
virtual void GenerateCopyConstructorCode(io::Printer *printer) const =0
google::protobuf::compiler::cpp::FieldGenerator::GeneratePrivateMembers
virtual void GeneratePrivateMembers(io::Printer *printer) const =0
cpp
Definition: third_party/googletest/googlemock/scripts/generator/cpp/__init__.py:1
google::protobuf::Descriptor::field_count
int field_count() const
google::protobuf::compiler::cpp::Options
Definition: cpp_options.h:52
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
google::protobuf::compiler::cpp::FieldGenerator::GenerateMessageClearingCode
virtual void GenerateMessageClearingCode(io::Printer *printer) const
Definition: cpp_field.h:124
google::protobuf::io::Printer
Definition: printer.h:181
google::protobuf::compiler::cpp::FieldGenerator::GenerateConstructorCode
virtual void GenerateConstructorCode(io::Printer *printer) const =0
google::protobuf::compiler::cpp::FieldGeneratorMap::field_generators_
std::vector< std::unique_ptr< FieldGenerator > > field_generators_
Definition: cpp_field.h:230
google::protobuf::compiler::cpp::FieldGenerator::GenerateMergingCode
virtual void GenerateMergingCode(io::Printer *printer) const =0
i
int i
Definition: gmock-matchers_test.cc:764
has_bit_indices_
const std::vector< int > & has_bit_indices_
Definition: cpp_message.cc:399
google::protobuf::compiler::cpp::FieldGeneratorMap::GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap)
google::protobuf::compiler::cpp::MessageSCCAnalyzer
Definition: cpp_helpers.h:524
google::protobuf::compiler::cpp::FieldGenerator::~FieldGenerator
virtual ~FieldGenerator()
Definition: cpp_field.cc:114
google::protobuf::compiler::cpp::FieldGenerator::GenerateDefaultInstanceAllocator
virtual void GenerateDefaultInstanceAllocator(io::Printer *) const
Definition: cpp_field.h:167
google::protobuf::compiler::cpp::FieldGenerator::GenerateArenaDestructorCode
virtual bool GenerateArenaDestructorCode(io::Printer *printer) const
Definition: cpp_field.h:162
google::protobuf::Descriptor
Definition: src/google/protobuf/descriptor.h:231
descriptor.h
google::protobuf::compiler::cpp::FieldGenerator::GenerateInlineAccessorDefinitions
virtual void GenerateInlineAccessorDefinitions(io::Printer *printer) const =0
google::protobuf::compiler::cpp::FieldGenerator::GenerateSerializeWithCachedSizesToArray
virtual void GenerateSerializeWithCachedSizesToArray(io::Printer *printer) const =0
google::protobuf::compiler::cpp::FieldGenerator::MergeFromCodedStreamNeedsArena
virtual bool MergeFromCodedStreamNeedsArena() const
Definition: cpp_field.h:176
cpp_options.h
google::protobuf::compiler::cpp::FieldGenerator::FieldGenerator
FieldGenerator(const FieldDescriptor *descriptor, const Options &options)
Definition: cpp_field.h:73
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::compiler::cpp::FieldGenerator::GenerateInternalAccessorDeclarations
virtual void GenerateInternalAccessorDeclarations(io::Printer *) const
Definition: cpp_field.h:110
google::protobuf::compiler::cpp::FieldGenerator::GenerateClearingCode
virtual void GenerateClearingCode(io::Printer *printer) const =0
google::protobuf::compiler::cpp::FieldGeneratorMap::SetHasBitIndices
void SetHasBitIndices(const std::vector< int > &has_bit_indices_)
Definition: cpp_field.h:222
google::protobuf::compiler::cpp::FieldGenerator::GenerateSerializeWithCachedSizes
virtual void GenerateSerializeWithCachedSizes(io::Printer *printer) const =0
google::protobuf::compiler::cpp::SetCommonOneofFieldVariables
void SetCommonOneofFieldVariables(const FieldDescriptor *descriptor, std::map< std::string, std::string > *variables)
Definition: cpp_field.cc:105


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:48