Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:22

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 #ifndef GOOGLE_PROTOBUF_MAP_FIELD_INL_H__
0009 #define GOOGLE_PROTOBUF_MAP_FIELD_INL_H__
0010 
0011 #include <cstddef>
0012 #include <cstdint>
0013 #include <memory>
0014 #include <string>
0015 #include <tuple>
0016 #include <type_traits>
0017 
0018 #include "absl/base/casts.h"
0019 #include "google/protobuf/map.h"
0020 #include "google/protobuf/map_field.h"
0021 #include "google/protobuf/map_type_handler.h"
0022 #include "google/protobuf/message.h"
0023 #include "google/protobuf/port.h"
0024 
0025 // must be last
0026 #include "google/protobuf/port_def.inc"
0027 
0028 #ifdef SWIG
0029 #error "You cannot SWIG proto headers"
0030 #endif
0031 
0032 namespace google {
0033 namespace protobuf {
0034 namespace internal {
0035 // UnwrapMapKey template. We're using overloading rather than template
0036 // specialization so that we can return a value or reference type depending on
0037 // `T`.
0038 inline int32_t UnwrapMapKeyImpl(const MapKey& map_key, const int32_t*) {
0039   return map_key.GetInt32Value();
0040 }
0041 inline uint32_t UnwrapMapKeyImpl(const MapKey& map_key, const uint32_t*) {
0042   return map_key.GetUInt32Value();
0043 }
0044 inline int64_t UnwrapMapKeyImpl(const MapKey& map_key, const int64_t*) {
0045   return map_key.GetInt64Value();
0046 }
0047 inline uint64_t UnwrapMapKeyImpl(const MapKey& map_key, const uint64_t*) {
0048   return map_key.GetUInt64Value();
0049 }
0050 inline bool UnwrapMapKeyImpl(const MapKey& map_key, const bool*) {
0051   return map_key.GetBoolValue();
0052 }
0053 inline const std::string& UnwrapMapKeyImpl(const MapKey& map_key,
0054                                            const std::string*) {
0055   return map_key.GetStringValue();
0056 }
0057 inline const MapKey& UnwrapMapKeyImpl(const MapKey& map_key, const MapKey*) {
0058   return map_key;
0059 }
0060 
0061 template <typename T>
0062 decltype(auto) UnwrapMapKey(const MapKey& map_key) {
0063   return UnwrapMapKeyImpl(map_key, static_cast<T*>(nullptr));
0064 }
0065 
0066 // SetMapKey
0067 inline void SetMapKey(MapKey* map_key, int32_t value) {
0068   map_key->SetInt32Value(value);
0069 }
0070 inline void SetMapKey(MapKey* map_key, uint32_t value) {
0071   map_key->SetUInt32Value(value);
0072 }
0073 inline void SetMapKey(MapKey* map_key, int64_t value) {
0074   map_key->SetInt64Value(value);
0075 }
0076 inline void SetMapKey(MapKey* map_key, uint64_t value) {
0077   map_key->SetUInt64Value(value);
0078 }
0079 inline void SetMapKey(MapKey* map_key, bool value) {
0080   map_key->SetBoolValue(value);
0081 }
0082 inline void SetMapKey(MapKey* map_key, const std::string& value) {
0083   map_key->SetStringValue(value);
0084 }
0085 inline void SetMapKey(MapKey* map_key, const MapKey& value) {
0086   map_key->CopyFrom(value);
0087 }
0088 
0089 // ------------------------TypeDefinedMapFieldBase---------------
0090 template <typename Key, typename T>
0091 void TypeDefinedMapFieldBase<Key, T>::SetMapIteratorValueImpl(
0092     MapIterator* map_iter) {
0093   if (map_iter->iter_.Equals(UntypedMapBase::EndIterator())) return;
0094   auto iter = typename Map<Key, T>::const_iterator(map_iter->iter_);
0095   SetMapKey(&map_iter->key_, iter->first);
0096   map_iter->value_.SetValueOrCopy(&iter->second);
0097 }
0098 
0099 template <typename Key, typename T>
0100 bool TypeDefinedMapFieldBase<Key, T>::InsertOrLookupMapValueNoSyncImpl(
0101     MapFieldBase& map, const MapKey& map_key, MapValueRef* val) {
0102   auto res = static_cast<TypeDefinedMapFieldBase&>(map).map_.try_emplace(
0103       UnwrapMapKey<Key>(map_key));
0104   val->SetValue(&res.first->second);
0105   return res.second;
0106 }
0107 
0108 template <typename Key, typename T>
0109 bool TypeDefinedMapFieldBase<Key, T>::LookupMapValueImpl(
0110     const MapFieldBase& self, const MapKey& map_key, MapValueConstRef* val) {
0111   const auto& map = static_cast<const TypeDefinedMapFieldBase&>(self).GetMap();
0112   auto iter = map.find(UnwrapMapKey<Key>(map_key));
0113   if (map.end() == iter) {
0114     return false;
0115   }
0116   if (val != nullptr) {
0117     val->SetValueOrCopy(&iter->second);
0118   }
0119   return true;
0120 }
0121 
0122 template <typename Key, typename T>
0123 bool TypeDefinedMapFieldBase<Key, T>::DeleteMapValueImpl(
0124     MapFieldBase& map, const MapKey& map_key) {
0125   return static_cast<TypeDefinedMapFieldBase&>(map).MutableMap()->erase(
0126       UnwrapMapKey<Key>(map_key));
0127 }
0128 
0129 template <typename Key, typename T>
0130 void TypeDefinedMapFieldBase<Key, T>::SwapImpl(MapFieldBase& lhs,
0131                                                MapFieldBase& rhs) {
0132   MapFieldBase::SwapImpl(lhs, rhs);
0133   static_cast<TypeDefinedMapFieldBase&>(lhs).map_.swap(
0134       static_cast<TypeDefinedMapFieldBase&>(rhs).map_);
0135 }
0136 
0137 template <typename Key, typename T>
0138 void TypeDefinedMapFieldBase<Key, T>::MergeFromImpl(MapFieldBase& base,
0139                                                     const MapFieldBase& other) {
0140   auto& self = static_cast<TypeDefinedMapFieldBase&>(base);
0141   self.SyncMapWithRepeatedField();
0142   const auto& other_field = static_cast<const TypeDefinedMapFieldBase&>(other);
0143   other_field.SyncMapWithRepeatedField();
0144   internal::MapMergeFrom(self.map_, other_field.map_);
0145   self.SetMapDirty();
0146 }
0147 
0148 template <typename Key, typename T>
0149 size_t TypeDefinedMapFieldBase<Key, T>::SpaceUsedExcludingSelfNoLockImpl(
0150     const MapFieldBase& map) {
0151   auto& self = static_cast<const TypeDefinedMapFieldBase&>(map);
0152   size_t size = 0;
0153   if (auto* p = self.maybe_payload()) {
0154     size += p->repeated_field.SpaceUsedExcludingSelfLong();
0155   }
0156   // We can't compile this expression for DynamicMapField even though it is
0157   // never used at runtime, so disable it at compile time.
0158   std::get<std::is_same<Map<Key, T>, Map<MapKey, MapValueRef>>::value>(
0159       std::make_tuple(
0160           [&](const auto& map) { size += map.SpaceUsedExcludingSelfLong(); },
0161           [](const auto&) {}))(self.map_);
0162   return size;
0163 }
0164 
0165 template <typename Key, typename T>
0166 void TypeDefinedMapFieldBase<Key, T>::UnsafeShallowSwapImpl(MapFieldBase& lhs,
0167                                                             MapFieldBase& rhs) {
0168   static_cast<TypeDefinedMapFieldBase&>(lhs).InternalSwap(
0169       static_cast<TypeDefinedMapFieldBase*>(&rhs));
0170 }
0171 
0172 template <typename Key, typename T>
0173 void TypeDefinedMapFieldBase<Key, T>::InternalSwap(
0174     TypeDefinedMapFieldBase* other) {
0175   MapFieldBase::InternalSwap(other);
0176   map_.InternalSwap(&other->map_);
0177 }
0178 
0179 // ----------------------------------------------------------------------
0180 
0181 template <typename Derived, typename Key, typename T,
0182           WireFormatLite::FieldType kKeyFieldType,
0183           WireFormatLite::FieldType kValueFieldType>
0184 const Message*
0185 MapField<Derived, Key, T, kKeyFieldType, kValueFieldType>::GetPrototypeImpl(
0186     const MapFieldBase&) {
0187   return Derived::internal_default_instance();
0188 }
0189 
0190 }  // namespace internal
0191 }  // namespace protobuf
0192 }  // namespace google
0193 
0194 #include "google/protobuf/port_undef.inc"
0195 
0196 #endif  // GOOGLE_PROTOBUF_MAP_FIELD_INL_H__