From d36c0c538a545fac5d9db6ba65c525246d4efa95 Mon Sep 17 00:00:00 2001 From: Feng Xiao Date: Wed, 29 Mar 2017 14:32:48 -0700 Subject: Down-integrate from google3. --- src/google/protobuf/map_field_inl.h | 290 +++++++++--------------------------- 1 file changed, 71 insertions(+), 219 deletions(-) (limited to 'src/google/protobuf/map_field_inl.h') diff --git a/src/google/protobuf/map_field_inl.h b/src/google/protobuf/map_field_inl.h index 2d84b0a3..8c5da3c6 100644 --- a/src/google/protobuf/map_field_inl.h +++ b/src/google/protobuf/map_field_inl.h @@ -162,81 +162,32 @@ void TypeDefinedMapFieldBase::CopyIterator( // ---------------------------------------------------------------------- -template -MapField::MapField() - : default_entry_(NULL) {} - -template -MapField::MapField( - Arena* arena) - : TypeDefinedMapFieldBase(arena), - MapFieldLite( - arena), - default_entry_(NULL) {} - -template -MapField::MapField( - const Message* default_entry) - : default_entry_(down_cast(default_entry)) {} - -template -MapField::MapField( - Arena* arena, const Message* default_entry) - : TypeDefinedMapFieldBase(arena), - MapFieldLite( - arena), - default_entry_(down_cast(default_entry)) {} - -template -MapField::~MapField() {} - -template -int -MapField::size() const { + WireFormatLite::FieldType kValueFieldType, int default_enum_value> +int MapField::size() const { MapFieldBase::SyncMapWithRepeatedField(); - return MapFieldLiteType::GetInternalMap().size(); + return impl_.GetMap().size(); } -template -void -MapField::Clear() { + WireFormatLite::FieldType kValueFieldType, int default_enum_value> +void MapField::Clear() { MapFieldBase::SyncMapWithRepeatedField(); - MapFieldLiteType::MutableInternalMap()->clear(); + impl_.MutableMap()->clear(); MapFieldBase::SetMapDirty(); } -template -void MapField::SetMapIteratorValue( - MapIterator* map_iter) const { - const Map& map = GetMap(); + WireFormatLite::FieldType kValueFieldType, int default_enum_value> +void MapField::SetMapIteratorValue(MapIterator* map_iter) + const { + const Map& map = impl_.GetMap(); typename Map::const_iterator iter = TypeDefinedMapFieldBase::InternalGetIterator(map_iter); if (iter == map.end()) return; @@ -244,27 +195,23 @@ void MapFieldvalue_.SetValue(&iter->second); } -template -bool MapField::ContainsMapKey( - const MapKey& map_key) const { - const Map& map = GetMap(); + WireFormatLite::FieldType kValueFieldType, int default_enum_value> +bool MapField::ContainsMapKey(const MapKey& map_key) const { + const Map& map = impl_.GetMap(); const Key& key = UnwrapMapKey(map_key); typename Map::const_iterator iter = map.find(key); return iter != map.end(); } -template -bool MapField::InsertOrLookupMapValue( - const MapKey& map_key, - MapValueRef* val) { + WireFormatLite::FieldType kValueFieldType, int default_enum_value> +bool MapField::InsertOrLookupMapValue(const MapKey& map_key, + MapValueRef* val) { // Always use mutable map because users may change the map value by // MapValueRef. Map* map = MutableMap(); @@ -280,118 +227,41 @@ bool MapField -bool MapField::DeleteMapValue( - const MapKey& map_key) { + WireFormatLite::FieldType kValueFieldType, int default_enum_value> +bool MapField::DeleteMapValue(const MapKey& map_key) { const Key& key = UnwrapMapKey(map_key); return MutableMap()->erase(key); } -template -const Map& -MapField::GetMap() const { - MapFieldBase::SyncMapWithRepeatedField(); - return MapFieldLiteType::GetInternalMap(); -} - -template -Map* -MapField::MutableMap() { - MapFieldBase::SyncMapWithRepeatedField(); - Map* result = MapFieldLiteType::MutableInternalMap(); - MapFieldBase::SetMapDirty(); - return result; -} - -template -void -MapField::MergeFrom( - const MapFieldLiteType& other) { - const MapField& down_other = down_cast(other); + WireFormatLite::FieldType kValueFieldType, int default_enum_value> +void MapField::MergeFrom(const MapField& other) { MapFieldBase::SyncMapWithRepeatedField(); - down_other.SyncMapWithRepeatedField(); - MapFieldLiteType::MergeFrom(other); + other.SyncMapWithRepeatedField(); + impl_.MergeFrom(other.impl_); MapFieldBase::SetMapDirty(); } -template -void -MapField::Swap( - MapFieldLiteType* other) { - MapField* down_other = down_cast(other); - std::swap(this->MapFieldBase::repeated_field_, down_other->repeated_field_); - MapFieldLiteType::Swap(other); - std::swap(this->MapFieldBase::state_, down_other->state_); -} - -template -void -MapField::SetEntryDescriptor( - const Descriptor** descriptor) { - this->MapFieldBase::entry_descriptor_ = descriptor; -} - -template -void -MapField::SetAssignDescriptorCallback(void (*callback)()) { - this->MapFieldBase::assign_descriptor_callback_ = callback; -} - -template -const Map& -MapField::GetInternalMap() const { - return MapFieldLiteType::GetInternalMap(); + WireFormatLite::FieldType kValueFieldType, int default_enum_value> +void MapField::Swap(MapField* other) { + std::swap(MapFieldBase::repeated_field_, other->repeated_field_); + impl_.Swap(&other->impl_); + std::swap(MapFieldBase::state_, other->state_); } -template -Map* -MapField::MutableInternalMap() { - return MapFieldLiteType::MutableInternalMap(); -} - -template -void -MapField::SyncRepeatedFieldWithMapNoLock() const { + WireFormatLite::FieldType kValueFieldType, int default_enum_value> +void MapField::SyncRepeatedFieldWithMapNoLock() const { if (this->MapFieldBase::repeated_field_ == NULL) { if (this->MapFieldBase::arena_ == NULL) { this->MapFieldBase::repeated_field_ = new RepeatedPtrField(); @@ -401,33 +271,35 @@ MapFieldMapFieldBase::arena_); } } - const Map& map = GetInternalMap(); + const Map& map = impl_.GetMap(); RepeatedPtrField* repeated_field = reinterpret_cast*>( this->MapFieldBase::repeated_field_); repeated_field->Clear(); + // The only way we can get at this point is through reflection and the + // only way we can get the reflection object is by having called GetReflection + // on the encompassing field. So that type must have existed and hence we + // know that this MapEntry default_type has also already been constructed. + // So it's safe to just call internal_default_instance(). + const Message* default_entry = Derived::internal_default_instance(); for (typename Map::const_iterator it = map.begin(); it != map.end(); ++it) { - InitDefaultEntryOnce(); - GOOGLE_CHECK(default_entry_ != NULL); EntryType* new_entry = - down_cast(default_entry_->New(this->MapFieldBase::arena_)); + down_cast(default_entry->New(this->MapFieldBase::arena_)); repeated_field->AddAllocated(new_entry); (*new_entry->mutable_key()) = it->first; (*new_entry->mutable_value()) = it->second; } } -template -void -MapField::SyncMapWithRepeatedFieldNoLock() const { - Map* map = const_cast(this)->MutableInternalMap(); + WireFormatLite::FieldType kValueFieldType, int default_enum_value> +void MapField::SyncMapWithRepeatedFieldNoLock() const { + Map* map = const_cast(this)->impl_.MutableMap(); RepeatedPtrField* repeated_field = reinterpret_cast*>( this->MapFieldBase::repeated_field_); @@ -444,44 +316,24 @@ MapField -int -MapField::SpaceUsedExcludingSelfNoLock() const { - int size = 0; + WireFormatLite::FieldType kValueFieldType, int default_enum_value> +size_t MapField::SpaceUsedExcludingSelfNoLock() const { + size_t size = 0; if (this->MapFieldBase::repeated_field_ != NULL) { - size += this->MapFieldBase::repeated_field_->SpaceUsedExcludingSelf(); + size += this->MapFieldBase::repeated_field_->SpaceUsedExcludingSelfLong(); } - Map* map = const_cast(this)->MutableInternalMap(); + Map* map = const_cast(this)->impl_.MutableMap(); size += sizeof(*map); - for (typename Map::iterator it = map->begin(); - it != map->end(); ++it) { - size += KeyTypeHandler::SpaceUsedInMap(it->first); - size += ValueTypeHandler::SpaceUsedInMap(it->second); + for (typename Map::iterator it = map->begin(); it != map->end(); + ++it) { + size += KeyTypeHandler::SpaceUsedInMapLong(it->first); + size += ValueTypeHandler::SpaceUsedInMapLong(it->second); } return size; } - -template -void -MapField::InitDefaultEntryOnce() - const { - if (default_entry_ == NULL) { - MapFieldBase::InitMetadataOnce(); - GOOGLE_CHECK(*this->MapFieldBase::entry_descriptor_ != NULL); - default_entry_ = down_cast( - MessageFactory::generated_factory()->GetPrototype( - *this->MapFieldBase::entry_descriptor_)); - } -} - } // namespace internal } // namespace protobuf -- cgit v1.2.3