aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/google/protobuf/compiler/csharp/csharp_primitive_field.cc')
-rw-r--r--src/google/protobuf/compiler/csharp/csharp_primitive_field.cc221
1 files changed, 76 insertions, 145 deletions
diff --git a/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc b/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc
index 652eb6b9..fc043ec0 100644
--- a/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc
+++ b/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc
@@ -49,81 +49,42 @@ namespace csharp {
PrimitiveFieldGenerator::PrimitiveFieldGenerator(
const FieldDescriptor* descriptor, int fieldOrdinal)
: FieldGeneratorBase(descriptor, fieldOrdinal) {
+ // TODO(jonskeet): Make this cleaner...
+ is_value_type = descriptor->type() != FieldDescriptor::TYPE_STRING
+ && descriptor->type() != FieldDescriptor::TYPE_BYTES;
+ if (!is_value_type) {
+ variables_["has_property_check"] = variables_["property_name"] + ".Length != 0";
+ variables_["other_has_property_check"] = "other." + variables_["property_name"] + ".Length != 0";
+ }
}
PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {
-
}
void PrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) {
- if (SupportFieldPresence(descriptor_->file())) {
- printer->Print(variables_, "private bool has$property_name$;\n");
- }
+ // TODO(jonskeet): Work out whether we want to prevent the fields from ever being
+ // null, or whether we just handle it, in the cases of bytes and string.
+ // (Basically, should null-handling code be in the getter or the setter?)
printer->Print(
variables_,
"private $type_name$ $name_def_message$;\n");
AddDeprecatedFlag(printer);
- if (SupportFieldPresence(descriptor_->file())) {
- printer->Print(
- variables_,
- "public bool Has$property_name$ {\n"
- " get { return has$property_name$; }\n"
- "}\n");
- }
- AddPublicMemberAttributes(printer);
printer->Print(
variables_,
- "public $type_name$ $property_name$ {\n"
+ "$access_level$ $type_name$ $property_name$ {\n"
" get { return $name$_; }\n"
- "}\n");
-}
-
-void PrimitiveFieldGenerator::GenerateBuilderMembers(io::Printer* printer) {
- AddDeprecatedFlag(printer);
- if (SupportFieldPresence(descriptor_->file())) {
+ " set {\n");
+ if (is_value_type) {
printer->Print(
variables_,
- "public bool Has$property_name$ {\n"
- " get { return result.has$property_name$; }\n"
- "}\n");
- }
- AddPublicMemberAttributes(printer);
- printer->Print(
- variables_,
- "public $type_name$ $property_name$ {\n"
- " get { return result.$property_name$; }\n"
- " set { Set$property_name$(value); }\n"
- "}\n");
- AddPublicMemberAttributes(printer);
- printer->Print(
- variables_,
- "public Builder Set$property_name$($type_name$ value) {\n");
- AddNullCheck(printer);
- printer->Print(" PrepareBuilder();\n");
- if (SupportFieldPresence(descriptor_->file())) {
- printer->Print(
- variables_,
- " result.has$property_name$ = true;\n");
- }
- printer->Print(
- variables_,
- " result.$name$_ = value;\n"
- " return this;\n"
- "}\n");
- AddDeprecatedFlag(printer);
- printer->Print(
- variables_,
- "public Builder Clear$property_name$() {\n"
- " PrepareBuilder();\n");
- if (SupportFieldPresence(descriptor_->file())) {
+ " $name$_ = value;\n");
+ } else {
printer->Print(
variables_,
- " result.has$property_name$ = false;\n");
+ " $name$_ = pb::Preconditions.CheckNotNull(value, \"value\");\n");
}
printer->Print(
- variables_,
- " result.$name$_ = $default_value$;\n"
- " return this;\n"
+ " }\n"
"}\n");
}
@@ -135,60 +96,68 @@ void PrimitiveFieldGenerator::GenerateMergingCode(io::Printer* printer) {
"}\n");
}
-void PrimitiveFieldGenerator::GenerateBuildingCode(io::Printer* printer) {
- // Nothing to do here for primitive types
-}
-
void PrimitiveFieldGenerator::GenerateParsingCode(io::Printer* printer) {
- if (SupportFieldPresence(descriptor_->file())) {
- printer->Print(
- variables_,
- "result.has$property_name$ = input.Read$capitalized_type_name$(ref result.$name$_);\n");
- } else {
- printer->Print(
- variables_,
- "input.Read$capitalized_type_name$(ref result.$name$_);\n");
- }
+ // Note: invoke the property setter rather than writing straight to the field,
+ // so that we can normalize "null to empty" for strings and bytes.
+ printer->Print(
+ variables_,
+ "$property_name$ = input.Read$capitalized_type_name$();\n");
}
void PrimitiveFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
printer->Print(
variables_,
"if ($has_property_check$) {\n"
- " output.Write$capitalized_type_name$($number$, field_names[$field_ordinal$], $property_name$);\n"
+ " output.WriteRawTag($tag_bytes$);\n"
+ " output.Write$capitalized_type_name$($property_name$);\n"
"}\n");
}
void PrimitiveFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
printer->Print(
variables_,
- "if ($has_property_check$) {\n"
- " size += pb::CodedOutputStream.Compute$capitalized_type_name$Size($number$, $property_name$);\n"
- "}\n");
+ "if ($has_property_check$) {\n");
+ printer->Indent();
+ int fixedSize = GetFixedSize(descriptor_->type());
+ if (fixedSize == -1) {
+ printer->Print(
+ variables_,
+ "size += $tag_size$ + pb::CodedOutputStream.Compute$capitalized_type_name$Size($property_name$);\n");
+ } else {
+ printer->Print(
+ "size += $tag_size$ + $fixed_size$;\n",
+ "fixed_size", SimpleItoa(fixedSize),
+ "tag_size", variables_["tag_size"]);
+ }
+ printer->Outdent();
+ printer->Print("}\n");
}
void PrimitiveFieldGenerator::WriteHash(io::Printer* printer) {
printer->Print(
variables_,
- "if ($has_property_check$) {\n"
- " hash ^= $name$_.GetHashCode();\n"
- "}\n");
+ "if ($has_property_check$) hash ^= $property_name$.GetHashCode();\n");
}
void PrimitiveFieldGenerator::WriteEquals(io::Printer* printer) {
- if (SupportFieldPresence(descriptor_->file())) {
- printer->Print(
- variables_,
- "if (has$property_name$ != other.has$property_name$ || (has$property_name$ && !$name$_.Equals(other.$name$_))) return false;\n");
- } else {
- printer->Print(
- variables_,
- "if (!$name$_.Equals(other.$name$_)) return false;\n");
- }
+ printer->Print(
+ variables_,
+ "if ($property_name$ != other.$property_name$) return false;\n");
}
void PrimitiveFieldGenerator::WriteToString(io::Printer* printer) {
printer->Print(
variables_,
- "PrintField(\"$descriptor_name$\", $has_property_check$, $name$_, writer);\n");
+ "PrintField(\"$descriptor_name$\", $has_property_check$, $property_name$, writer);\n");
+}
+
+void PrimitiveFieldGenerator::GenerateCloningCode(io::Printer* printer) {
+ printer->Print(variables_,
+ "$name$_ = other.$name$_;\n");
+}
+
+void PrimitiveFieldGenerator::GenerateCodecCode(io::Printer* printer) {
+ printer->Print(
+ variables_,
+ "pb::FieldCodec.For$capitalized_type_name$($tag$)");
}
PrimitiveOneofFieldGenerator::PrimitiveOneofFieldGenerator(
@@ -202,79 +171,41 @@ PrimitiveOneofFieldGenerator::~PrimitiveOneofFieldGenerator() {
void PrimitiveOneofFieldGenerator::GenerateMembers(io::Printer* printer) {
AddDeprecatedFlag(printer);
- if (SupportFieldPresence(descriptor_->file())) {
- printer->Print(
- variables_,
- "public bool Has$property_name$ {\n"
- " get { return $has_property_check$; }\n"
- "}\n");
- }
- AddPublicMemberAttributes(printer);
printer->Print(
variables_,
- "public $type_name$ $property_name$ {\n"
+ "$access_level$ $type_name$ $property_name$ {\n"
" get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : $default_value$; }\n"
- "}\n");
-}
-
-void PrimitiveOneofFieldGenerator::GenerateBuilderMembers(io::Printer* printer) {
- AddDeprecatedFlag(printer);
- if (SupportFieldPresence(descriptor_->file())) {
+ " set {\n");
+ if (is_value_type) {
+ printer->Print(
+ variables_,
+ " $oneof_name$_ = value;\n");
+ } else {
+ printer->Print(
+ variables_,
+ " $oneof_name$_ = pb::Preconditions.CheckNotNull(value, \"value\");\n");
+ }
printer->Print(
variables_,
- "public bool Has$property_name$ {\n"
- " get { return result.$has_property_check$; }\n"
+ " $oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n"
+ " }\n"
"}\n");
- }
- AddPublicMemberAttributes(printer);
- printer->Print(
- variables_,
- "public $type_name$ $property_name$ {\n"
- " get { return result.$has_property_check$ ? ($type_name$) result.$oneof_name$_ : $default_value$; }\n"
- " set { Set$property_name$(value); }\n"
- "}\n");
- AddPublicMemberAttributes(printer);
- printer->Print(
- variables_,
- "public Builder Set$property_name$($type_name$ value) {\n");
- AddNullCheck(printer);
- printer->Print(
- variables_,
- " PrepareBuilder();\n"
- " result.$oneof_name$_ = value;\n"
- " result.$oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n"
- " return this;\n"
- "}\n");
- AddDeprecatedFlag(printer);
- printer->Print(
- variables_,
- "public Builder Clear$property_name$() {\n"
- " PrepareBuilder();\n"
- " if (result.$has_property_check$) {\n"
- " result.$oneof_name$Case_ = $oneof_property_name$OneofCase.None;\n"
- " }\n"
- " return this;\n"
- "}\n");
}
-void PrimitiveOneofFieldGenerator::WriteEquals(io::Printer* printer) {
- printer->Print(
- variables_,
- "if (!$property_name$.Equals(other.$property_name$)) return false;\n");
-}
void PrimitiveOneofFieldGenerator::WriteToString(io::Printer* printer) {
printer->Print(variables_,
"PrintField(\"$descriptor_name$\", $has_property_check$, $oneof_name$_, writer);\n");
}
void PrimitiveOneofFieldGenerator::GenerateParsingCode(io::Printer* printer) {
- printer->Print(
- variables_,
- "$type_name$ value = $default_value$;\n"
- "if (input.Read$capitalized_type_name$(ref value)) {\n"
- " result.$oneof_name$_ = value;\n"
- " result.$oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n"
- "}\n");
+ printer->Print(
+ variables_,
+ "$property_name$ = input.Read$capitalized_type_name$();\n");
+}
+
+void PrimitiveOneofFieldGenerator::GenerateCloningCode(io::Printer* printer) {
+ printer->Print(variables_,
+ "$property_name$ = other.$property_name$;\n");
}
} // namespace csharp