• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef _SHARED_PTR_H
36 #include <google/protobuf/stubs/shared_ptr.h>
37 #endif
38 
39 #include <google/protobuf/map.h>
40 #include <google/protobuf/map_field.h>
41 #include <google/protobuf/map_type_handler.h>
42 
43 namespace google {
44 namespace protobuf {
45 namespace internal {
46 // UnwrapMapKey template
47 template<typename T>
48 T UnwrapMapKey(const MapKey& map_key);
49 template<>
50 inline int32 UnwrapMapKey<int32>(const MapKey& map_key) {
51   return map_key.GetInt32Value();
52 }
53 template<>
54 inline uint32 UnwrapMapKey<uint32>(const MapKey& map_key) {
55   return map_key.GetUInt32Value();
56 }
57 template<>
58 inline int64 UnwrapMapKey<int64>(const MapKey& map_key) {
59   return map_key.GetInt64Value();
60 }
61 template<>
62 inline uint64 UnwrapMapKey<uint64>(const MapKey& map_key) {
63   return map_key.GetUInt64Value();
64 }
65 template<>
66 inline bool UnwrapMapKey<bool>(const MapKey& map_key) {
67   return map_key.GetBoolValue();
68 }
69 template<>
70 inline string UnwrapMapKey<string>(const MapKey& map_key) {
71   return map_key.GetStringValue();
72 }
73 
74 // SetMapKey template
75 template<typename T>
76 inline void SetMapKey(MapKey* map_key, const T& value);
77 template<>
78 inline void SetMapKey<int32>(MapKey* map_key, const int32& value) {
79   map_key->SetInt32Value(value);
80 }
81 template<>
82 inline void SetMapKey<uint32>(MapKey* map_key, const uint32& value) {
83   map_key->SetUInt32Value(value);
84 }
85 template<>
86 inline void SetMapKey<int64>(MapKey* map_key, const int64& value) {
87   map_key->SetInt64Value(value);
88 }
89 template<>
90 inline void SetMapKey<uint64>(MapKey* map_key, const uint64& value) {
91   map_key->SetUInt64Value(value);
92 }
93 template<>
94 inline void SetMapKey<bool>(MapKey* map_key, const bool& value) {
95   map_key->SetBoolValue(value);
96 }
97 template<>
98 inline void SetMapKey<string>(MapKey* map_key, const string& value) {
99   map_key->SetStringValue(value);
100 }
101 
102 // ------------------------TypeDefinedMapFieldBase---------------
103 template <typename Key, typename T>
104 typename Map<Key, T>::const_iterator&
InternalGetIterator(const MapIterator * map_iter)105 TypeDefinedMapFieldBase<Key, T>::InternalGetIterator(
106     const MapIterator* map_iter) const {
107   return *reinterpret_cast<typename Map<Key, T>::const_iterator *>(
108       map_iter->iter_);
109 }
110 
111 template <typename Key, typename T>
MapBegin(MapIterator * map_iter)112 void TypeDefinedMapFieldBase<Key, T>::MapBegin(MapIterator* map_iter) const {
113   InternalGetIterator(map_iter) = GetMap().begin();
114   SetMapIteratorValue(map_iter);
115 }
116 
117 template <typename Key, typename T>
MapEnd(MapIterator * map_iter)118 void TypeDefinedMapFieldBase<Key, T>::MapEnd(MapIterator* map_iter) const {
119   InternalGetIterator(map_iter) = GetMap().end();
120 }
121 
122 template <typename Key, typename T>
EqualIterator(const MapIterator & a,const MapIterator & b)123 bool TypeDefinedMapFieldBase<Key, T>::EqualIterator(const MapIterator& a,
124                                                     const MapIterator& b)
125     const {
126   return InternalGetIterator(&a) == InternalGetIterator(&b);
127 }
128 
129 template <typename Key, typename T>
IncreaseIterator(MapIterator * map_iter)130 void TypeDefinedMapFieldBase<Key, T>::IncreaseIterator(MapIterator* map_iter)
131     const {
132   ++InternalGetIterator(map_iter);
133   SetMapIteratorValue(map_iter);
134 }
135 
136 template <typename Key, typename T>
InitializeIterator(MapIterator * map_iter)137 void TypeDefinedMapFieldBase<Key, T>::InitializeIterator(
138     MapIterator* map_iter) const {
139   map_iter->iter_ = new typename Map<Key, T>::const_iterator;
140   GOOGLE_CHECK(map_iter->iter_ != NULL);
141 }
142 
143 template <typename Key, typename T>
DeleteIterator(MapIterator * map_iter)144 void TypeDefinedMapFieldBase<Key, T>::DeleteIterator(MapIterator* map_iter)
145     const {
146   delete reinterpret_cast<typename Map<Key, T>::const_iterator *>(
147       map_iter->iter_);
148 }
149 
150 template <typename Key, typename T>
CopyIterator(MapIterator * this_iter,const MapIterator & that_iter)151 void TypeDefinedMapFieldBase<Key, T>::CopyIterator(
152     MapIterator* this_iter,
153     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 Key, typename T,
166           WireFormatLite::FieldType kKeyFieldType,
167           WireFormatLite::FieldType kValueFieldType,
168           int default_enum_value>
MapField()169 MapField<Key, T, kKeyFieldType, kValueFieldType, default_enum_value>::MapField()
170     : default_entry_(NULL) {}
171 
172 template <typename Key, typename T,
173           WireFormatLite::FieldType kKeyFieldType,
174           WireFormatLite::FieldType kValueFieldType,
175           int default_enum_value>
MapField(Arena * arena)176 MapField<Key, T, kKeyFieldType, kValueFieldType, default_enum_value>::MapField(
177     Arena* arena)
178     : TypeDefinedMapFieldBase<Key, T>(arena),
179       MapFieldLite<Key, T, kKeyFieldType, kValueFieldType, default_enum_value>(
180           arena),
181       default_entry_(NULL) {}
182 
183 template <typename Key, typename T,
184           WireFormatLite::FieldType kKeyFieldType,
185           WireFormatLite::FieldType kValueFieldType,
186           int default_enum_value>
MapField(const Message * default_entry)187 MapField<Key, T, kKeyFieldType, kValueFieldType, default_enum_value>::MapField(
188     const Message* default_entry)
189     : default_entry_(down_cast<const EntryType*>(default_entry)) {}
190 
191 template <typename Key, typename T,
192           WireFormatLite::FieldType kKeyFieldType,
193           WireFormatLite::FieldType kValueFieldType,
194           int default_enum_value>
MapField(Arena * arena,const Message * default_entry)195 MapField<Key, T, kKeyFieldType, kValueFieldType, default_enum_value>::MapField(
196     Arena* arena, const Message* default_entry)
197     : TypeDefinedMapFieldBase<Key, T>(arena),
198       MapFieldLite<Key, T, kKeyFieldType, kValueFieldType, default_enum_value>(
199           arena),
200       default_entry_(down_cast<const EntryType*>(default_entry)) {}
201 
202 template <typename Key, typename T,
203           WireFormatLite::FieldType kKeyFieldType,
204           WireFormatLite::FieldType kValueFieldType,
205           int default_enum_value>
206 MapField<Key, T, kKeyFieldType, kValueFieldType,
~MapField()207          default_enum_value>::~MapField() {}
208 
209 template <typename Key, typename T,
210           WireFormatLite::FieldType kKeyFieldType,
211           WireFormatLite::FieldType kValueFieldType,
212           int default_enum_value>
213 int
214 MapField<Key, T, kKeyFieldType, kValueFieldType,
size()215          default_enum_value>::size() const {
216   MapFieldBase::SyncMapWithRepeatedField();
217   return MapFieldLiteType::GetInternalMap().size();
218 }
219 
220 template <typename Key, typename T,
221           WireFormatLite::FieldType kKeyFieldType,
222           WireFormatLite::FieldType kValueFieldType,
223           int default_enum_value>
224 void
225 MapField<Key, T, kKeyFieldType, kValueFieldType,
Clear()226          default_enum_value>::Clear() {
227   MapFieldBase::SyncMapWithRepeatedField();
228   MapFieldLiteType::MutableInternalMap()->clear();
229   MapFieldBase::SetMapDirty();
230 }
231 
232 template <typename Key, typename T,
233           WireFormatLite::FieldType kKeyFieldType,
234           WireFormatLite::FieldType kValueFieldType,
235           int default_enum_value>
236 void MapField<Key, T, kKeyFieldType, kValueFieldType,
SetMapIteratorValue(MapIterator * map_iter)237               default_enum_value>::SetMapIteratorValue(
238                   MapIterator* map_iter) const {
239   const Map<Key, T>& map = GetMap();
240   typename Map<Key, T>::const_iterator iter =
241       TypeDefinedMapFieldBase<Key, T>::InternalGetIterator(map_iter);
242   if (iter == map.end()) return;
243   SetMapKey(&map_iter->key_, iter->first);
244   map_iter->value_.SetValue(&iter->second);
245 }
246 
247 template <typename Key, typename T,
248           WireFormatLite::FieldType kKeyFieldType,
249           WireFormatLite::FieldType kValueFieldType,
250           int default_enum_value>
251 bool MapField<Key, T, kKeyFieldType, kValueFieldType,
ContainsMapKey(const MapKey & map_key)252               default_enum_value>::ContainsMapKey(
253                   const MapKey& map_key) const {
254   const Map<Key, T>& map = GetMap();
255   const Key& key = UnwrapMapKey<Key>(map_key);
256   typename Map<Key, T>::const_iterator iter = map.find(key);
257   return iter != map.end();
258 }
259 
260 template <typename Key, typename T,
261           WireFormatLite::FieldType kKeyFieldType,
262           WireFormatLite::FieldType kValueFieldType,
263           int default_enum_value>
264 bool MapField<Key, T, kKeyFieldType, kValueFieldType,
InsertOrLookupMapValue(const MapKey & map_key,MapValueRef * val)265               default_enum_value>::InsertOrLookupMapValue(
266                   const MapKey& map_key,
267                   MapValueRef* val) {
268   // Always use mutable map because users may change the map value by
269   // MapValueRef.
270   Map<Key, T>* map = MutableMap();
271   const Key& key = UnwrapMapKey<Key>(map_key);
272   typename Map<Key, T>::iterator iter = map->find(key);
273   if (map->end() == iter) {
274     val->SetValue(&((*map)[key]));
275     return true;
276   }
277   // Key is already in the map. Make sure (*map)[key] is not called.
278   // [] may reorder the map and iterators.
279   val->SetValue(&(iter->second));
280   return false;
281 }
282 
283 template <typename Key, typename T,
284           WireFormatLite::FieldType kKeyFieldType,
285           WireFormatLite::FieldType kValueFieldType,
286           int default_enum_value>
287 bool MapField<Key, T, kKeyFieldType, kValueFieldType,
DeleteMapValue(const MapKey & map_key)288               default_enum_value>::DeleteMapValue(
289                   const MapKey& map_key) {
290   const Key& key = UnwrapMapKey<Key>(map_key);
291   return MutableMap()->erase(key);
292 }
293 
294 template <typename Key, typename T,
295           WireFormatLite::FieldType kKeyFieldType,
296           WireFormatLite::FieldType kValueFieldType,
297           int default_enum_value>
298 const Map<Key, T>&
299 MapField<Key, T, kKeyFieldType, kValueFieldType,
GetMap()300          default_enum_value>::GetMap() const {
301   MapFieldBase::SyncMapWithRepeatedField();
302   return MapFieldLiteType::GetInternalMap();
303 }
304 
305 template <typename Key, typename T,
306           WireFormatLite::FieldType kKeyFieldType,
307           WireFormatLite::FieldType kValueFieldType,
308           int default_enum_value>
309 Map<Key, T>*
310 MapField<Key, T, kKeyFieldType, kValueFieldType,
MutableMap()311          default_enum_value>::MutableMap() {
312   MapFieldBase::SyncMapWithRepeatedField();
313   Map<Key, T>* result = MapFieldLiteType::MutableInternalMap();
314   MapFieldBase::SetMapDirty();
315   return result;
316 }
317 
318 template <typename Key, typename T,
319           WireFormatLite::FieldType kKeyFieldType,
320           WireFormatLite::FieldType kValueFieldType,
321           int default_enum_value>
322 void
323 MapField<Key, T, kKeyFieldType, kValueFieldType,
MergeFrom(const MapFieldLiteType & other)324          default_enum_value>::MergeFrom(
325     const MapFieldLiteType& other) {
326   const MapField& down_other = down_cast<const MapField&>(other);
327   MapFieldBase::SyncMapWithRepeatedField();
328   down_other.SyncMapWithRepeatedField();
329   MapFieldLiteType::MergeFrom(other);
330   MapFieldBase::SetMapDirty();
331 }
332 
333 template <typename Key, typename T,
334           WireFormatLite::FieldType kKeyFieldType,
335           WireFormatLite::FieldType kValueFieldType,
336           int default_enum_value>
337 void
338 MapField<Key, T, kKeyFieldType, kValueFieldType,
Swap(MapFieldLiteType * other)339          default_enum_value>::Swap(
340     MapFieldLiteType* other) {
341   MapField* down_other = down_cast<MapField*>(other);
342   std::swap(MapFieldBase::repeated_field_, down_other->repeated_field_);
343   MapFieldLiteType::Swap(other);
344   std::swap(MapFieldBase::state_, down_other->state_);
345 }
346 
347 template <typename Key, typename T,
348           WireFormatLite::FieldType kKeyFieldType,
349           WireFormatLite::FieldType kValueFieldType,
350           int default_enum_value>
351 void
352 MapField<Key, T, kKeyFieldType, kValueFieldType,
SetEntryDescriptor(const Descriptor ** descriptor)353          default_enum_value>::SetEntryDescriptor(
354     const Descriptor** descriptor) {
355   MapFieldBase::entry_descriptor_ = descriptor;
356 }
357 
358 template <typename Key, typename T,
359           WireFormatLite::FieldType kKeyFieldType,
360           WireFormatLite::FieldType kValueFieldType,
361           int default_enum_value>
362 void
363 MapField<Key, T, kKeyFieldType, kValueFieldType,
SetAssignDescriptorCallback(void (* callback)())364          default_enum_value>::SetAssignDescriptorCallback(void (*callback)()) {
365   MapFieldBase::assign_descriptor_callback_ = callback;
366 }
367 
368 template <typename Key, typename T,
369           WireFormatLite::FieldType kKeyFieldType,
370           WireFormatLite::FieldType kValueFieldType,
371           int default_enum_value>
372 const Map<Key, T>&
373 MapField<Key, T, kKeyFieldType, kValueFieldType,
GetInternalMap()374          default_enum_value>::GetInternalMap() const {
375   return MapFieldLiteType::GetInternalMap();
376 }
377 
378 template <typename Key, typename T,
379           WireFormatLite::FieldType kKeyFieldType,
380           WireFormatLite::FieldType kValueFieldType,
381           int default_enum_value>
382 Map<Key, T>*
383 MapField<Key, T, kKeyFieldType, kValueFieldType,
MutableInternalMap()384          default_enum_value>::MutableInternalMap() {
385   return MapFieldLiteType::MutableInternalMap();
386 }
387 
388 template <typename Key, typename T,
389           WireFormatLite::FieldType kKeyFieldType,
390           WireFormatLite::FieldType kValueFieldType,
391           int default_enum_value>
392 void
393 MapField<Key, T, kKeyFieldType, kValueFieldType,
SyncRepeatedFieldWithMapNoLock()394          default_enum_value>::SyncRepeatedFieldWithMapNoLock() const {
395   if (MapFieldBase::repeated_field_ == NULL) {
396     if (MapFieldBase::arena_ == NULL) {
397       MapFieldBase::repeated_field_ = new RepeatedPtrField<Message>();
398     } else {
399       MapFieldBase::repeated_field_ =
400           Arena::CreateMessage<RepeatedPtrField<Message> >(
401               MapFieldBase::arena_);
402     }
403   }
404   const Map<Key, T>& map = GetInternalMap();
405   RepeatedPtrField<EntryType>* repeated_field =
406       reinterpret_cast<RepeatedPtrField<EntryType>*>(
407           MapFieldBase::repeated_field_);
408 
409   repeated_field->Clear();
410 
411   for (typename Map<Key, T>::const_iterator it = map.begin();
412        it != map.end(); ++it) {
413     InitDefaultEntryOnce();
414     GOOGLE_CHECK(default_entry_ != NULL);
415     EntryType* new_entry =
416         down_cast<EntryType*>(default_entry_->New(MapFieldBase::arena_));
417     repeated_field->AddAllocated(new_entry);
418     (*new_entry->mutable_key()) = it->first;
419     (*new_entry->mutable_value()) = it->second;
420   }
421 }
422 
423 template <typename Key, typename T,
424           WireFormatLite::FieldType kKeyFieldType,
425           WireFormatLite::FieldType kValueFieldType,
426           int default_enum_value>
427 void
428 MapField<Key, T, kKeyFieldType, kValueFieldType,
SyncMapWithRepeatedFieldNoLock()429          default_enum_value>::SyncMapWithRepeatedFieldNoLock() const {
430   Map<Key, T>* map = const_cast<MapField*>(this)->MutableInternalMap();
431   RepeatedPtrField<EntryType>* repeated_field =
432       reinterpret_cast<RepeatedPtrField<EntryType>*>(
433           MapFieldBase::repeated_field_);
434   GOOGLE_CHECK(MapFieldBase::repeated_field_ != NULL);
435   map->clear();
436   for (typename RepeatedPtrField<EntryType>::iterator it =
437            repeated_field->begin(); it != repeated_field->end(); ++it) {
438     // Cast is needed because Map's api and internal storage is different when
439     // value is enum. For enum, we cannot cast an int to enum. Thus, we have to
440     // copy value. For other types, they have same exposed api type and internal
441     // stored type. We should not introduce value copy for them. We achieve this
442     // by casting to value for enum while casting to reference for other types.
443     (*map)[it->key()] = static_cast<CastValueType>(it->value());
444   }
445 }
446 
447 template <typename Key, typename T,
448           WireFormatLite::FieldType kKeyFieldType,
449           WireFormatLite::FieldType kValueFieldType,
450           int default_enum_value>
451 int
452 MapField<Key, T, kKeyFieldType, kValueFieldType,
SpaceUsedExcludingSelfNoLock()453          default_enum_value>::SpaceUsedExcludingSelfNoLock() const {
454   int size = 0;
455   if (MapFieldBase::repeated_field_ != NULL) {
456     size += MapFieldBase::repeated_field_->SpaceUsedExcludingSelf();
457   }
458   Map<Key, T>* map = const_cast<MapField*>(this)->MutableInternalMap();
459   size += sizeof(*map);
460   for (typename Map<Key, T>::iterator it = map->begin();
461        it != map->end(); ++it) {
462     size += KeyTypeHandler::SpaceUsedInMap(it->first);
463     size += ValueTypeHandler::SpaceUsedInMap(it->second);
464   }
465   return size;
466 }
467 
468 template <typename Key, typename T,
469           WireFormatLite::FieldType kKeyFieldType,
470           WireFormatLite::FieldType kValueFieldType,
471           int default_enum_value>
472 void
473 MapField<Key, T, kKeyFieldType, kValueFieldType,
InitDefaultEntryOnce()474          default_enum_value>::InitDefaultEntryOnce()
475     const {
476   if (default_entry_ == NULL) {
477     MapFieldBase::InitMetadataOnce();
478     GOOGLE_CHECK(*MapFieldBase::entry_descriptor_ != NULL);
479     default_entry_ = down_cast<const EntryType*>(
480         MessageFactory::generated_factory()->GetPrototype(
481             *MapFieldBase::entry_descriptor_));
482   }
483 }
484 
485 }  // namespace internal
486 }  // namespace protobuf
487 
488 }  // namespace google
489 #endif  // GOOGLE_PROTOBUF_MAP_FIELD_INL_H__
490