// Protocol Buffers - Google's data interchange format // Copyright 2008 Google Inc. All rights reserved. // https://developers.google.com/protocol-buffers/ // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef GOOGLE_PROTOBUF_MAP_FIELD_INL_H__ #define GOOGLE_PROTOBUF_MAP_FIELD_INL_H__ #include #ifndef _SHARED_PTR_H #include #endif #include #include #include namespace google { namespace protobuf { namespace internal { // UnwrapMapKey template template T UnwrapMapKey(const MapKey& map_key); template<> inline int32 UnwrapMapKey(const MapKey& map_key) { return map_key.GetInt32Value(); } template<> inline uint32 UnwrapMapKey(const MapKey& map_key) { return map_key.GetUInt32Value(); } template<> inline int64 UnwrapMapKey(const MapKey& map_key) { return map_key.GetInt64Value(); } template<> inline uint64 UnwrapMapKey(const MapKey& map_key) { return map_key.GetUInt64Value(); } template<> inline bool UnwrapMapKey(const MapKey& map_key) { return map_key.GetBoolValue(); } template<> inline string UnwrapMapKey(const MapKey& map_key) { return map_key.GetStringValue(); } // SetMapKey template template inline void SetMapKey(MapKey* map_key, const T& value); template<> inline void SetMapKey(MapKey* map_key, const int32& value) { map_key->SetInt32Value(value); } template<> inline void SetMapKey(MapKey* map_key, const uint32& value) { map_key->SetUInt32Value(value); } template<> inline void SetMapKey(MapKey* map_key, const int64& value) { map_key->SetInt64Value(value); } template<> inline void SetMapKey(MapKey* map_key, const uint64& value) { map_key->SetUInt64Value(value); } template<> inline void SetMapKey(MapKey* map_key, const bool& value) { map_key->SetBoolValue(value); } template<> inline void SetMapKey(MapKey* map_key, const string& value) { map_key->SetStringValue(value); } // ------------------------TypeDefinedMapFieldBase--------------- template typename Map::const_iterator& TypeDefinedMapFieldBase::InternalGetIterator( const MapIterator* map_iter) const { return *reinterpret_cast::const_iterator *>( map_iter->iter_); } template void TypeDefinedMapFieldBase::MapBegin(MapIterator* map_iter) const { InternalGetIterator(map_iter) = GetMap().begin(); SetMapIteratorValue(map_iter); } template void TypeDefinedMapFieldBase::MapEnd(MapIterator* map_iter) const { InternalGetIterator(map_iter) = GetMap().end(); } template bool TypeDefinedMapFieldBase::EqualIterator(const MapIterator& a, const MapIterator& b) const { return InternalGetIterator(&a) == InternalGetIterator(&b); } template void TypeDefinedMapFieldBase::IncreaseIterator(MapIterator* map_iter) const { ++InternalGetIterator(map_iter); SetMapIteratorValue(map_iter); } template void TypeDefinedMapFieldBase::InitializeIterator( MapIterator* map_iter) const { map_iter->iter_ = new typename Map::const_iterator; GOOGLE_CHECK(map_iter->iter_ != NULL); } template void TypeDefinedMapFieldBase::DeleteIterator(MapIterator* map_iter) const { delete reinterpret_cast::const_iterator *>( map_iter->iter_); } template void TypeDefinedMapFieldBase::CopyIterator( MapIterator* this_iter, const MapIterator& that_iter) const { InternalGetIterator(this_iter) = InternalGetIterator(&that_iter); this_iter->key_.SetType(that_iter.key_.type()); // MapValueRef::type() fails when containing data is null. However, if // this_iter points to MapEnd, data can be null. this_iter->value_.SetType( static_cast(that_iter.value_.type_)); SetMapIteratorValue(this_iter); } // ---------------------------------------------------------------------- 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 { MapFieldBase::SyncMapWithRepeatedField(); return MapFieldLiteType::GetInternalMap().size(); } template void MapField::Clear() { MapFieldBase::SyncMapWithRepeatedField(); MapFieldLiteType::MutableInternalMap()->clear(); MapFieldBase::SetMapDirty(); } template void MapField::SetMapIteratorValue( MapIterator* map_iter) const { const Map& map = GetMap(); typename Map::const_iterator iter = TypeDefinedMapFieldBase::InternalGetIterator(map_iter); if (iter == map.end()) return; SetMapKey(&map_iter->key_, iter->first); map_iter->value_.SetValue(&iter->second); } template bool MapField::ContainsMapKey( const MapKey& map_key) const { const Map& map = 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) { // Always use mutable map because users may change the map value by // MapValueRef. Map* map = MutableMap(); const Key& key = UnwrapMapKey(map_key); typename Map::iterator iter = map->find(key); if (map->end() == iter) { val->SetValue(&((*map)[key])); return true; } // Key is already in the map. Make sure (*map)[key] is not called. // [] may reorder the map and iterators. val->SetValue(&(iter->second)); return false; } template 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); MapFieldBase::SyncMapWithRepeatedField(); down_other.SyncMapWithRepeatedField(); MapFieldLiteType::MergeFrom(other); 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(); } template Map* MapField::MutableInternalMap() { return MapFieldLiteType::MutableInternalMap(); } template void MapField::SyncRepeatedFieldWithMapNoLock() const { if (this->MapFieldBase::repeated_field_ == NULL) { if (this->MapFieldBase::arena_ == NULL) { this->MapFieldBase::repeated_field_ = new RepeatedPtrField(); } else { this->MapFieldBase::repeated_field_ = Arena::CreateMessage >( this->MapFieldBase::arena_); } } const Map& map = GetInternalMap(); RepeatedPtrField* repeated_field = reinterpret_cast*>( this->MapFieldBase::repeated_field_); repeated_field->Clear(); 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_)); 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(); RepeatedPtrField* repeated_field = reinterpret_cast*>( this->MapFieldBase::repeated_field_); GOOGLE_CHECK(this->MapFieldBase::repeated_field_ != NULL); map->clear(); for (typename RepeatedPtrField::iterator it = repeated_field->begin(); it != repeated_field->end(); ++it) { // Cast is needed because Map's api and internal storage is different when // value is enum. For enum, we cannot cast an int to enum. Thus, we have to // copy value. For other types, they have same exposed api type and internal // stored type. We should not introduce value copy for them. We achieve this // by casting to value for enum while casting to reference for other types. (*map)[it->key()] = static_cast(it->value()); } } template int MapField::SpaceUsedExcludingSelfNoLock() const { int size = 0; if (this->MapFieldBase::repeated_field_ != NULL) { size += this->MapFieldBase::repeated_field_->SpaceUsedExcludingSelf(); } Map* map = const_cast(this)->MutableInternalMap(); size += sizeof(*map); for (typename Map::iterator it = map->begin(); it != map->end(); ++it) { size += KeyTypeHandler::SpaceUsedInMap(it->first); size += ValueTypeHandler::SpaceUsedInMap(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 } // namespace google #endif // GOOGLE_PROTOBUF_MAP_FIELD_INL_H__