1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #ifndef GOOGLE_PROTOBUF_MAP_FIELD_INL_H__
32 #define GOOGLE_PROTOBUF_MAP_FIELD_INL_H__
33
34 #include <memory>
35
36 #include <google/protobuf/stubs/casts.h>
37 #include <google/protobuf/map.h>
38 #include <google/protobuf/map_field.h>
39 #include <google/protobuf/map_type_handler.h>
40
41 #ifdef SWIG
42 #error "You cannot SWIG proto headers"
43 #endif
44
45 namespace google {
46 namespace protobuf {
47 namespace internal {
48 // UnwrapMapKey template
49 template <typename T>
50 T UnwrapMapKey(const MapKey& map_key);
51 template <>
52 inline int32 UnwrapMapKey<int32>(const MapKey& map_key) {
53 return map_key.GetInt32Value();
54 }
55 template <>
56 inline uint32 UnwrapMapKey<uint32>(const MapKey& map_key) {
57 return map_key.GetUInt32Value();
58 }
59 template <>
60 inline int64 UnwrapMapKey<int64>(const MapKey& map_key) {
61 return map_key.GetInt64Value();
62 }
63 template <>
64 inline uint64 UnwrapMapKey<uint64>(const MapKey& map_key) {
65 return map_key.GetUInt64Value();
66 }
67 template <>
68 inline bool UnwrapMapKey<bool>(const MapKey& map_key) {
69 return map_key.GetBoolValue();
70 }
71 template <>
72 inline std::string UnwrapMapKey<std::string>(const MapKey& map_key) {
73 return map_key.GetStringValue();
74 }
75
76 // SetMapKey template
77 template <typename T>
78 inline void SetMapKey(MapKey* map_key, const T& value);
79 template <>
80 inline void SetMapKey<int32>(MapKey* map_key, const int32& value) {
81 map_key->SetInt32Value(value);
82 }
83 template <>
84 inline void SetMapKey<uint32>(MapKey* map_key, const uint32& value) {
85 map_key->SetUInt32Value(value);
86 }
87 template <>
88 inline void SetMapKey<int64>(MapKey* map_key, const int64& value) {
89 map_key->SetInt64Value(value);
90 }
91 template <>
92 inline void SetMapKey<uint64>(MapKey* map_key, const uint64& value) {
93 map_key->SetUInt64Value(value);
94 }
95 template <>
96 inline void SetMapKey<bool>(MapKey* map_key, const bool& value) {
97 map_key->SetBoolValue(value);
98 }
99 template <>
100 inline void SetMapKey<std::string>(MapKey* map_key, const std::string& value) {
101 map_key->SetStringValue(value);
102 }
103
104 // ------------------------TypeDefinedMapFieldBase---------------
105 template <typename Key, typename T>
106 typename Map<Key, T>::const_iterator&
InternalGetIterator(const MapIterator * map_iter)107 TypeDefinedMapFieldBase<Key, T>::InternalGetIterator(
108 const MapIterator* map_iter) const {
109 return *reinterpret_cast<typename Map<Key, T>::const_iterator*>(
110 map_iter->iter_);
111 }
112
113 template <typename Key, typename T>
MapBegin(MapIterator * map_iter)114 void TypeDefinedMapFieldBase<Key, T>::MapBegin(MapIterator* map_iter) const {
115 InternalGetIterator(map_iter) = GetMap().begin();
116 SetMapIteratorValue(map_iter);
117 }
118
119 template <typename Key, typename T>
MapEnd(MapIterator * map_iter)120 void TypeDefinedMapFieldBase<Key, T>::MapEnd(MapIterator* map_iter) const {
121 InternalGetIterator(map_iter) = GetMap().end();
122 }
123
124 template <typename Key, typename T>
EqualIterator(const MapIterator & a,const MapIterator & b)125 bool TypeDefinedMapFieldBase<Key, T>::EqualIterator(
126 const MapIterator& a, const MapIterator& b) const {
127 return InternalGetIterator(&a) == InternalGetIterator(&b);
128 }
129
130 template <typename Key, typename T>
IncreaseIterator(MapIterator * map_iter)131 void TypeDefinedMapFieldBase<Key, T>::IncreaseIterator(
132 MapIterator* map_iter) const {
133 ++InternalGetIterator(map_iter);
134 SetMapIteratorValue(map_iter);
135 }
136
137 template <typename Key, typename T>
InitializeIterator(MapIterator * map_iter)138 void TypeDefinedMapFieldBase<Key, T>::InitializeIterator(
139 MapIterator* map_iter) const {
140 map_iter->iter_ = new typename Map<Key, T>::const_iterator;
141 GOOGLE_CHECK(map_iter->iter_ != NULL);
142 }
143
144 template <typename Key, typename T>
DeleteIterator(MapIterator * map_iter)145 void TypeDefinedMapFieldBase<Key, T>::DeleteIterator(
146 MapIterator* map_iter) const {
147 delete reinterpret_cast<typename Map<Key, T>::const_iterator*>(
148 map_iter->iter_);
149 }
150
151 template <typename Key, typename T>
CopyIterator(MapIterator * this_iter,const MapIterator & that_iter)152 void TypeDefinedMapFieldBase<Key, T>::CopyIterator(
153 MapIterator* this_iter, const MapIterator& that_iter) const {
154 InternalGetIterator(this_iter) = InternalGetIterator(&that_iter);
155 this_iter->key_.SetType(that_iter.key_.type());
156 // MapValueRef::type() fails when containing data is null. However, if
157 // this_iter points to MapEnd, data can be null.
158 this_iter->value_.SetType(
159 static_cast<FieldDescriptor::CppType>(that_iter.value_.type_));
160 SetMapIteratorValue(this_iter);
161 }
162
163 // ----------------------------------------------------------------------
164
165 template <typename Derived, typename Key, typename T,
166 WireFormatLite::FieldType kKeyFieldType,
167 WireFormatLite::FieldType kValueFieldType, int default_enum_value>
168 int MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
size()169 default_enum_value>::size() const {
170 MapFieldBase::SyncMapWithRepeatedField();
171 return static_cast<int>(impl_.GetMap().size());
172 }
173
174 template <typename Derived, typename Key, typename T,
175 WireFormatLite::FieldType kKeyFieldType,
176 WireFormatLite::FieldType kValueFieldType, int default_enum_value>
177 void MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
Clear()178 default_enum_value>::Clear() {
179 if (this->MapFieldBase::repeated_field_ != nullptr) {
180 RepeatedPtrField<EntryType>* repeated_field =
181 reinterpret_cast<RepeatedPtrField<EntryType>*>(
182 this->MapFieldBase::repeated_field_);
183 repeated_field->Clear();
184 }
185
186 impl_.MutableMap()->clear();
187 // Data in map and repeated field are both empty, but we can't set status
188 // CLEAN. Because clear is a generated API, we cannot invalidate previous
189 // reference to map.
190 MapFieldBase::SetMapDirty();
191 }
192
193 template <typename Derived, typename Key, typename T,
194 WireFormatLite::FieldType kKeyFieldType,
195 WireFormatLite::FieldType kValueFieldType, int default_enum_value>
196 void MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
SetMapIteratorValue(MapIterator * map_iter)197 default_enum_value>::SetMapIteratorValue(MapIterator* map_iter)
198 const {
199 const Map<Key, T>& map = impl_.GetMap();
200 typename Map<Key, T>::const_iterator iter =
201 TypeDefinedMapFieldBase<Key, T>::InternalGetIterator(map_iter);
202 if (iter == map.end()) return;
203 SetMapKey(&map_iter->key_, iter->first);
204 map_iter->value_.SetValue(&iter->second);
205 }
206
207 template <typename Derived, typename Key, typename T,
208 WireFormatLite::FieldType kKeyFieldType,
209 WireFormatLite::FieldType kValueFieldType, int default_enum_value>
210 bool MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
ContainsMapKey(const MapKey & map_key)211 default_enum_value>::ContainsMapKey(const MapKey& map_key) const {
212 const Map<Key, T>& map = impl_.GetMap();
213 const Key& key = UnwrapMapKey<Key>(map_key);
214 typename Map<Key, T>::const_iterator iter = map.find(key);
215 return iter != map.end();
216 }
217
218 template <typename Derived, typename Key, typename T,
219 WireFormatLite::FieldType kKeyFieldType,
220 WireFormatLite::FieldType kValueFieldType, int default_enum_value>
221 bool MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
InsertOrLookupMapValue(const MapKey & map_key,MapValueRef * val)222 default_enum_value>::InsertOrLookupMapValue(const MapKey& map_key,
223 MapValueRef* val) {
224 // Always use mutable map because users may change the map value by
225 // MapValueRef.
226 Map<Key, T>* map = MutableMap();
227 const Key& key = UnwrapMapKey<Key>(map_key);
228 typename Map<Key, T>::iterator iter = map->find(key);
229 if (map->end() == iter) {
230 val->SetValue(&((*map)[key]));
231 return true;
232 }
233 // Key is already in the map. Make sure (*map)[key] is not called.
234 // [] may reorder the map and iterators.
235 val->SetValue(&(iter->second));
236 return false;
237 }
238
239 template <typename Derived, typename Key, typename T,
240 WireFormatLite::FieldType kKeyFieldType,
241 WireFormatLite::FieldType kValueFieldType, int default_enum_value>
242 bool MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
DeleteMapValue(const MapKey & map_key)243 default_enum_value>::DeleteMapValue(const MapKey& map_key) {
244 const Key& key = UnwrapMapKey<Key>(map_key);
245 return MutableMap()->erase(key);
246 }
247
248 template <typename Derived, typename Key, typename T,
249 WireFormatLite::FieldType kKeyFieldType,
250 WireFormatLite::FieldType kValueFieldType, int default_enum_value>
251 void MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
MergeFrom(const MapFieldBase & other)252 default_enum_value>::MergeFrom(const MapFieldBase& other) {
253 MapFieldBase::SyncMapWithRepeatedField();
254 const MapField& other_field = static_cast<const MapField&>(other);
255 other_field.SyncMapWithRepeatedField();
256 impl_.MergeFrom(other_field.impl_);
257 MapFieldBase::SetMapDirty();
258 }
259
260 template <typename Derived, typename Key, typename T,
261 WireFormatLite::FieldType kKeyFieldType,
262 WireFormatLite::FieldType kValueFieldType, int default_enum_value>
263 void MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
Swap(MapFieldBase * other)264 default_enum_value>::Swap(MapFieldBase* other) {
265 MapField* other_field = down_cast<MapField*>(other);
266 std::swap(this->MapFieldBase::repeated_field_, other_field->repeated_field_);
267 impl_.Swap(&other_field->impl_);
268 // a relaxed swap of the atomic
269 auto other_state = other_field->state_.load(std::memory_order_relaxed);
270 auto this_state = this->MapFieldBase::state_.load(std::memory_order_relaxed);
271 other_field->state_.store(this_state, std::memory_order_relaxed);
272 this->MapFieldBase::state_.store(other_state, std::memory_order_relaxed);
273 }
274
275 template <typename Derived, typename Key, typename T,
276 WireFormatLite::FieldType kKeyFieldType,
277 WireFormatLite::FieldType kValueFieldType, int default_enum_value>
278 void MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
SyncRepeatedFieldWithMapNoLock()279 default_enum_value>::SyncRepeatedFieldWithMapNoLock() const {
280 if (this->MapFieldBase::repeated_field_ == NULL) {
281 if (this->MapFieldBase::arena_ == NULL) {
282 this->MapFieldBase::repeated_field_ = new RepeatedPtrField<Message>();
283 } else {
284 this->MapFieldBase::repeated_field_ =
285 Arena::CreateMessage<RepeatedPtrField<Message> >(
286 this->MapFieldBase::arena_);
287 }
288 }
289 const Map<Key, T>& map = impl_.GetMap();
290 RepeatedPtrField<EntryType>* repeated_field =
291 reinterpret_cast<RepeatedPtrField<EntryType>*>(
292 this->MapFieldBase::repeated_field_);
293
294 repeated_field->Clear();
295
296 // The only way we can get at this point is through reflection and the
297 // only way we can get the reflection object is by having called GetReflection
298 // on the encompassing field. So that type must have existed and hence we
299 // know that this MapEntry default_type has also already been constructed.
300 // So it's safe to just call internal_default_instance().
301 const Message* default_entry = Derived::internal_default_instance();
302 for (typename Map<Key, T>::const_iterator it = map.begin(); it != map.end();
303 ++it) {
304 EntryType* new_entry =
305 down_cast<EntryType*>(default_entry->New(this->MapFieldBase::arena_));
306 repeated_field->AddAllocated(new_entry);
307 (*new_entry->mutable_key()) = it->first;
308 (*new_entry->mutable_value()) = it->second;
309 }
310 }
311
312 template <typename Derived, typename Key, typename T,
313 WireFormatLite::FieldType kKeyFieldType,
314 WireFormatLite::FieldType kValueFieldType, int default_enum_value>
315 void MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
SyncMapWithRepeatedFieldNoLock()316 default_enum_value>::SyncMapWithRepeatedFieldNoLock() const {
317 Map<Key, T>* map = const_cast<MapField*>(this)->impl_.MutableMap();
318 RepeatedPtrField<EntryType>* repeated_field =
319 reinterpret_cast<RepeatedPtrField<EntryType>*>(
320 this->MapFieldBase::repeated_field_);
321 GOOGLE_CHECK(this->MapFieldBase::repeated_field_ != NULL);
322 map->clear();
323 for (typename RepeatedPtrField<EntryType>::iterator it =
324 repeated_field->begin();
325 it != repeated_field->end(); ++it) {
326 // Cast is needed because Map's api and internal storage is different when
327 // value is enum. For enum, we cannot cast an int to enum. Thus, we have to
328 // copy value. For other types, they have same exposed api type and internal
329 // stored type. We should not introduce value copy for them. We achieve this
330 // by casting to value for enum while casting to reference for other types.
331 (*map)[it->key()] = static_cast<CastValueType>(it->value());
332 }
333 }
334
335 template <typename Derived, typename Key, typename T,
336 WireFormatLite::FieldType kKeyFieldType,
337 WireFormatLite::FieldType kValueFieldType, int default_enum_value>
338 size_t MapField<Derived, Key, T, kKeyFieldType, kValueFieldType,
SpaceUsedExcludingSelfNoLock()339 default_enum_value>::SpaceUsedExcludingSelfNoLock() const {
340 size_t size = 0;
341 if (this->MapFieldBase::repeated_field_ != NULL) {
342 size += this->MapFieldBase::repeated_field_->SpaceUsedExcludingSelfLong();
343 }
344 Map<Key, T>* map = const_cast<MapField*>(this)->impl_.MutableMap();
345 size += sizeof(*map);
346 for (typename Map<Key, T>::iterator it = map->begin(); it != map->end();
347 ++it) {
348 size += KeyTypeHandler::SpaceUsedInMapLong(it->first);
349 size += ValueTypeHandler::SpaceUsedInMapLong(it->second);
350 }
351 return size;
352 }
353 } // namespace internal
354 } // namespace protobuf
355 } // namespace google
356
357 #endif // GOOGLE_PROTOBUF_MAP_FIELD_INL_H__
358