File indexing completed on 2025-03-01 10:26:07
0001 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_SERVICE_COMMON_H__
0002 #define GOOGLE_PROTOBUF_COMPILER_JAVA_SERVICE_COMMON_H__
0003
0004 #include <cstddef>
0005 #include <memory>
0006 #include <vector>
0007
0008 #include "absl/log/absl_check.h"
0009 #include "absl/log/absl_log.h"
0010 #include "google/protobuf/compiler/java/context.h"
0011 #include "google/protobuf/compiler/java/name_resolver.h"
0012 #include "google/protobuf/descriptor.h"
0013 #include "google/protobuf/io/printer.h"
0014
0015 namespace google {
0016 namespace protobuf {
0017 namespace compiler {
0018 namespace java {
0019
0020 static const int kMaxStaticSize = 1 << 15;
0021
0022 class FieldGenerator {
0023 public:
0024 virtual ~FieldGenerator() = default;
0025 virtual void GenerateSerializationCode(io::Printer* printer) const = 0;
0026 };
0027
0028
0029 template <typename FieldGeneratorType>
0030 class FieldGeneratorMap {
0031 public:
0032 explicit FieldGeneratorMap(const Descriptor* descriptor)
0033 : descriptor_(descriptor) {
0034 field_generators_.reserve(static_cast<size_t>(descriptor->field_count()));
0035 }
0036
0037 ~FieldGeneratorMap() {
0038 for (const auto* g : field_generators_) {
0039 delete g;
0040 }
0041 }
0042
0043 FieldGeneratorMap(FieldGeneratorMap&&) = default;
0044 FieldGeneratorMap& operator=(FieldGeneratorMap&&) = default;
0045
0046 FieldGeneratorMap(const FieldGeneratorMap&) = delete;
0047 FieldGeneratorMap& operator=(const FieldGeneratorMap&) = delete;
0048
0049 void Add(const FieldDescriptor* field,
0050 std::unique_ptr<FieldGeneratorType> field_generator) {
0051 ABSL_CHECK_EQ(field->containing_type(), descriptor_);
0052 field_generators_.push_back(field_generator.release());
0053 }
0054
0055 const FieldGeneratorType& get(const FieldDescriptor* field) const {
0056 ABSL_CHECK_EQ(field->containing_type(), descriptor_);
0057 return *field_generators_[static_cast<size_t>(field->index())];
0058 }
0059
0060 std::vector<const FieldGenerator*> field_generators() const {
0061 std::vector<const FieldGenerator*> field_generators;
0062 field_generators.reserve(field_generators_.size());
0063 for (const auto* g : field_generators_) {
0064 field_generators.push_back(g);
0065 }
0066 return field_generators;
0067 }
0068
0069 private:
0070 const Descriptor* descriptor_;
0071 std::vector<const FieldGeneratorType*> field_generators_;
0072 };
0073
0074 inline void ReportUnexpectedPackedFieldsCall() {
0075
0076
0077
0078
0079
0080 ABSL_LOG(FATAL) << "GenerateBuilderParsingCodeFromPacked() "
0081 << "called on field generator that does not support packing.";
0082 }
0083
0084 }
0085 }
0086 }
0087 }
0088
0089 #endif