diff options
author | Ulas Kirazci <ulas@google.com> | 2013-07-29 18:22:16 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2013-07-29 18:22:16 +0000 |
commit | be47f55e2f93df84abb55759e49cf381d2745cc5 (patch) | |
tree | bf47056d6c723ec8939717efb9a5f30cbe7783af /src/google/protobuf/compiler | |
parent | 021f8f1badf1c4db519e3f35d600dec4b5c52eff (diff) | |
parent | 35d9fd84ff1d9ecdb91156b757cc9fdcc3f25249 (diff) | |
download | external_protobuf-be47f55e2f93df84abb55759e49cf381d2745cc5.zip external_protobuf-be47f55e2f93df84abb55759e49cf381d2745cc5.tar.gz external_protobuf-be47f55e2f93df84abb55759e49cf381d2745cc5.tar.bz2 |
Merge "Add an option to inspect "has" state upon parse."
Diffstat (limited to 'src/google/protobuf/compiler')
6 files changed, 83 insertions, 31 deletions
diff --git a/src/google/protobuf/compiler/javanano/javanano_enum_field.cc b/src/google/protobuf/compiler/javanano/javanano_enum_field.cc index 215f341..9c4acb9 100644 --- a/src/google/protobuf/compiler/javanano/javanano_enum_field.cc +++ b/src/google/protobuf/compiler/javanano/javanano_enum_field.cc @@ -82,12 +82,22 @@ void EnumFieldGenerator:: GenerateMembers(io::Printer* printer) const { printer->Print(variables_, "public int $name$ = $default$;\n"); + + if (params_.generate_has()) { + printer->Print(variables_, + "public boolean has$capitalized_name$ = false;\n"); + } } void EnumFieldGenerator:: GenerateParsingCode(io::Printer* printer) const { printer->Print(variables_, " this.$name$ = input.readInt32();\n"); + + if (params_.generate_has()) { + printer->Print(variables_, + " has$capitalized_name$ = true;\n"); + } } void EnumFieldGenerator:: @@ -96,8 +106,14 @@ GenerateSerializationCode(io::Printer* printer) const { printer->Print(variables_, "output.writeInt32($number$, this.$name$);\n"); } else { + if (params_.generate_has()) { + printer->Print(variables_, + "if (this.$name$ != $default$ || has$capitalized_name$) {\n"); + } else { + printer->Print(variables_, + "if (this.$name$ != $default$) {\n"); + } printer->Print(variables_, - "if (this.$name$ != $default$) {\n" " output.writeInt32($number$, this.$name$);\n" "}\n"); } @@ -110,8 +126,14 @@ GenerateSerializedSizeCode(io::Printer* printer) const { "size += com.google.protobuf.nano.CodedOutputByteBufferNano\n" " .computeInt32Size($number$, this.$name$);\n"); } else { + if (params_.generate_has()) { + printer->Print(variables_, + "if (this.$name$ != $default$ || has$capitalized_name$) {\n"); + } else { + printer->Print(variables_, + "if (this.$name$ != $default$) {\n"); + } printer->Print(variables_, - "if (this.$name$ != $default$) {\n" " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n" " .computeInt32Size($number$, this.$name$);\n" "}\n"); diff --git a/src/google/protobuf/compiler/javanano/javanano_generator.cc b/src/google/protobuf/compiler/javanano/javanano_generator.cc index 4b6b6a5..76e7263 100644 --- a/src/google/protobuf/compiler/javanano/javanano_generator.cc +++ b/src/google/protobuf/compiler/javanano/javanano_generator.cc @@ -118,6 +118,8 @@ bool JavaNanoGenerator::Generate(const FileDescriptor* file, params.set_store_unknown_fields(options[i].second == "true"); } else if (options[i].first == "java_multiple_files") { params.set_override_java_multiple_files(options[i].second == "true"); + } else if (options[i].first == "java_nano_generate_has") { + params.set_generate_has(options[i].second == "true"); } else { *error = "Ignore unknown javanano generator option: " + options[i].first; } diff --git a/src/google/protobuf/compiler/javanano/javanano_message.cc b/src/google/protobuf/compiler/javanano/javanano_message.cc index b5983a2..de56944 100644 --- a/src/google/protobuf/compiler/javanano/javanano_message.cc +++ b/src/google/protobuf/compiler/javanano/javanano_message.cc @@ -406,6 +406,15 @@ void MessageGenerator::GenerateClear(io::Printer* printer) { "name", RenameJavaKeywords(UnderscoresToCamelCase(field)), "default", DefaultValue(params_, field)); } + + if (params_.generate_has() && + field->label() != FieldDescriptor::LABEL_REPEATED && + field->type() != FieldDescriptor::TYPE_GROUP && + field->type() != FieldDescriptor::TYPE_MESSAGE) { + printer->Print( + "has$capitalized_name$ = false;\n", + "capitalized_name", UnderscoresToCapitalizedCamelCase(field)); + } } // Clear unknown fields. diff --git a/src/google/protobuf/compiler/javanano/javanano_params.h b/src/google/protobuf/compiler/javanano/javanano_params.h index 5c0e6f0..51686da 100644 --- a/src/google/protobuf/compiler/javanano/javanano_params.h +++ b/src/google/protobuf/compiler/javanano/javanano_params.h @@ -57,13 +57,15 @@ class Params { NameMap java_packages_; NameMap java_outer_classnames_; NameSet java_multiple_files_; + bool generate_has_; public: Params(const string & base_name) : empty_(""), base_name_(base_name), override_java_multiple_files_(JAVANANO_MUL_UNSET), - store_unknown_fields_(false) { + store_unknown_fields_(false), + generate_has_(false) { } const string& base_name() const { @@ -151,6 +153,13 @@ class Params { return store_unknown_fields_; } + void set_generate_has(bool value) { + generate_has_ = value; + } + bool generate_has() const { + return generate_has_; + } + }; } // namespace javanano diff --git a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc index 987a103..20ea6b0 100644 --- a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc +++ b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc @@ -321,12 +321,46 @@ GenerateMembers(io::Printer* printer) const { printer->Print(variables_, "public $type$ $name$ = $default$;\n"); } + + if (params_.generate_has()) { + printer->Print(variables_, + "public boolean has$capitalized_name$ = false;\n"); + } } void PrimitiveFieldGenerator:: GenerateParsingCode(io::Printer* printer) const { printer->Print(variables_, "this.$name$ = input.read$capitalized_type$();\n"); + + if (params_.generate_has()) { + printer->Print(variables_, + "has$capitalized_name$ = true;\n"); + } +} + +void PrimitiveFieldGenerator:: +GenerateSerializationConditional(io::Printer* printer) const { + if (params_.generate_has()) { + printer->Print(variables_, + "if (has$capitalized_name$ || "); + } else { + printer->Print(variables_, + "if ("); + } + if (IsArrayType(GetJavaType(descriptor_))) { + printer->Print(variables_, + "!java.util.Arrays.equals(this.$name$, $default$)) {\n"); + } else if (IsReferenceType(GetJavaType(descriptor_))) { + printer->Print(variables_, + "!this.$name$.equals($default$)) {\n"); + } else if (IsDefaultNaN(descriptor_)) { + printer->Print(variables_, + "!$capitalized_type$.isNaN(this.$name$)) {\n"); + } else { + printer->Print(variables_, + "this.$name$ != $default$) {\n"); + } } void PrimitiveFieldGenerator:: @@ -335,20 +369,7 @@ GenerateSerializationCode(io::Printer* printer) const { printer->Print(variables_, "output.write$capitalized_type$($number$, this.$name$);\n"); } else { - if (IsArrayType(GetJavaType(descriptor_))) { - printer->Print(variables_, - "if (!java.util.Arrays.equals(this.$name$, $default$)) {\n"); - } else if (IsReferenceType(GetJavaType(descriptor_))) { - printer->Print(variables_, - "if (!this.$name$.equals($default$)) {\n"); - } else if (IsDefaultNaN(descriptor_)) { - printer->Print(variables_, - "if (!$capitalized_type$.isNaN(this.$name$)) {\n"); - } else { - printer->Print(variables_, - "if (this.$name$ != $default$) {\n"); - } - + GenerateSerializationConditional(printer); printer->Print(variables_, " output.write$capitalized_type$($number$, this.$name$);\n" "}\n"); @@ -362,20 +383,7 @@ GenerateSerializedSizeCode(io::Printer* printer) const { "size += com.google.protobuf.nano.CodedOutputByteBufferNano\n" " .compute$capitalized_type$Size($number$, this.$name$);\n"); } else { - if (IsArrayType(GetJavaType(descriptor_))) { - printer->Print(variables_, - "if (!java.util.Arrays.equals(this.$name$, $default$)) {\n"); - } else if (IsReferenceType(GetJavaType(descriptor_))) { - printer->Print(variables_, - "if (!this.$name$.equals($default$)) {\n"); - } else if (IsDefaultNaN(descriptor_)) { - printer->Print(variables_, - "if (!$capitalized_type$.isNaN(this.$name$)) {\n"); - } else { - printer->Print(variables_, - "if (this.$name$ != $default$) {\n"); - } - + GenerateSerializationConditional(printer); printer->Print(variables_, " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n" " .compute$capitalized_type$Size($number$, this.$name$);\n" diff --git a/src/google/protobuf/compiler/javanano/javanano_primitive_field.h b/src/google/protobuf/compiler/javanano/javanano_primitive_field.h index 9b1bb46..aa6355b 100644 --- a/src/google/protobuf/compiler/javanano/javanano_primitive_field.h +++ b/src/google/protobuf/compiler/javanano/javanano_primitive_field.h @@ -58,6 +58,8 @@ class PrimitiveFieldGenerator : public FieldGenerator { string GetBoxedType() const; private: + void GenerateSerializationConditional(io::Printer* printer) const; + const FieldDescriptor* descriptor_; map<string, string> variables_; |