diff options
author | Max Cai <maxtroy@google.com> | 2013-09-20 18:29:40 +0100 |
---|---|---|
committer | Max Cai <maxtroy@google.com> | 2013-09-23 15:27:13 +0100 |
commit | b337f25628ceb3649df5c4e89b5125e7698a9b47 (patch) | |
tree | dd8dc893365bbe59b77a303e163e10532eaf6017 /src/google/protobuf/compiler/javanano/javanano_field.cc | |
parent | ee4410d551c43d800e821ffd220cc69535fef19e (diff) |
Accessor style for optional fields.
This CL implements the 'optional_field_style=accessors' option.
All optional fields will now be 1 Java field and 1 bit in a shared
bitfield behind get/set/has/clear accessor methods. The setter
performs null check for reference types (Strings and byte[]s).
Also decentralized the clear code generation.
Change-Id: I60ac78329e352e76c2f8139fba1f292383080ad3
Diffstat (limited to 'src/google/protobuf/compiler/javanano/javanano_field.cc')
-rw-r--r-- | src/google/protobuf/compiler/javanano/javanano_field.cc | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/src/google/protobuf/compiler/javanano/javanano_field.cc b/src/google/protobuf/compiler/javanano/javanano_field.cc index ea25786c..6629f965 100644 --- a/src/google/protobuf/compiler/javanano/javanano_field.cc +++ b/src/google/protobuf/compiler/javanano/javanano_field.cc @@ -53,16 +53,21 @@ FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor, const Params extension_generators_( new scoped_ptr<FieldGenerator>[descriptor->extension_count()]) { + int next_has_bit_index = 0; // Construct all the FieldGenerators. for (int i = 0; i < descriptor->field_count(); i++) { - field_generators_[i].reset(MakeGenerator(descriptor->field(i), params)); + field_generators_[i].reset( + MakeGenerator(descriptor->field(i), params, &next_has_bit_index)); } for (int i = 0; i < descriptor->extension_count(); i++) { - extension_generators_[i].reset(MakeGenerator(descriptor->extension(i), params)); + extension_generators_[i].reset( + MakeGenerator(descriptor->extension(i), params, &next_has_bit_index)); } + total_bits_ = next_has_bit_index; } -FieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field, const Params ¶ms) { +FieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field, + const Params ¶ms, int* next_has_bit_index) { if (field->is_repeated()) { switch (GetJavaType(field)) { case JAVATYPE_MESSAGE: @@ -72,6 +77,21 @@ FieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field, c default: return new RepeatedPrimitiveFieldGenerator(field, params); } + } else if (params.optional_field_accessors() && field->is_optional()) { + // We need a has-bit for each primitive/enum field because their default + // values could be same as explicitly set values. But we don't need it + // for a message field because they have no defaults and Nano uses 'null' + // for unset messages, which cannot be set explicitly. + switch (GetJavaType(field)) { + case JAVATYPE_MESSAGE: + return new AccessorMessageFieldGenerator(field, params); + case JAVATYPE_ENUM: + return new AccessorEnumFieldGenerator( + field, params, (*next_has_bit_index)++); + default: + return new AccessorPrimitiveFieldGenerator( + field, params, (*next_has_bit_index)++); + } } else { switch (GetJavaType(field)) { case JAVATYPE_MESSAGE: |