diff options
Diffstat (limited to 'src/google/protobuf/map_field.cc')
-rw-r--r-- | src/google/protobuf/map_field.cc | 121 |
1 files changed, 65 insertions, 56 deletions
diff --git a/src/google/protobuf/map_field.cc b/src/google/protobuf/map_field.cc index ac29c7e9..2ce54cb3 100644 --- a/src/google/protobuf/map_field.cc +++ b/src/google/protobuf/map_field.cc @@ -43,15 +43,13 @@ MapFieldBase::~MapFieldBase() { const RepeatedPtrFieldBase& MapFieldBase::GetRepeatedField() const { SyncRepeatedFieldWithMap(); - return *reinterpret_cast<::google::protobuf::internal::RepeatedPtrFieldBase*>( - repeated_field_); + return *reinterpret_cast<RepeatedPtrFieldBase*>(repeated_field_); } RepeatedPtrFieldBase* MapFieldBase::MutableRepeatedField() { SyncRepeatedFieldWithMap(); SetRepeatedDirty(); - return reinterpret_cast<::google::protobuf::internal::RepeatedPtrFieldBase*>( - repeated_field_); + return reinterpret_cast<RepeatedPtrFieldBase*>(repeated_field_); } size_t MapFieldBase::SpaceUsedExcludingSelfLong() const { @@ -179,12 +177,12 @@ bool DynamicMapField::InsertOrLookupMapValue( // Allocate memory for the inserted MapValueRef, and initialize to // default value. switch (val_des->cpp_type()) { -#define HANDLE_TYPE(CPPTYPE, TYPE) \ - case google::protobuf::FieldDescriptor::CPPTYPE_##CPPTYPE: { \ - TYPE * value = new TYPE(); \ - map_val.SetValue(value); \ - break; \ - } +#define HANDLE_TYPE(CPPTYPE, TYPE) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: { \ + TYPE* value = new TYPE(); \ + map_val.SetValue(value); \ + break; \ + } HANDLE_TYPE(INT32, int32); HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); @@ -195,7 +193,7 @@ bool DynamicMapField::InsertOrLookupMapValue( HANDLE_TYPE(STRING, string); HANDLE_TYPE(ENUM, int32); #undef HANDLE_TYPE - case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: { + case FieldDescriptor::CPPTYPE_MESSAGE: { const Message& message = default_entry_->GetReflection()->GetMessage( *default_entry_, val_des); Message* value = message.New(); @@ -245,6 +243,17 @@ void DynamicMapField::SetMapIteratorValue(MapIterator* map_iter) const { map_iter->value_.CopyFrom(iter->second); } +void DynamicMapField::Swap(MapFieldBase* other) { + DynamicMapField* other_field = down_cast<DynamicMapField*>(other); + std::swap(this->MapFieldBase::repeated_field_, other_field->repeated_field_); + map_.swap(other_field->map_); + // a relaxed swap of the atomic + auto other_state = other_field->state_.load(std::memory_order_relaxed); + auto this_state = this->MapFieldBase::state_.load(std::memory_order_relaxed); + other_field->state_.store(this_state, std::memory_order_relaxed); + this->MapFieldBase::state_.store(other_state, std::memory_order_relaxed); +} + void DynamicMapField::SyncRepeatedFieldWithMapNoLock() const { const Reflection* reflection = default_entry_->GetReflection(); const FieldDescriptor* key_des = @@ -269,61 +278,61 @@ void DynamicMapField::SyncRepeatedFieldWithMapNoLock() const { MapFieldBase::repeated_field_->AddAllocated(new_entry); const MapKey& map_key = it->first; switch (key_des->cpp_type()) { - case google::protobuf::FieldDescriptor::CPPTYPE_STRING: + case FieldDescriptor::CPPTYPE_STRING: reflection->SetString(new_entry, key_des, map_key.GetStringValue()); break; - case google::protobuf::FieldDescriptor::CPPTYPE_INT64: + case FieldDescriptor::CPPTYPE_INT64: reflection->SetInt64(new_entry, key_des, map_key.GetInt64Value()); break; - case google::protobuf::FieldDescriptor::CPPTYPE_INT32: + case FieldDescriptor::CPPTYPE_INT32: reflection->SetInt32(new_entry, key_des, map_key.GetInt32Value()); break; - case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: + case FieldDescriptor::CPPTYPE_UINT64: reflection->SetUInt64(new_entry, key_des, map_key.GetUInt64Value()); break; - case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: + case FieldDescriptor::CPPTYPE_UINT32: reflection->SetUInt32(new_entry, key_des, map_key.GetUInt32Value()); break; - case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: + case FieldDescriptor::CPPTYPE_BOOL: reflection->SetBool(new_entry, key_des, map_key.GetBoolValue()); break; - case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: - case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: - case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: - case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: + case FieldDescriptor::CPPTYPE_DOUBLE: + case FieldDescriptor::CPPTYPE_FLOAT: + case FieldDescriptor::CPPTYPE_ENUM: + case FieldDescriptor::CPPTYPE_MESSAGE: GOOGLE_LOG(FATAL) << "Can't get here."; break; } const MapValueRef& map_val = it->second; switch (val_des->cpp_type()) { - case google::protobuf::FieldDescriptor::CPPTYPE_STRING: + case FieldDescriptor::CPPTYPE_STRING: reflection->SetString(new_entry, val_des, map_val.GetStringValue()); break; - case google::protobuf::FieldDescriptor::CPPTYPE_INT64: + case FieldDescriptor::CPPTYPE_INT64: reflection->SetInt64(new_entry, val_des, map_val.GetInt64Value()); break; - case google::protobuf::FieldDescriptor::CPPTYPE_INT32: + case FieldDescriptor::CPPTYPE_INT32: reflection->SetInt32(new_entry, val_des, map_val.GetInt32Value()); break; - case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: + case FieldDescriptor::CPPTYPE_UINT64: reflection->SetUInt64(new_entry, val_des, map_val.GetUInt64Value()); break; - case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: + case FieldDescriptor::CPPTYPE_UINT32: reflection->SetUInt32(new_entry, val_des, map_val.GetUInt32Value()); break; - case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: + case FieldDescriptor::CPPTYPE_BOOL: reflection->SetBool(new_entry, val_des, map_val.GetBoolValue()); break; - case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: + case FieldDescriptor::CPPTYPE_DOUBLE: reflection->SetDouble(new_entry, val_des, map_val.GetDoubleValue()); break; - case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: + case FieldDescriptor::CPPTYPE_FLOAT: reflection->SetFloat(new_entry, val_des, map_val.GetFloatValue()); break; - case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: + case FieldDescriptor::CPPTYPE_ENUM: reflection->SetEnumValue(new_entry, val_des, map_val.GetEnumValue()); break; - case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: { + case FieldDescriptor::CPPTYPE_MESSAGE: { const Message& message = map_val.GetMessageValue(); reflection->MutableMessage(new_entry, val_des)->CopyFrom(message); break; @@ -351,28 +360,28 @@ void DynamicMapField::SyncMapWithRepeatedFieldNoLock() const { it != MapFieldBase::repeated_field_->end(); ++it) { MapKey map_key; switch (key_des->cpp_type()) { - case google::protobuf::FieldDescriptor::CPPTYPE_STRING: + case FieldDescriptor::CPPTYPE_STRING: map_key.SetStringValue(reflection->GetString(*it, key_des)); break; - case google::protobuf::FieldDescriptor::CPPTYPE_INT64: + case FieldDescriptor::CPPTYPE_INT64: map_key.SetInt64Value(reflection->GetInt64(*it, key_des)); break; - case google::protobuf::FieldDescriptor::CPPTYPE_INT32: + case FieldDescriptor::CPPTYPE_INT32: map_key.SetInt32Value(reflection->GetInt32(*it, key_des)); break; - case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: + case FieldDescriptor::CPPTYPE_UINT64: map_key.SetUInt64Value(reflection->GetUInt64(*it, key_des)); break; - case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: + case FieldDescriptor::CPPTYPE_UINT32: map_key.SetUInt32Value(reflection->GetUInt32(*it, key_des)); break; - case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: + case FieldDescriptor::CPPTYPE_BOOL: map_key.SetBoolValue(reflection->GetBool(*it, key_des)); break; - case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: - case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: - case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: - case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: + case FieldDescriptor::CPPTYPE_DOUBLE: + case FieldDescriptor::CPPTYPE_FLOAT: + case FieldDescriptor::CPPTYPE_ENUM: + case FieldDescriptor::CPPTYPE_MESSAGE: GOOGLE_LOG(FATAL) << "Can't get here."; break; } @@ -386,13 +395,13 @@ void DynamicMapField::SyncMapWithRepeatedFieldNoLock() const { MapValueRef& map_val = (*map)[map_key]; map_val.SetType(val_des->cpp_type()); switch (val_des->cpp_type()) { -#define HANDLE_TYPE(CPPTYPE, TYPE, METHOD) \ - case google::protobuf::FieldDescriptor::CPPTYPE_##CPPTYPE: { \ - TYPE * value = new TYPE; \ - *value = reflection->Get##METHOD(*it, val_des); \ - map_val.SetValue(value); \ - break; \ - } +#define HANDLE_TYPE(CPPTYPE, TYPE, METHOD) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: { \ + TYPE* value = new TYPE; \ + *value = reflection->Get##METHOD(*it, val_des); \ + map_val.SetValue(value); \ + break; \ + } HANDLE_TYPE(INT32, int32, Int32); HANDLE_TYPE(INT64, int64, Int64); HANDLE_TYPE(UINT32, uint32, UInt32); @@ -403,7 +412,7 @@ void DynamicMapField::SyncMapWithRepeatedFieldNoLock() const { HANDLE_TYPE(STRING, string, String); HANDLE_TYPE(ENUM, int32, EnumValue); #undef HANDLE_TYPE - case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: { + case FieldDescriptor::CPPTYPE_MESSAGE: { const Message& message = reflection->GetMessage(*it, val_des); Message* value = message.New(); value->CopyFrom(message); @@ -426,16 +435,16 @@ size_t DynamicMapField::SpaceUsedExcludingSelfNoLock() const { size += sizeof(it->first) * map_size; size += sizeof(it->second) * map_size; // If key is string, add the allocated space. - if (it->first.type() == google::protobuf::FieldDescriptor::CPPTYPE_STRING) { + if (it->first.type() == FieldDescriptor::CPPTYPE_STRING) { size += sizeof(string) * map_size; } // Add the allocated space in MapValueRef. switch (it->second.type()) { -#define HANDLE_TYPE(CPPTYPE, TYPE) \ - case google::protobuf::FieldDescriptor::CPPTYPE_##CPPTYPE: { \ - size += sizeof(TYPE) * map_size; \ - break; \ - } +#define HANDLE_TYPE(CPPTYPE, TYPE) \ + case FieldDescriptor::CPPTYPE_##CPPTYPE: { \ + size += sizeof(TYPE) * map_size; \ + break; \ + } HANDLE_TYPE(INT32, int32); HANDLE_TYPE(INT64, int64); HANDLE_TYPE(UINT32, uint32); @@ -446,7 +455,7 @@ size_t DynamicMapField::SpaceUsedExcludingSelfNoLock() const { HANDLE_TYPE(STRING, string); HANDLE_TYPE(ENUM, int32); #undef HANDLE_TYPE - case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: { + case FieldDescriptor::CPPTYPE_MESSAGE: { while (it != map_.end()) { const Message& message = it->second.GetMessageValue(); size += message.GetReflection()->SpaceUsedLong(message); |