diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/google/protobuf/compiler/csharp/csharp_enum_field.cc | 168 | ||||
-rw-r--r-- | src/google/protobuf/compiler/csharp/csharp_enum_field.h | 23 | ||||
-rw-r--r-- | src/google/protobuf/wire_format_unittest.cc | 2 |
3 files changed, 24 insertions, 169 deletions
diff --git a/src/google/protobuf/compiler/csharp/csharp_enum_field.cc b/src/google/protobuf/compiler/csharp/csharp_enum_field.cc index a2d8a98b..28f5a05c 100644 --- a/src/google/protobuf/compiler/csharp/csharp_enum_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_enum_field.cc @@ -48,81 +48,10 @@ namespace csharp { EnumFieldGenerator::EnumFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal) - : FieldGeneratorBase(descriptor, fieldOrdinal) { - if (SupportFieldPresence(descriptor_->file())) { - has_property_check = "has" + property_name(); - } else { - has_property_check = property_name() + " != " + default_value(); - } + : PrimitiveFieldGenerator(descriptor, fieldOrdinal) { } EnumFieldGenerator::~EnumFieldGenerator() { - -} - -void EnumFieldGenerator::GenerateMembers(Writer* writer) { - if (SupportFieldPresence(descriptor_->file())) { - writer->WriteLine("private bool has$0$;", property_name()); - } - writer->WriteLine("private $0$ $1$_ = $2$;", type_name(), name(), - default_value()); - AddDeprecatedFlag(writer); - if (SupportFieldPresence(descriptor_->file())) { - writer->WriteLine("public bool Has$0$ {", property_name()); - writer->WriteLine(" get { return has$0$; }", property_name()); - writer->WriteLine("}"); - } - AddPublicMemberAttributes(writer); - writer->WriteLine("public $0$ $1$ {", type_name(), property_name()); - writer->WriteLine(" get { return $0$_; }", name()); - writer->WriteLine("}"); -} - -void EnumFieldGenerator::GenerateBuilderMembers(Writer* writer) { - AddDeprecatedFlag(writer); - if (SupportFieldPresence(descriptor_->file())) { - writer->WriteLine("public bool Has$0$ {", property_name()); - writer->WriteLine(" get { return result.has$0$; }", property_name()); - writer->WriteLine("}"); - } - AddPublicMemberAttributes(writer); - writer->WriteLine("public $0$ $1$ {", type_name(), property_name()); - writer->WriteLine(" get { return result.$0$; }", property_name()); - writer->WriteLine(" set { Set$0$(value); }", property_name()); - writer->WriteLine("}"); - AddPublicMemberAttributes(writer); - writer->WriteLine("public Builder Set$0$($1$ value) {", property_name(), - type_name()); - writer->WriteLine(" PrepareBuilder();"); - if (SupportFieldPresence(descriptor_->file())) { - writer->WriteLine(" result.has$0$ = true;", property_name()); - } - writer->WriteLine(" result.$0$_ = value;", name()); - writer->WriteLine(" return this;"); - writer->WriteLine("}"); - AddDeprecatedFlag(writer); - writer->WriteLine("public Builder Clear$0$() {", property_name()); - writer->WriteLine(" PrepareBuilder();"); - if (SupportFieldPresence(descriptor_->file())) { - writer->WriteLine(" result.has$0$ = false;", property_name()); - } - writer->WriteLine(" result.$0$_ = $1$;", name(), default_value()); - writer->WriteLine(" return this;"); - writer->WriteLine("}"); -} - -void EnumFieldGenerator::GenerateMergingCode(Writer* writer) { - if (SupportFieldPresence(descriptor_->file())) { - writer->WriteLine("if (other.Has$0$) {", property_name()); - } else { - writer->WriteLine("if (other.$0$ != $1$) {", property_name(), default_value()); - } - writer->WriteLine(" $0$ = other.$0$;", property_name()); - writer->WriteLine("}"); -} - -void EnumFieldGenerator::GenerateBuildingCode(Writer* writer) { - // Nothing to do here for enum types } void EnumFieldGenerator::GenerateParsingCode(Writer* writer) { @@ -160,91 +89,14 @@ void EnumFieldGenerator::GenerateSerializedSizeCode(Writer* writer) { writer->WriteLine("}"); } -void EnumFieldGenerator::WriteHash(Writer* writer) { - writer->WriteLine("if ($0$) {", has_property_check); - writer->WriteLine(" hash ^= $0$_.GetHashCode();", name()); - writer->WriteLine("}"); -} -void EnumFieldGenerator::WriteEquals(Writer* writer) { - if (SupportFieldPresence(descriptor_->file())) { - writer->WriteLine( - "if (has$0$ != other.has$0$ || (has$0$ && !$1$_.Equals(other.$1$_))) return false;", - property_name(), name()); - } else { - writer->WriteLine( - "if (!$0$_.Equals(other.$0$_)) return false;", name()); - } -} -void EnumFieldGenerator::WriteToString(Writer* writer) { - writer->WriteLine("PrintField(\"$0$\", $1$, $2$_, writer);", - descriptor_->name(), has_property_check, name()); -} - EnumOneofFieldGenerator::EnumOneofFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal) - : EnumFieldGenerator(descriptor, fieldOrdinal) { - has_property_check = oneof_name() + "Case_ == " + oneof_property_name() + - "OneofCase." + property_name(); + : PrimitiveOneofFieldGenerator(descriptor, fieldOrdinal) { } EnumOneofFieldGenerator::~EnumOneofFieldGenerator() { } -void EnumOneofFieldGenerator::GenerateMembers(Writer* writer) { - AddDeprecatedFlag(writer); - if (SupportFieldPresence(descriptor_->file())) { - writer->WriteLine("public bool Has$0$ {", property_name()); - writer->WriteLine(" get { return $0$; }", has_property_check); - writer->WriteLine("}"); - } - AddPublicMemberAttributes(writer); - writer->WriteLine("public $0$ $1$ {", type_name(), property_name()); - writer->WriteLine(" get { return $0$ ? ($1$) $2$_ : $3$; }", - has_property_check, type_name(), oneof_name(), default_value()); - writer->WriteLine("}"); -} - -void EnumOneofFieldGenerator::GenerateBuilderMembers(Writer* writer) { - AddDeprecatedFlag(writer); - if (SupportFieldPresence(descriptor_->file())) { - writer->WriteLine("public bool Has$0$ {", property_name()); - writer->WriteLine(" get { return result.$0$; }", has_property_check); - writer->WriteLine("}"); - } - AddPublicMemberAttributes(writer); - writer->WriteLine("public $0$ $1$ {", type_name(), property_name()); - writer->WriteLine(" get { return result.$0$ ? ($1$) result.$2$_ : $3$; }", - has_property_check, type_name(), oneof_name(), default_value()); - writer->WriteLine(" set { Set$0$(value); }", property_name()); - writer->WriteLine("}"); - AddPublicMemberAttributes(writer); - writer->WriteLine("public Builder Set$0$($1$ value) {", property_name(), - type_name()); - writer->WriteLine(" PrepareBuilder();"); - writer->WriteLine(" result.$0$_ = value;", oneof_name()); - writer->WriteLine(" result.$0$Case_ = $1$OneofCase.$2$;", - oneof_name(), oneof_property_name(), property_name()); - writer->WriteLine(" return this;"); - writer->WriteLine("}"); - AddDeprecatedFlag(writer); - writer->WriteLine("public Builder Clear$0$() {", property_name()); - writer->WriteLine(" PrepareBuilder();"); - writer->WriteLine(" if (result.$0$) {", has_property_check); - writer->WriteLine(" result.$0$Case_ = $1$OneofCase.None;", - oneof_name(), oneof_property_name()); - writer->WriteLine(" }"); - writer->WriteLine(" return this;"); - writer->WriteLine("}"); -} - -void EnumOneofFieldGenerator::WriteEquals(Writer* writer) { - writer->WriteLine("if (!$0$.Equals(other.$0$)) return false;", property_name()); -} -void EnumOneofFieldGenerator::WriteToString(Writer* writer) { - writer->WriteLine("PrintField(\"$0$\", $1$, $2$_, writer);", - descriptor_->name(), has_property_check, oneof_name()); -} - void EnumOneofFieldGenerator::GenerateParsingCode(Writer* writer) { writer->WriteLine("object unknown;"); writer->WriteLine("$0$ enumValue = $1$;", type_name(), default_value()); @@ -266,6 +118,22 @@ void EnumOneofFieldGenerator::GenerateParsingCode(Writer* writer) { writer->WriteLine("}"); } +void EnumOneofFieldGenerator::GenerateSerializationCode(Writer* writer) { + writer->WriteLine("if ($0$) {", has_property_check); + writer->WriteLine( + " output.WriteEnum($0$, field_names[$2$], (int) $1$, $1$);", number(), + property_name(), field_ordinal()); + writer->WriteLine("}"); +} + +void EnumOneofFieldGenerator::GenerateSerializedSizeCode(Writer* writer) { + writer->WriteLine("if ($0$) {", has_property_check); + writer->WriteLine( + " size += pb::CodedOutputStream.ComputeEnumSize($0$, (int) $1$);", + number(), property_name()); + writer->WriteLine("}"); +} + } // namespace csharp } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/csharp/csharp_enum_field.h b/src/google/protobuf/compiler/csharp/csharp_enum_field.h index 565287b2..c6b7b848 100644 --- a/src/google/protobuf/compiler/csharp/csharp_enum_field.h +++ b/src/google/protobuf/compiler/csharp/csharp_enum_field.h @@ -34,7 +34,7 @@ #include <string> #include <google/protobuf/compiler/code_generator.h> -#include <google/protobuf/compiler/csharp/csharp_field_base.h> +#include <google/protobuf/compiler/csharp/csharp_primitive_field.h> namespace google { namespace protobuf { @@ -43,40 +43,27 @@ namespace csharp { class Writer; -class EnumFieldGenerator : public FieldGeneratorBase { +class EnumFieldGenerator : public PrimitiveFieldGenerator { public: EnumFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal); ~EnumFieldGenerator(); - virtual void GenerateMembers(Writer* writer); - virtual void GenerateBuilderMembers(Writer* writer); - virtual void GenerateMergingCode(Writer* writer); - virtual void GenerateBuildingCode(Writer* writer); virtual void GenerateParsingCode(Writer* writer); virtual void GenerateSerializationCode(Writer* writer); virtual void GenerateSerializedSizeCode(Writer* writer); - virtual void WriteHash(Writer* writer); - virtual void WriteEquals(Writer* writer); - virtual void WriteToString(Writer* writer); - - protected: - string has_property_check; - private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumFieldGenerator); }; -class EnumOneofFieldGenerator : public EnumFieldGenerator { +class EnumOneofFieldGenerator : public PrimitiveOneofFieldGenerator { public: EnumOneofFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal); ~EnumOneofFieldGenerator(); - virtual void GenerateMembers(Writer* writer); - virtual void GenerateBuilderMembers(Writer* writer); - virtual void WriteEquals(Writer* writer); - virtual void WriteToString(Writer* writer); virtual void GenerateParsingCode(Writer* writer); + virtual void GenerateSerializationCode(Writer* writer); + virtual void GenerateSerializedSizeCode(Writer* writer); private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumOneofFieldGenerator); diff --git a/src/google/protobuf/wire_format_unittest.cc b/src/google/protobuf/wire_format_unittest.cc index e80c5a71..4b151f97 100644 --- a/src/google/protobuf/wire_format_unittest.cc +++ b/src/google/protobuf/wire_format_unittest.cc @@ -768,7 +768,7 @@ TEST(WireFormatTest, RepeatedScalarsDifferentTagSizes) { } TEST(WireFormatTest, CompatibleTypes) { - const int64 data = 0x100000000; + const int64 data = 0x100000000LL; unittest::Int64Message msg1; msg1.set_data(data); string serialized; |