protobuf/src/google/protobuf/compiler/cpp/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 <cstdint>
39 #include <map>
40 #include <memory>
41 #include <string>
42 
43 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
44 #include <google/protobuf/compiler/cpp/cpp_options.h>
45 #include <google/protobuf/descriptor.h>
46 
47 namespace google {
48 namespace protobuf {
49 namespace io {
50 class Printer; // printer.h
51 }
52 } // namespace protobuf
53 } // namespace google
54 
55 namespace google {
56 namespace protobuf {
57 namespace compiler {
58 namespace cpp {
59 
60 // Helper function: set variables in the map that are the same for all
61 // field code generators.
62 // ['name', 'index', 'number', 'classname', 'declared_type', 'tag_size',
63 // 'deprecation'].
65  std::map<std::string, std::string>* variables,
66  const Options& options);
67 
70  std::map<std::string, std::string>* variables);
71 
72 class FieldGenerator {
73  public:
75  const Options& options)
77  virtual ~FieldGenerator();
79  io::Printer* printer) const final{};
80  // Generate lines of code declaring members fields of the message class
81  // needed to represent this field. These are placed inside the message
82  // class.
83  virtual void GeneratePrivateMembers(io::Printer* printer) const = 0;
84 
85  // Generate static default variable for this field. These are placed inside
86  // the message class. Most field types don't need this, so the default
87  // implementation is empty.
88  virtual void GenerateStaticMembers(io::Printer* /*printer*/) const {}
89 
90  // Generate prototypes for all of the accessor functions related to this
91  // field. These are placed inside the class definition.
92  virtual void GenerateAccessorDeclarations(io::Printer* printer) const = 0;
93 
94  // Generate inline definitions of accessor functions for this field.
95  // These are placed inside the header after all class definitions.
97  io::Printer* printer) const = 0;
98 
99  // Generate definitions of accessors that aren't inlined. These are
100  // placed somewhere in the .cc file.
101  // Most field types don't need this, so the default implementation is empty.
103  io::Printer* /*printer*/) const {}
104 
105  // Generate declarations of accessors that are for internal purposes only.
106  // Most field types don't need this, so the default implementation is empty.
108  io::Printer* /*printer*/) const {}
109 
110  // Generate definitions of accessors that are for internal purposes only.
111  // Most field types don't need this, so the default implementation is empty.
113  io::Printer* /*printer*/) const {}
114 
115  // Generate lines of code (statements, not declarations) which clear the
116  // field. This is used to define the clear_$name$() method
117  virtual void GenerateClearingCode(io::Printer* printer) const = 0;
118 
119  // Generate lines of code (statements, not declarations) which clear the
120  // field as part of the Clear() method for the whole message. For message
121  // types which have field presence bits, MessageGenerator::GenerateClear
122  // will have already checked the presence bits.
123  //
124  // Since most field types can re-use GenerateClearingCode, this method is
125  // not pure virtual.
126  virtual void GenerateMessageClearingCode(io::Printer* printer) const {
127  GenerateClearingCode(printer);
128  }
129 
130  // Generate lines of code (statements, not declarations) which merges the
131  // contents of the field from the current message to the target message,
132  // which is stored in the generated code variable "from".
133  // This is used to fill in the MergeFrom method for the whole message.
134  // Details of this usage can be found in message.cc under the
135  // GenerateMergeFrom method.
136  virtual void GenerateMergingCode(io::Printer* printer) const = 0;
137 
138  // Generates a copy constructor
139  virtual void GenerateCopyConstructorCode(io::Printer* printer) const = 0;
140 
141  // Generate lines of code (statements, not declarations) which swaps
142  // this field and the corresponding field of another message, which
143  // is stored in the generated code variable "other". This is used to
144  // define the Swap method. Details of usage can be found in
145  // message.cc under the GenerateSwap method.
146  virtual void GenerateSwappingCode(io::Printer* printer) const = 0;
147 
148  // Generate initialization code for private members declared by
149  // GeneratePrivateMembers(). These go into the message class's SharedCtor()
150  // method, invoked by each of the generated constructors.
151  virtual void GenerateConstructorCode(io::Printer* printer) const = 0;
152 
153  // Generate any code that needs to go in the class's SharedDtor() method,
154  // invoked by the destructor.
155  // Most field types don't need this, so the default implementation is empty.
156  virtual void GenerateDestructorCode(io::Printer* /*printer*/) const {}
157 
158  // Generate a manual destructor invocation for use when the message is on an
159  // arena. The code that this method generates will be executed inside a
160  // shared-for-the-whole-message-class method registered with
161  // OwnDestructor(). The method should return |true| if it generated any code
162  // that requires a call; this allows the message generator to eliminate the
163  // OwnDestructor() registration if no fields require it.
164  virtual bool GenerateArenaDestructorCode(io::Printer* printer) const {
165  return false;
166  }
167 
168  // Generate initialization code for private members declared by
169  // GeneratePrivateMembers(), specifically for the constexpr constructor.
170  // These go into the constructor's initializer list and must follow that
171  // syntax (eg `field_(args)`). Does not include `:` or `,` separators.
172  virtual void GenerateConstinitInitializer(io::Printer* printer) const {}
173 
174  // Generate lines to serialize this field directly to the array "target",
175  // which are placed within the message's SerializeWithCachedSizesToArray()
176  // method. This must also advance "target" past the written bytes.
178  io::Printer* printer) const = 0;
179 
180  // Generate lines to compute the serialized size of this field, which
181  // are placed in the message's ByteSize() method.
182  virtual void GenerateByteSize(io::Printer* printer) const = 0;
183 
184  // Generates lines to call IsInitialized() for eligible message fields. Non
185  // message fields won't need to override this function.
186  virtual void GenerateIsInitialized(io::Printer* printer) const {}
187 
188  virtual bool IsInlined() const { return false; }
189 
190  void SetHasBitIndex(int32_t has_bit_index);
191  void SetInlinedStringIndex(int32_t inlined_string_index);
192 
193  protected:
195  const Options& options_;
196  std::map<std::string, std::string> variables_;
197 
198  private:
200 };
201 
202 // Convenience class which constructs FieldGenerators for a Descriptor.
203 class FieldGeneratorMap {
204  public:
205  FieldGeneratorMap(const Descriptor* descriptor, const Options& options,
206  MessageSCCAnalyzer* scc_analyzer);
208 
209  const FieldGenerator& get(const FieldDescriptor* field) const;
210 
211  void SetHasBitIndices(const std::vector<int>& has_bit_indices_) {
212  for (int i = 0; i < descriptor_->field_count(); ++i) {
213  field_generators_[i]->SetHasBitIndex(has_bit_indices_[i]);
214  }
215  }
216 
217  void SetInlinedStringIndices(const std::vector<int>& inlined_string_indices) {
218  for (int i = 0; i < descriptor_->field_count(); ++i) {
219  field_generators_[i]->SetInlinedStringIndex(inlined_string_indices[i]);
220  }
221  }
222 
223  private:
224  const Descriptor* descriptor_;
225  std::vector<std::unique_ptr<FieldGenerator>> field_generators_;
226 
228  const FieldDescriptor* field, const Options& options,
229  MessageSCCAnalyzer* scc_analyzer);
231  const Options& options,
232  MessageSCCAnalyzer* scc_analyzer);
233 
235 };
236 
237 } // namespace cpp
238 } // namespace compiler
239 } // namespace protobuf
240 } // namespace google
241 
242 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
google::protobuf::compiler::cpp::FieldGenerator::GenerateIsInitialized
virtual void GenerateIsInitialized(io::Printer *printer) const
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:186
google::protobuf::compiler::cpp::FieldGenerator::GenerateByteSize
virtual void GenerateByteSize(io::Printer *printer) const =0
has_bit_indices_
const std::vector< int > & has_bit_indices_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_message.cc:397
google::protobuf::FieldDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:515
google::protobuf::compiler::cpp::FieldGenerator::variables_
std::map< std::string, std::string > variables_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:191
google::protobuf::compiler::cpp::FieldGenerator::descriptor_
const FieldDescriptor * descriptor_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:189
google::protobuf::compiler::cpp::FieldGenerator::GenerateInternalAccessorDefinitions
virtual void GenerateInternalAccessorDefinitions(io::Printer *) const
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:107
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: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_field.cc:93
google::protobuf::compiler::cpp::FieldGeneratorMap::MakeGenerator
static FieldGenerator * MakeGenerator(const FieldDescriptor *field, const Options &options, MessageSCCAnalyzer *scc_analyzer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_field.cc:134
options
double_dict options[]
Definition: capstone_test.c:55
google::protobuf::compiler::cpp::SetCommonFieldVariables
void SetCommonFieldVariables(const FieldDescriptor *descriptor, std::map< std::string, std::string > *variables, const Options &options)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_field.cc:58
google::protobuf::compiler::cpp::FieldGenerator::GenerateDestructorCode
virtual void GenerateDestructorCode(io::Printer *) const
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:156
google::protobuf::compiler::cpp::FieldGenerator::SetInlinedStringIndex
void SetInlinedStringIndex(int32_t inlined_string_index)
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_field.cc:285
grpc::protobuf::io::Printer
GRPC_CUSTOM_PRINTER Printer
Definition: src/compiler/config.h:54
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf::compiler::cpp::FieldGenerator::GenerateSerializeWithCachedSizes
virtual void GenerateSerializeWithCachedSizes(io::Printer *printer) const final
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:78
google::protobuf::compiler::cpp::FieldGenerator::GenerateStaticMembers
virtual void GenerateStaticMembers(io::Printer *) const
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:88
google::protobuf::compiler::cpp::FieldGeneratorMap::MakeGoogleInternalGenerator
static FieldGenerator * MakeGoogleInternalGenerator(const FieldDescriptor *field, const Options &options, MessageSCCAnalyzer *scc_analyzer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_field.cc:127
google::protobuf::compiler::cpp::FieldGenerator::GenerateNonInlineAccessorDefinitions
virtual void GenerateNonInlineAccessorDefinitions(io::Printer *) const
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:102
FieldDescriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:133
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: protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:188
google::protobuf::compiler::cpp::FieldGeneratorMap::~FieldGeneratorMap
~FieldGeneratorMap()
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_field.cc:184
google::protobuf::compiler::cpp::FieldGeneratorMap::SetInlinedStringIndices
void SetInlinedStringIndices(const std::vector< int > &inlined_string_indices)
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:217
google::protobuf::compiler::cpp::FieldGenerator::GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator)
google::protobuf::compiler::cpp::FieldGenerator
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:71
google::protobuf::compiler::cpp::FieldGeneratorMap::get
const FieldGenerator & get(const FieldDescriptor *field) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_field.cc:186
google::protobuf::compiler::cpp::FieldGeneratorMap::FieldGeneratorMap
FieldGeneratorMap(const Descriptor *descriptor, const Options &options, MessageSCCAnalyzer *scc_analyzer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_field.cc:116
google::protobuf::compiler::cpp::FieldGeneratorMap
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:198
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
io
cpp
Definition: third_party/bloaty/third_party/googletest/googlemock/scripts/generator/cpp/__init__.py:1
google::protobuf::Descriptor::field_count
int field_count() const
google::protobuf::compiler::cpp::FieldGeneratorMap::field_generators_
std::vector< std::unique_ptr< FieldGenerator > > field_generators_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:214
google::protobuf::compiler::cpp::Options
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_options.h:52
google::protobuf::compiler::cpp::FieldGenerator::GenerateMessageClearingCode
virtual void GenerateMessageClearingCode(io::Printer *printer) const
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:126
google::protobuf::io::Printer
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/printer.h:181
google::protobuf::compiler::cpp::FieldGenerator::GenerateConstructorCode
virtual void GenerateConstructorCode(io::Printer *printer) const =0
google::protobuf::compiler::cpp::FieldGenerator::GenerateMergingCode
virtual void GenerateMergingCode(io::Printer *printer) const =0
google::protobuf::compiler::cpp::FieldGeneratorMap::descriptor_
const Descriptor * descriptor_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:213
field
const FieldDescriptor * field
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/parser_unittest.cc:2692
google::protobuf::compiler::cpp::FieldGenerator::GenerateConstinitInitializer
virtual void GenerateConstinitInitializer(io::Printer *printer) const
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:172
google::protobuf::compiler::cpp::FieldGeneratorMap::GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap)
google::protobuf::compiler::cpp::MessageSCCAnalyzer
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.h:519
google::protobuf::compiler::cpp::FieldGenerator::~FieldGenerator
virtual ~FieldGenerator()
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_field.cc:114
google::protobuf::compiler::cpp::FieldGenerator::GenerateArenaDestructorCode
virtual bool GenerateArenaDestructorCode(io::Printer *printer) const
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:164
google::protobuf::Descriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:231
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
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
google::protobuf::compiler::cpp::FieldGenerator::options_
const Options & options_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:190
descriptor
static const char descriptor[1336]
Definition: certs.upbdefs.c:16
google::protobuf::compiler::cpp::FieldGenerator::FieldGenerator
FieldGenerator(const FieldDescriptor *descriptor, const Options &options)
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:74
compiler
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/plugin.pb.cc:21
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
google::protobuf::compiler::cpp::FieldGenerator::GenerateInternalAccessorDeclarations
virtual void GenerateInternalAccessorDeclarations(io::Printer *) const
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:112
google::protobuf::compiler::cpp::FieldGenerator::GenerateClearingCode
virtual void GenerateClearingCode(io::Printer *printer) const =0
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
google::protobuf::compiler::cpp::FieldGeneratorMap::SetHasBitIndices
void SetHasBitIndices(const std::vector< int > &has_bit_indices_)
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_field.h:211
google::protobuf::compiler::cpp::SetCommonOneofFieldVariables
void SetCommonOneofFieldVariables(const FieldDescriptor *descriptor, std::map< std::string, std::string > *variables)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_field.cc:105


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:04