• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #ifndef GOOGLE_PROTOBUF_MAP_FIELD_INL_H__
9 #define GOOGLE_PROTOBUF_MAP_FIELD_INL_H__
10 
11 #include <cstddef>
12 #include <cstdint>
13 #include <memory>
14 #include <string>
15 #include <tuple>
16 #include <type_traits>
17 
18 #include "absl/base/casts.h"
19 #include "absl/strings/string_view.h"
20 #include "google/protobuf/map.h"
21 #include "google/protobuf/map_field.h"
22 #include "google/protobuf/map_type_handler.h"
23 #include "google/protobuf/message.h"
24 #include "google/protobuf/port.h"
25 
26 // must be last
27 #include "google/protobuf/port_def.inc"
28 
29 #ifdef SWIG
30 #error "You cannot SWIG proto headers"
31 #endif
32 
33 namespace google {
34 namespace protobuf {
35 namespace internal {
36 // UnwrapMapKey template. We're using overloading rather than template
37 // specialization so that we can return a value or reference type depending on
38 // `T`.
UnwrapMapKeyImpl(const MapKey & map_key,const int32_t *)39 inline int32_t UnwrapMapKeyImpl(const MapKey& map_key, const int32_t*) {
40   return map_key.GetInt32Value();
41 }
UnwrapMapKeyImpl(const MapKey & map_key,const uint32_t *)42 inline uint32_t UnwrapMapKeyImpl(const MapKey& map_key, const uint32_t*) {
43   return map_key.GetUInt32Value();
44 }
UnwrapMapKeyImpl(const MapKey & map_key,const int64_t *)45 inline int64_t UnwrapMapKeyImpl(const MapKey& map_key, const int64_t*) {
46   return map_key.GetInt64Value();
47 }
UnwrapMapKeyImpl(const MapKey & map_key,const uint64_t *)48 inline uint64_t UnwrapMapKeyImpl(const MapKey& map_key, const uint64_t*) {
49   return map_key.GetUInt64Value();
50 }
UnwrapMapKeyImpl(const MapKey & map_key,const bool *)51 inline bool UnwrapMapKeyImpl(const MapKey& map_key, const bool*) {
52   return map_key.GetBoolValue();
53 }
UnwrapMapKeyImpl(const MapKey & map_key,const std::string *)54 inline absl::string_view UnwrapMapKeyImpl(const MapKey& map_key,
55                                           const std::string*) {
56   return map_key.GetStringValue();
57 }
UnwrapMapKeyImpl(const MapKey & map_key,const MapKey *)58 inline const MapKey& UnwrapMapKeyImpl(const MapKey& map_key, const MapKey*) {
59   return map_key;
60 }
61 
62 template <typename T>
decltype(auto)63 decltype(auto) UnwrapMapKey(const MapKey& map_key) {
64   return UnwrapMapKeyImpl(map_key, static_cast<T*>(nullptr));
65 }
66 
67 // SetMapKey
SetMapKey(MapKey * map_key,int32_t value)68 inline void SetMapKey(MapKey* map_key, int32_t value) {
69   map_key->SetInt32Value(value);
70 }
SetMapKey(MapKey * map_key,uint32_t value)71 inline void SetMapKey(MapKey* map_key, uint32_t value) {
72   map_key->SetUInt32Value(value);
73 }
SetMapKey(MapKey * map_key,int64_t value)74 inline void SetMapKey(MapKey* map_key, int64_t value) {
75   map_key->SetInt64Value(value);
76 }
SetMapKey(MapKey * map_key,uint64_t value)77 inline void SetMapKey(MapKey* map_key, uint64_t value) {
78   map_key->SetUInt64Value(value);
79 }
SetMapKey(MapKey * map_key,bool value)80 inline void SetMapKey(MapKey* map_key, bool value) {
81   map_key->SetBoolValue(value);
82 }
SetMapKey(MapKey * map_key,const std::string & value)83 inline void SetMapKey(MapKey* map_key, const std::string& value) {
84   map_key->SetStringValue(value);
85 }
SetMapKey(MapKey * map_key,const MapKey & value)86 inline void SetMapKey(MapKey* map_key, const MapKey& value) {
87   map_key->CopyFrom(value);
88 }
89 
90 // ------------------------TypeDefinedMapFieldBase---------------
91 template <typename Key, typename T>
SetMapIteratorValueImpl(MapIterator * map_iter)92 void TypeDefinedMapFieldBase<Key, T>::SetMapIteratorValueImpl(
93     MapIterator* map_iter) {
94   if (map_iter->iter_.Equals(UntypedMapBase::EndIterator())) return;
95   auto iter = typename Map<Key, T>::const_iterator(map_iter->iter_);
96   SetMapKey(&map_iter->key_, iter->first);
97   map_iter->value_.SetValueOrCopy(&iter->second);
98 }
99 
100 template <typename Key, typename T>
InsertOrLookupMapValueNoSyncImpl(MapFieldBase & map,const MapKey & map_key,MapValueRef * val)101 bool TypeDefinedMapFieldBase<Key, T>::InsertOrLookupMapValueNoSyncImpl(
102     MapFieldBase& map, const MapKey& map_key, MapValueRef* val) {
103   auto res = static_cast<TypeDefinedMapFieldBase&>(map).map_.try_emplace(
104       UnwrapMapKey<Key>(map_key));
105   val->SetValue(&res.first->second);
106   return res.second;
107 }
108 
109 template <typename Key, typename T>
LookupMapValueImpl(const MapFieldBase & self,const MapKey & map_key,MapValueConstRef * val)110 bool TypeDefinedMapFieldBase<Key, T>::LookupMapValueImpl(
111     const MapFieldBase& self, const MapKey& map_key, MapValueConstRef* val) {
112   const auto& map = static_cast<const TypeDefinedMapFieldBase&>(self).GetMap();
113   auto iter = map.find(UnwrapMapKey<Key>(map_key));
114   if (map.end() == iter) {
115     return false;
116   }
117   if (val != nullptr) {
118     val->SetValueOrCopy(&iter->second);
119   }
120   return true;
121 }
122 
123 template <typename Key, typename T>
DeleteMapValueImpl(MapFieldBase & map,const MapKey & map_key)124 bool TypeDefinedMapFieldBase<Key, T>::DeleteMapValueImpl(
125     MapFieldBase& map, const MapKey& map_key) {
126   return static_cast<TypeDefinedMapFieldBase&>(map).MutableMap()->erase(
127       UnwrapMapKey<Key>(map_key));
128 }
129 
130 template <typename Key, typename T>
SwapImpl(MapFieldBase & lhs,MapFieldBase & rhs)131 void TypeDefinedMapFieldBase<Key, T>::SwapImpl(MapFieldBase& lhs,
132                                                MapFieldBase& rhs) {
133   MapFieldBase::SwapImpl(lhs, rhs);
134   static_cast<TypeDefinedMapFieldBase&>(lhs).map_.swap(
135       static_cast<TypeDefinedMapFieldBase&>(rhs).map_);
136 }
137 
138 template <typename Key, typename T>
MergeFromImpl(MapFieldBase & base,const MapFieldBase & other)139 void TypeDefinedMapFieldBase<Key, T>::MergeFromImpl(MapFieldBase& base,
140                                                     const MapFieldBase& other) {
141   auto& self = static_cast<TypeDefinedMapFieldBase&>(base);
142   self.SyncMapWithRepeatedField();
143   const auto& other_field = static_cast<const TypeDefinedMapFieldBase&>(other);
144   other_field.SyncMapWithRepeatedField();
145   internal::MapMergeFrom(self.map_, other_field.map_);
146   self.SetMapDirty();
147 }
148 
149 template <typename Key, typename T>
SpaceUsedExcludingSelfNoLockImpl(const MapFieldBase & map)150 size_t TypeDefinedMapFieldBase<Key, T>::SpaceUsedExcludingSelfNoLockImpl(
151     const MapFieldBase& map) {
152   auto& self = static_cast<const TypeDefinedMapFieldBase&>(map);
153   size_t size = 0;
154   if (auto* p = self.maybe_payload()) {
155     size += p->repeated_field.SpaceUsedExcludingSelfLong();
156   }
157   // We can't compile this expression for DynamicMapField even though it is
158   // never used at runtime, so disable it at compile time.
159   std::get<std::is_same<Map<Key, T>, Map<MapKey, MapValueRef>>::value>(
160       std::make_tuple(
161           [&](const auto& map) { size += map.SpaceUsedExcludingSelfLong(); },
162           [](const auto&) {}))(self.map_);
163   return size;
164 }
165 
166 template <typename Key, typename T>
UnsafeShallowSwapImpl(MapFieldBase & lhs,MapFieldBase & rhs)167 void TypeDefinedMapFieldBase<Key, T>::UnsafeShallowSwapImpl(MapFieldBase& lhs,
168                                                             MapFieldBase& rhs) {
169   static_cast<TypeDefinedMapFieldBase&>(lhs).InternalSwap(
170       static_cast<TypeDefinedMapFieldBase*>(&rhs));
171 }
172 
173 template <typename Key, typename T>
InternalSwap(TypeDefinedMapFieldBase * other)174 void TypeDefinedMapFieldBase<Key, T>::InternalSwap(
175     TypeDefinedMapFieldBase* other) {
176   MapFieldBase::InternalSwap(other);
177   map_.InternalSwap(&other->map_);
178 }
179 
180 // ----------------------------------------------------------------------
181 
182 template <typename Derived, typename Key, typename T,
183           WireFormatLite::FieldType kKeyFieldType,
184           WireFormatLite::FieldType kValueFieldType>
185 const Message*
GetPrototypeImpl(const MapFieldBase &)186 MapField<Derived, Key, T, kKeyFieldType, kValueFieldType>::GetPrototypeImpl(
187     const MapFieldBase&) {
188   return Derived::internal_default_instance();
189 }
190 
191 }  // namespace internal
192 }  // namespace protobuf
193 }  // namespace google
194 
195 #include "google/protobuf/port_undef.inc"
196 
197 #endif  // GOOGLE_PROTOBUF_MAP_FIELD_INL_H__
198