cpp_field.h 9.3 KB

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