1 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_SERVICE_COMMON_H__ 2 #define GOOGLE_PROTOBUF_COMPILER_JAVA_SERVICE_COMMON_H__ 3 4 #include <cstddef> 5 #include <memory> 6 #include <vector> 7 8 #include "absl/log/absl_check.h" 9 #include "absl/log/absl_log.h" 10 #include "google/protobuf/compiler/java/context.h" 11 #include "google/protobuf/compiler/java/name_resolver.h" 12 #include "google/protobuf/descriptor.h" 13 #include "google/protobuf/io/printer.h" 14 15 namespace google { 16 namespace protobuf { 17 namespace compiler { 18 namespace java { 19 20 static const int kMaxStaticSize = 1 << 15; // aka 32k 21 22 class FieldGenerator { 23 public: 24 virtual ~FieldGenerator() = default; 25 virtual void GenerateSerializationCode(io::Printer* printer) const = 0; 26 virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0; 27 }; 28 29 // Convenience class which constructs FieldGenerators for a Descriptor. 30 template <typename FieldGeneratorType> 31 class FieldGeneratorMap { 32 public: FieldGeneratorMap(const Descriptor * descriptor)33 explicit FieldGeneratorMap(const Descriptor* descriptor) 34 : descriptor_(descriptor) { 35 field_generators_.reserve(static_cast<size_t>(descriptor->field_count())); 36 } 37 ~FieldGeneratorMap()38 ~FieldGeneratorMap() { 39 for (const auto* g : field_generators_) { 40 delete g; 41 } 42 } 43 44 FieldGeneratorMap(FieldGeneratorMap&&) = default; 45 FieldGeneratorMap& operator=(FieldGeneratorMap&&) = default; 46 47 FieldGeneratorMap(const FieldGeneratorMap&) = delete; 48 FieldGeneratorMap& operator=(const FieldGeneratorMap&) = delete; 49 Add(const FieldDescriptor * field,std::unique_ptr<FieldGeneratorType> field_generator)50 void Add(const FieldDescriptor* field, 51 std::unique_ptr<FieldGeneratorType> field_generator) { 52 ABSL_CHECK_EQ(field->containing_type(), descriptor_); 53 field_generators_.push_back(field_generator.release()); 54 } 55 get(const FieldDescriptor * field)56 const FieldGeneratorType& get(const FieldDescriptor* field) const { 57 ABSL_CHECK_EQ(field->containing_type(), descriptor_); 58 return *field_generators_[static_cast<size_t>(field->index())]; 59 } 60 field_generators()61 std::vector<const FieldGenerator*> field_generators() const { 62 std::vector<const FieldGenerator*> field_generators; 63 field_generators.reserve(field_generators_.size()); 64 for (const auto* g : field_generators_) { 65 field_generators.push_back(g); 66 } 67 return field_generators; 68 } 69 70 private: 71 const Descriptor* descriptor_; 72 std::vector<const FieldGeneratorType*> field_generators_; 73 }; 74 ReportUnexpectedPackedFieldsCall()75inline void ReportUnexpectedPackedFieldsCall() { 76 // Reaching here indicates a bug. Cases are: 77 // - This FieldGenerator should support packing, 78 // but this method should be overridden. 79 // - This FieldGenerator doesn't support packing, and this method 80 // should never have been called. 81 ABSL_LOG(FATAL) << "GenerateBuilderParsingCodeFromPacked() " 82 << "called on field generator that does not support packing."; 83 } 84 85 } // namespace java 86 } // namespace compiler 87 } // namespace protobuf 88 } // namespace google 89 90 #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_SERVICE_COMMON_H__ 91