java_field.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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_JAVA_FIELD_H__
  34. #define GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__
  35. #include <map>
  36. #include <memory>
  37. #include <string>
  38. #include <google/protobuf/stubs/common.h>
  39. #include <google/protobuf/descriptor.h>
  40. #include <google/protobuf/stubs/logging.h>
  41. namespace google {
  42. namespace protobuf {
  43. namespace compiler {
  44. namespace java {
  45. class Context; // context.h
  46. class ClassNameResolver; // name_resolver.h
  47. }
  48. }
  49. namespace io {
  50. class Printer; // printer.h
  51. }
  52. }
  53. namespace protobuf {
  54. namespace compiler {
  55. namespace java {
  56. class ImmutableFieldGenerator {
  57. public:
  58. ImmutableFieldGenerator() {}
  59. virtual ~ImmutableFieldGenerator();
  60. virtual int GetNumBitsForMessage() const = 0;
  61. virtual int GetNumBitsForBuilder() const = 0;
  62. virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0;
  63. virtual void GenerateMembers(io::Printer* printer) const = 0;
  64. virtual void GenerateBuilderMembers(io::Printer* printer) const = 0;
  65. virtual void GenerateInitializationCode(io::Printer* printer) const = 0;
  66. virtual void GenerateBuilderClearCode(io::Printer* printer) const = 0;
  67. virtual void GenerateMergingCode(io::Printer* printer) const = 0;
  68. virtual void GenerateBuildingCode(io::Printer* printer) const = 0;
  69. virtual void GenerateParsingCode(io::Printer* printer) const = 0;
  70. virtual void GenerateParsingCodeFromPacked(io::Printer* printer) const;
  71. virtual void GenerateParsingDoneCode(io::Printer* printer) const = 0;
  72. virtual void GenerateSerializationCode(io::Printer* printer) const = 0;
  73. virtual void GenerateSerializedSizeCode(io::Printer* printer) const = 0;
  74. virtual void GenerateFieldBuilderInitializationCode(io::Printer* printer)
  75. const = 0;
  76. virtual void GenerateEqualsCode(io::Printer* printer) const = 0;
  77. virtual void GenerateHashCode(io::Printer* printer) const = 0;
  78. virtual string GetBoxedType() const = 0;
  79. private:
  80. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableFieldGenerator);
  81. };
  82. class ImmutableFieldLiteGenerator {
  83. public:
  84. ImmutableFieldLiteGenerator() {}
  85. virtual ~ImmutableFieldLiteGenerator();
  86. virtual int GetNumBitsForMessage() const = 0;
  87. virtual int GetNumBitsForBuilder() const = 0;
  88. virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0;
  89. virtual void GenerateMembers(io::Printer* printer) const = 0;
  90. virtual void GenerateBuilderMembers(io::Printer* printer) const = 0;
  91. virtual void GenerateInitializationCode(io::Printer* printer) const = 0;
  92. virtual void GenerateVisitCode(io::Printer* printer) const = 0;
  93. virtual void GenerateDynamicMethodMakeImmutableCode(io::Printer* printer)
  94. const = 0;
  95. virtual void GenerateParsingCode(io::Printer* printer) const = 0;
  96. virtual void GenerateParsingCodeFromPacked(io::Printer* printer) const;
  97. virtual void GenerateParsingDoneCode(io::Printer* printer) const = 0;
  98. virtual void GenerateSerializationCode(io::Printer* printer) const = 0;
  99. virtual void GenerateSerializedSizeCode(io::Printer* printer) const = 0;
  100. virtual void GenerateFieldBuilderInitializationCode(io::Printer* printer)
  101. const = 0;
  102. virtual void GenerateEqualsCode(io::Printer* printer) const = 0;
  103. virtual void GenerateHashCode(io::Printer* printer) const = 0;
  104. virtual string GetBoxedType() const = 0;
  105. private:
  106. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableFieldLiteGenerator);
  107. };
  108. // Convenience class which constructs FieldGenerators for a Descriptor.
  109. template<typename FieldGeneratorType>
  110. class FieldGeneratorMap {
  111. public:
  112. explicit FieldGeneratorMap(const Descriptor* descriptor,
  113. Context* context);
  114. ~FieldGeneratorMap();
  115. const FieldGeneratorType& get(const FieldDescriptor* field) const;
  116. private:
  117. const Descriptor* descriptor_;
  118. Context* context_;
  119. ClassNameResolver* name_resolver_;
  120. std::vector<std::unique_ptr<FieldGeneratorType>> field_generators_;
  121. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap);
  122. };
  123. template<typename FieldGeneratorType>
  124. inline const FieldGeneratorType&
  125. FieldGeneratorMap<FieldGeneratorType>::get(const FieldDescriptor* field) const {
  126. GOOGLE_CHECK_EQ(field->containing_type(), descriptor_);
  127. return *field_generators_[field->index()];
  128. }
  129. // Instantiate template for mutable and immutable maps.
  130. template<>
  131. FieldGeneratorMap<ImmutableFieldGenerator>::
  132. FieldGeneratorMap(const Descriptor* descriptor,
  133. Context* context);
  134. template<>
  135. FieldGeneratorMap<ImmutableFieldGenerator>::~FieldGeneratorMap();
  136. template <>
  137. FieldGeneratorMap<ImmutableFieldLiteGenerator>::FieldGeneratorMap(
  138. const Descriptor* descriptor, Context* context);
  139. template <>
  140. FieldGeneratorMap<ImmutableFieldLiteGenerator>::~FieldGeneratorMap();
  141. // Field information used in FieldGeneartors.
  142. struct FieldGeneratorInfo {
  143. string name;
  144. string capitalized_name;
  145. string disambiguated_reason;
  146. };
  147. // Oneof information used in OneofFieldGenerators.
  148. struct OneofGeneratorInfo {
  149. string name;
  150. string capitalized_name;
  151. };
  152. // Set some common variables used in variable FieldGenerators.
  153. void SetCommonFieldVariables(const FieldDescriptor* descriptor,
  154. const FieldGeneratorInfo* info,
  155. std::map<string, string>* variables);
  156. // Set some common oneof variables used in OneofFieldGenerators.
  157. void SetCommonOneofVariables(const FieldDescriptor* descriptor,
  158. const OneofGeneratorInfo* info,
  159. std::map<string, string>* variables);
  160. // Print useful comments before a field's accessors.
  161. void PrintExtraFieldInfo(const std::map<string, string>& variables,
  162. io::Printer* printer);
  163. } // namespace java
  164. } // namespace compiler
  165. } // namespace protobuf
  166. } // namespace google
  167. #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__