cpp_field.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
  34. #define GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
  35. #include <map>
  36. #include <memory>
  37. #include <string>
  38. #include <google/protobuf/compiler/cpp/cpp_helpers.h>
  39. #include <google/protobuf/compiler/cpp/cpp_options.h>
  40. #include <google/protobuf/descriptor.h>
  41. namespace google {
  42. namespace protobuf {
  43. namespace io {
  44. class Printer; // printer.h
  45. }
  46. } // namespace protobuf
  47. } // namespace google
  48. namespace google {
  49. namespace protobuf {
  50. namespace compiler {
  51. namespace cpp {
  52. // Helper function: set variables in the map that are the same for all
  53. // field code generators.
  54. // ['name', 'index', 'number', 'classname', 'declared_type', 'tag_size',
  55. // 'deprecation'].
  56. void SetCommonFieldVariables(const FieldDescriptor* descriptor,
  57. std::map<std::string, std::string>* variables,
  58. const Options& options);
  59. void SetCommonOneofFieldVariables(
  60. const FieldDescriptor* descriptor,
  61. std::map<std::string, std::string>* variables);
  62. class FieldGenerator {
  63. public:
  64. explicit FieldGenerator(const FieldDescriptor* descriptor,
  65. const Options& options)
  66. : descriptor_(descriptor), options_(options) {}
  67. virtual ~FieldGenerator();
  68. virtual void GenerateSerializeWithCachedSizes(
  69. io::Printer* printer) const final{};
  70. // Generate lines of code declaring members fields of the message class
  71. // needed to represent this field. These are placed inside the message
  72. // class.
  73. virtual void GeneratePrivateMembers(io::Printer* printer) const = 0;
  74. // Generate static default variable for this field. These are placed inside
  75. // the message class. Most field types don't need this, so the default
  76. // implementation is empty.
  77. virtual void GenerateStaticMembers(io::Printer* /*printer*/) const {}
  78. // Generate prototypes for all of the accessor functions related to this
  79. // field. These are placed inside the class definition.
  80. virtual void GenerateAccessorDeclarations(io::Printer* printer) const = 0;
  81. // Generate inline definitions of accessor functions for this field.
  82. // These are placed inside the header after all class definitions.
  83. virtual void GenerateInlineAccessorDefinitions(
  84. io::Printer* printer) const = 0;
  85. // Generate definitions of accessors that aren't inlined. These are
  86. // placed somewhere in the .cc file.
  87. // Most field types don't need this, so the default implementation is empty.
  88. virtual void GenerateNonInlineAccessorDefinitions(
  89. io::Printer* /*printer*/) const {}
  90. // Generate declarations of accessors that are for internal purposes only.
  91. // Most field types don't need this, so the default implementation is empty.
  92. virtual void GenerateInternalAccessorDefinitions(
  93. io::Printer* /*printer*/) const {}
  94. // Generate definitions of accessors that are for internal purposes only.
  95. // Most field types don't need this, so the default implementation is empty.
  96. virtual void GenerateInternalAccessorDeclarations(
  97. io::Printer* /*printer*/) const {}
  98. // Generate lines of code (statements, not declarations) which clear the
  99. // field. This is used to define the clear_$name$() method
  100. virtual void GenerateClearingCode(io::Printer* printer) const = 0;
  101. // Generate lines of code (statements, not declarations) which clear the
  102. // field as part of the Clear() method for the whole message. For message
  103. // types which have field presence bits, MessageGenerator::GenerateClear
  104. // will have already checked the presence bits.
  105. //
  106. // Since most field types can re-use GenerateClearingCode, this method is
  107. // not pure virtual.
  108. virtual void GenerateMessageClearingCode(io::Printer* printer) const {
  109. GenerateClearingCode(printer);
  110. }
  111. // Generate lines of code (statements, not declarations) which merges the
  112. // contents of the field from the current message to the target message,
  113. // which is stored in the generated code variable "from".
  114. // This is used to fill in the MergeFrom method for the whole message.
  115. // Details of this usage can be found in message.cc under the
  116. // GenerateMergeFrom method.
  117. virtual void GenerateMergingCode(io::Printer* printer) const = 0;
  118. // Generates a copy constructor
  119. virtual void GenerateCopyConstructorCode(io::Printer* printer) const = 0;
  120. // Generate lines of code (statements, not declarations) which swaps
  121. // this field and the corresponding field of another message, which
  122. // is stored in the generated code variable "other". This is used to
  123. // define the Swap method. Details of usage can be found in
  124. // message.cc under the GenerateSwap method.
  125. virtual void GenerateSwappingCode(io::Printer* printer) const = 0;
  126. // Generate initialization code for private members declared by
  127. // GeneratePrivateMembers(). These go into the message class's SharedCtor()
  128. // method, invoked by each of the generated constructors.
  129. virtual void GenerateConstructorCode(io::Printer* printer) const = 0;
  130. // Generate any code that needs to go in the class's SharedDtor() method,
  131. // invoked by the destructor.
  132. // Most field types don't need this, so the default implementation is empty.
  133. virtual void GenerateDestructorCode(io::Printer* /*printer*/) const {}
  134. // Generate a manual destructor invocation for use when the message is on an
  135. // arena. The code that this method generates will be executed inside a
  136. // shared-for-the-whole-message-class method registered with
  137. // OwnDestructor(). The method should return |true| if it generated any code
  138. // that requires a call; this allows the message generator to eliminate the
  139. // OwnDestructor() registration if no fields require it.
  140. virtual bool GenerateArenaDestructorCode(io::Printer* printer) const {
  141. return false;
  142. }
  143. // Generate lines to serialize this field directly to the array "target",
  144. // which are placed within the message's SerializeWithCachedSizesToArray()
  145. // method. This must also advance "target" past the written bytes.
  146. virtual void GenerateSerializeWithCachedSizesToArray(
  147. io::Printer* printer) const = 0;
  148. // Generate lines to compute the serialized size of this field, which
  149. // are placed in the message's ByteSize() method.
  150. virtual void GenerateByteSize(io::Printer* printer) const = 0;
  151. void SetHasBitIndex(int32 has_bit_index);
  152. protected:
  153. const FieldDescriptor* descriptor_;
  154. const Options& options_;
  155. std::map<std::string, std::string> variables_;
  156. private:
  157. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator);
  158. };
  159. // Convenience class which constructs FieldGenerators for a Descriptor.
  160. class FieldGeneratorMap {
  161. public:
  162. FieldGeneratorMap(const Descriptor* descriptor, const Options& options,
  163. MessageSCCAnalyzer* scc_analyzer);
  164. ~FieldGeneratorMap();
  165. const FieldGenerator& get(const FieldDescriptor* field) const;
  166. void SetHasBitIndices(const std::vector<int>& has_bit_indices_) {
  167. for (int i = 0; i < descriptor_->field_count(); ++i) {
  168. field_generators_[i]->SetHasBitIndex(has_bit_indices_[i]);
  169. }
  170. }
  171. private:
  172. const Descriptor* descriptor_;
  173. std::vector<std::unique_ptr<FieldGenerator>> field_generators_;
  174. static FieldGenerator* MakeGoogleInternalGenerator(
  175. const FieldDescriptor* field, const Options& options,
  176. MessageSCCAnalyzer* scc_analyzer);
  177. static FieldGenerator* MakeGenerator(const FieldDescriptor* field,
  178. const Options& options,
  179. MessageSCCAnalyzer* scc_analyzer);
  180. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap);
  181. };
  182. } // namespace cpp
  183. } // namespace compiler
  184. } // namespace protobuf
  185. } // namespace google
  186. #endif // GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__