1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 #ifndef GOOGLE_PROTOBUF_COMPILER_CSHARP_FIELD_BASE_H__ 9 #define GOOGLE_PROTOBUF_COMPILER_CSHARP_FIELD_BASE_H__ 10 11 #include <string> 12 13 #include "google/protobuf/compiler/code_generator.h" 14 #include "absl/container/flat_hash_map.h" 15 #include "absl/strings/ascii.h" 16 #include "absl/strings/escaping.h" 17 #include "absl/strings/str_replace.h" 18 #include "absl/strings/str_split.h" 19 #include "google/protobuf/compiler/csharp/csharp_source_generator_base.h" 20 #include "google/protobuf/descriptor.h" 21 #include "google/protobuf/io/printer.h" 22 23 namespace google { 24 namespace protobuf { 25 namespace compiler { 26 namespace csharp { 27 28 class FieldGeneratorBase : public SourceGeneratorBase { 29 public: 30 FieldGeneratorBase(const FieldDescriptor* descriptor, 31 int presenceIndex, 32 const Options* options); 33 ~FieldGeneratorBase(); 34 35 FieldGeneratorBase(const FieldGeneratorBase&) = delete; 36 FieldGeneratorBase& operator=(const FieldGeneratorBase&) = delete; 37 38 virtual void GenerateCloningCode(io::Printer* printer) = 0; 39 virtual void GenerateFreezingCode(io::Printer* printer); 40 virtual void GenerateCodecCode(io::Printer* printer); 41 virtual void GenerateExtensionCode(io::Printer* printer); 42 virtual void GenerateMembers(io::Printer* printer) = 0; 43 virtual void GenerateMergingCode(io::Printer* printer) = 0; 44 virtual void GenerateParsingCode(io::Printer* printer) = 0; 45 virtual void GenerateParsingCode(io::Printer* printer, bool use_parse_context); 46 virtual void GenerateSerializationCode(io::Printer* printer) = 0; 47 virtual void GenerateSerializationCode(io::Printer* printer, bool use_write_context); 48 virtual void GenerateSerializedSizeCode(io::Printer* printer) = 0; 49 50 virtual void WriteHash(io::Printer* printer) = 0; 51 virtual void WriteEquals(io::Printer* printer) = 0; 52 // Currently unused, as we use reflection to generate JSON 53 virtual void WriteToString(io::Printer* printer) = 0; 54 55 protected: 56 const FieldDescriptor* descriptor_; 57 const int presenceIndex_; 58 absl::flat_hash_map<absl::string_view, std::string> variables_; 59 60 void AddDeprecatedFlag(io::Printer* printer); 61 void AddNullCheck(io::Printer* printer); 62 void AddNullCheck(io::Printer* printer, const std::string& name); 63 64 void AddPublicMemberAttributes(io::Printer* printer); 65 void SetCommonOneofFieldVariables( 66 absl::flat_hash_map<absl::string_view, std::string>* variables); 67 68 std::string oneof_property_name(); 69 std::string oneof_case_name(); 70 std::string oneof_name(); 71 std::string property_name(); 72 std::string name(); 73 std::string type_name(); 74 std::string type_name(const FieldDescriptor* descriptor); 75 bool has_default_value(); 76 std::string default_value(); 77 std::string default_value(const FieldDescriptor* descriptor); 78 std::string number(); 79 std::string capitalized_type_name(); 80 81 private: 82 void SetCommonFieldVariables( 83 absl::flat_hash_map<absl::string_view, std::string>* variables); 84 std::string GetStringDefaultValueInternal(const FieldDescriptor* descriptor); 85 std::string GetBytesDefaultValueInternal(const FieldDescriptor* descriptor); 86 }; 87 88 } // namespace csharp 89 } // namespace compiler 90 } // namespace protobuf 91 } // namespace google 92 93 #endif // GOOGLE_PROTOBUF_COMPILER_CSHARP_FIELD_BASE_H__ 94 95