PageRenderTime 37ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/google/protobuf/compiler/cpp/cpp_field.h

https://gitlab.com/admin-github-cloud/protobuf
C Header | 229 lines | 80 code | 39 blank | 110 comment | 0 complexity | 0723d005220628c8c2264c722103ac3d MD5 | raw 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. // 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. #ifndef _SHARED_PTR_H
  38. #include <google/protobuf/stubs/shared_ptr.h>
  39. #endif
  40. #include <string>
  41. #include <google/protobuf/descriptor.h>
  42. #include <google/protobuf/compiler/cpp/cpp_options.h>
  43. namespace google {
  44. namespace protobuf {
  45. namespace io {
  46. class Printer; // printer.h
  47. }
  48. }
  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. map<string, string>* variables,
  58. const Options& options);
  59. void SetCommonOneofFieldVariables(const FieldDescriptor* descriptor,
  60. map<string, string>* variables);
  61. class FieldGenerator {
  62. public:
  63. explicit FieldGenerator(const Options& options) : options_(options) {}
  64. virtual ~FieldGenerator();
  65. // Generate lines of code declaring members fields of the message class
  66. // needed to represent this field. These are placed inside the message
  67. // class.
  68. virtual void GeneratePrivateMembers(io::Printer* printer) const = 0;
  69. // Generate static default variable for this field. These are placed inside
  70. // the message class. Most field types don't need this, so the default
  71. // implementation is empty.
  72. virtual void GenerateStaticMembers(io::Printer* /*printer*/) const {}
  73. // Generate prototypes for accessors that will manipulate imported
  74. // messages inline. These are for .proto.h headers.
  75. //
  76. // In .proto.h mode, the headers of imports are not #included. However,
  77. // functions that manipulate the imported message types need access to
  78. // the class definition of the imported message, meaning that the headers
  79. // must be #included. To get around this, functions that manipulate
  80. // imported message objects are defined as dependent functions in a base
  81. // template class. By making them dependent template functions, the
  82. // function templates will not be instantiated until they are called, so
  83. // we can defer to those translation units to #include the necessary
  84. // generated headers.
  85. //
  86. // See:
  87. // http://en.cppreference.com/w/cpp/language/class_template#Implicit_instantiation
  88. //
  89. // Most field types don't need this, so the default implementation is empty.
  90. virtual void GenerateDependentAccessorDeclarations(
  91. io::Printer* printer) const {}
  92. // Generate prototypes for all of the accessor functions related to this
  93. // field. These are placed inside the class definition.
  94. virtual void GenerateAccessorDeclarations(io::Printer* printer) const = 0;
  95. // Generate inline definitions of depenent accessor functions for this field.
  96. // These are placed inside the header after all class definitions.
  97. virtual void GenerateDependentInlineAccessorDefinitions(
  98. io::Printer* printer) const {}
  99. // Generate inline definitions of accessor functions for this field.
  100. // These are placed inside the header after all class definitions.
  101. // In non-.proto.h mode, this generates dependent accessor functions as well.
  102. virtual void GenerateInlineAccessorDefinitions(
  103. io::Printer* printer, bool is_inline) const = 0;
  104. // Generate definitions of accessors that aren't inlined. These are
  105. // placed somewhere in the .cc file.
  106. // Most field types don't need this, so the default implementation is empty.
  107. virtual void GenerateNonInlineAccessorDefinitions(
  108. io::Printer* /*printer*/) const {}
  109. // Generate lines of code (statements, not declarations) which clear the
  110. // field. This is used to define the clear_$name$() method as well as
  111. // the Clear() method for the whole message.
  112. virtual void GenerateClearingCode(io::Printer* printer) const = 0;
  113. // Generate lines of code (statements, not declarations) which merges the
  114. // contents of the field from the current message to the target message,
  115. // which is stored in the generated code variable "from".
  116. // This is used to fill in the MergeFrom method for the whole message.
  117. // Details of this usage can be found in message.cc under the
  118. // GenerateMergeFrom method.
  119. virtual void GenerateMergingCode(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 OwnDestructor().
  137. // The method should return |true| if it generated any code that requires a
  138. // call; this allows the message generator to eliminate the OwnDestructor()
  139. // registration if no fields require it.
  140. virtual bool GenerateArenaDestructorCode(io::Printer* printer) const {
  141. return false;
  142. }
  143. // Generate code that allocates the fields's default instance.
  144. virtual void GenerateDefaultInstanceAllocator(io::Printer* /*printer*/)
  145. const {}
  146. // Generate code that should be run when ShutdownProtobufLibrary() is called,
  147. // to delete all dynamically-allocated objects.
  148. virtual void GenerateShutdownCode(io::Printer* /*printer*/) const {}
  149. // Generate lines to decode this field, which will be placed inside the
  150. // message's MergeFromCodedStream() method.
  151. virtual void GenerateMergeFromCodedStream(io::Printer* printer) const = 0;
  152. // Generate lines to decode this field from a packed value, which will be
  153. // placed inside the message's MergeFromCodedStream() method.
  154. virtual void GenerateMergeFromCodedStreamWithPacking(io::Printer* printer)
  155. const;
  156. // Generate lines to serialize this field, which are placed within the
  157. // message's SerializeWithCachedSizes() method.
  158. virtual void GenerateSerializeWithCachedSizes(io::Printer* printer) const = 0;
  159. // Generate lines to serialize this field directly to the array "target",
  160. // which are placed within the message's SerializeWithCachedSizesToArray()
  161. // method. This must also advance "target" past the written bytes.
  162. virtual void GenerateSerializeWithCachedSizesToArray(
  163. io::Printer* printer) const = 0;
  164. // Generate lines to compute the serialized size of this field, which
  165. // are placed in the message's ByteSize() method.
  166. virtual void GenerateByteSize(io::Printer* printer) const = 0;
  167. protected:
  168. const Options& options_;
  169. private:
  170. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator);
  171. };
  172. // Convenience class which constructs FieldGenerators for a Descriptor.
  173. class FieldGeneratorMap {
  174. public:
  175. FieldGeneratorMap(const Descriptor* descriptor, const Options& options);
  176. ~FieldGeneratorMap();
  177. const FieldGenerator& get(const FieldDescriptor* field) const;
  178. private:
  179. const Descriptor* descriptor_;
  180. const Options& options_;
  181. google::protobuf::scoped_array<google::protobuf::scoped_ptr<FieldGenerator> > field_generators_;
  182. static FieldGenerator* MakeGenerator(const FieldDescriptor* field,
  183. const Options& options);
  184. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap);
  185. };
  186. } // namespace cpp
  187. } // namespace compiler
  188. } // namespace protobuf
  189. } // namespace google
  190. #endif // GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__