// 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 #include #include #include #include "absl/base/casts.h" #include "google/protobuf/map.h" #include "google/protobuf/map_field.h" #include "google/protobuf/map_type_handler.h" #include "google/protobuf/port.h" // must be last #include "google/protobuf/port_def.inc" #ifdef SWIG #error "You cannot SWIG proto headers" #endif namespace google { namespace protobuf { namespace internal { // UnwrapMapKey template template T UnwrapMapKey(const MapKey& map_key); template <> inline int32_t UnwrapMapKey(const MapKey& map_key) { return map_key.GetInt32Value(); } template <> inline uint32_t UnwrapMapKey(const MapKey& map_key) { return map_key.GetUInt32Value(); } template <> inline int64_t UnwrapMapKey(const MapKey& map_key) { return map_key.GetInt64Value(); } template <> inline uint64_t UnwrapMapKey(const MapKey& map_key) { return map_key.GetUInt64Value(); } template <> inline bool UnwrapMapKey(const MapKey& map_key) { return map_key.GetBoolValue(); } template <> inline std::string UnwrapMapKey(const MapKey& map_key) { return map_key.GetStringValue(); } template <> inline MapKey UnwrapMapKey(const MapKey& map_key) { return map_key; } // SetMapKey inline void SetMapKey(MapKey* map_key, int32_t value) { map_key->SetInt32Value(value); } inline void SetMapKey(MapKey* map_key, uint32_t value) { map_key->SetUInt32Value(value); } inline void SetMapKey(MapKey* map_key, int64_t value) { map_key->SetInt64Value(value); } inline void SetMapKey(MapKey* map_key, uint64_t value) { map_key->SetUInt64Value(value); } inline void SetMapKey(MapKey* map_key, bool value) { map_key->SetBoolValue(value); } inline void SetMapKey(MapKey* map_key, const std::string& value) { map_key->SetStringValue(value); } inline void SetMapKey(MapKey* map_key, const MapKey& value) { map_key->CopyFrom(value); } // ------------------------TypeDefinedMapFieldBase--------------- template void TypeDefinedMapFieldBase::SetMapIteratorValue( MapIterator* map_iter) const { if (map_iter->iter_.Equals(UntypedMapBase::EndIterator())) return; auto iter = typename Map::const_iterator(map_iter->iter_); SetMapKey(&map_iter->key_, iter->first); map_iter->value_.SetValueOrCopy(&iter->second); } template bool TypeDefinedMapFieldBase::ContainsMapKey( const MapKey& map_key) const { return GetMap().contains(UnwrapMapKey(map_key)); } template bool TypeDefinedMapFieldBase::InsertOrLookupMapValueNoSync( const MapKey& map_key, MapValueRef* val) { auto res = map_.try_emplace(UnwrapMapKey(map_key)); val->SetValue(&res.first->second); return res.second; } template bool TypeDefinedMapFieldBase::LookupMapValue( const MapKey& map_key, MapValueConstRef* val) const { const auto& map = GetMap(); auto iter = map.find(UnwrapMapKey(map_key)); if (map.end() == iter) { return false; } val->SetValueOrCopy(&iter->second); return true; } template bool TypeDefinedMapFieldBase::DeleteMapValue(const MapKey& map_key) { return MutableMap()->erase(UnwrapMapKey(map_key)); } template void TypeDefinedMapFieldBase::Swap(MapFieldBase* other) { MapFieldBase::Swap(other); auto* other_field = DownCast(other); map_.Swap(&other_field->map_); } template void TypeDefinedMapFieldBase::MergeFrom(const MapFieldBase& other) { SyncMapWithRepeatedField(); const auto& other_field = static_cast(other); other_field.SyncMapWithRepeatedField(); internal::MapMergeFrom(map_, other_field.map_); SetMapDirty(); } template size_t TypeDefinedMapFieldBase::SpaceUsedExcludingSelfNoLock() const { size_t size = 0; if (auto* p = maybe_payload()) { size += p->repeated_field.SpaceUsedExcludingSelfLong(); } // We can't compile this expression for DynamicMapField even though it is // never used at runtime, so disable it at compile time. std::get, Map>::value>( std::make_tuple( [&](const auto& map) { size += map.SpaceUsedExcludingSelfLong(); }, [](const auto&) {}))(map_); return size; } template void TypeDefinedMapFieldBase::UnsafeShallowSwap(MapFieldBase* other) { InternalSwap(DownCast(other)); } template void TypeDefinedMapFieldBase::InternalSwap( TypeDefinedMapFieldBase* other) { MapFieldBase::InternalSwap(other); map_.InternalSwap(&other->map_); } // ---------------------------------------------------------------------- template const Message* MapField::GetPrototype() const { return Derived::internal_default_instance(); } } // namespace internal } // namespace protobuf } // namespace google #include "google/protobuf/port_undef.inc" #endif // GOOGLE_PROTOBUF_MAP_FIELD_INL_H__