Line data Source code
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&
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>
112 0 : void TypeDefinedMapFieldBase<Key, T>::MapBegin(MapIterator* map_iter) const {
113 0 : InternalGetIterator(map_iter) = GetMap().begin();
114 0 : SetMapIteratorValue(map_iter);
115 0 : }
116 :
117 : template <typename Key, typename T>
118 0 : void TypeDefinedMapFieldBase<Key, T>::MapEnd(MapIterator* map_iter) const {
119 0 : InternalGetIterator(map_iter) = GetMap().end();
120 0 : }
121 :
122 : template <typename Key, typename T>
123 0 : bool TypeDefinedMapFieldBase<Key, T>::EqualIterator(const MapIterator& a,
124 : const MapIterator& b)
125 : const {
126 0 : return InternalGetIterator(&a) == InternalGetIterator(&b);
127 : }
128 :
129 : template <typename Key, typename T>
130 0 : void TypeDefinedMapFieldBase<Key, T>::IncreaseIterator(MapIterator* map_iter)
131 : const {
132 0 : ++InternalGetIterator(map_iter);
133 0 : SetMapIteratorValue(map_iter);
134 0 : }
135 :
136 : template <typename Key, typename T>
137 0 : void TypeDefinedMapFieldBase<Key, T>::InitializeIterator(
138 : MapIterator* map_iter) const {
139 0 : map_iter->iter_ = new typename Map<Key, T>::const_iterator;
140 0 : GOOGLE_CHECK(map_iter->iter_ != NULL);
141 0 : }
142 :
143 : template <typename Key, typename T>
144 0 : void TypeDefinedMapFieldBase<Key, T>::DeleteIterator(MapIterator* map_iter)
145 : const {
146 0 : delete reinterpret_cast<typename Map<Key, T>::const_iterator *>(
147 : map_iter->iter_);
148 0 : }
149 :
150 : template <typename Key, typename T>
151 0 : void TypeDefinedMapFieldBase<Key, T>::CopyIterator(
152 : MapIterator* this_iter,
153 : const MapIterator& that_iter) const {
154 0 : InternalGetIterator(this_iter) = InternalGetIterator(&that_iter);
155 0 : 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 0 : this_iter->value_.SetType((FieldDescriptor::CppType)that_iter.value_.type_);
159 0 : SetMapIteratorValue(this_iter);
160 0 : }
161 :
162 : // ----------------------------------------------------------------------
163 :
164 : template <typename Key, typename T,
165 : WireFormatLite::FieldType kKeyFieldType,
166 : WireFormatLite::FieldType kValueFieldType,
167 : int default_enum_value>
168 : MapField<Key, T, kKeyFieldType, kValueFieldType, default_enum_value>::MapField()
169 : : default_entry_(NULL) {}
170 :
171 : template <typename Key, typename T,
172 : WireFormatLite::FieldType kKeyFieldType,
173 : WireFormatLite::FieldType kValueFieldType,
174 : int default_enum_value>
175 : MapField<Key, T, kKeyFieldType, kValueFieldType, default_enum_value>::MapField(
176 : Arena* arena)
177 : : TypeDefinedMapFieldBase<Key, T>(arena),
178 : MapFieldLite<Key, T, kKeyFieldType, kValueFieldType, default_enum_value>(
179 : arena),
180 : default_entry_(NULL) {}
181 :
182 : template <typename Key, typename T,
183 : WireFormatLite::FieldType kKeyFieldType,
184 : WireFormatLite::FieldType kValueFieldType,
185 : int default_enum_value>
186 : MapField<Key, T, kKeyFieldType, kValueFieldType, default_enum_value>::MapField(
187 : const Message* default_entry)
188 : : default_entry_(down_cast<const EntryType*>(default_entry)) {}
189 :
190 : template <typename Key, typename T,
191 : WireFormatLite::FieldType kKeyFieldType,
192 : WireFormatLite::FieldType kValueFieldType,
193 : int default_enum_value>
194 : MapField<Key, T, kKeyFieldType, kValueFieldType, default_enum_value>::MapField(
195 : Arena* arena, const Message* default_entry)
196 : : TypeDefinedMapFieldBase<Key, T>(arena),
197 : MapFieldLite<Key, T, kKeyFieldType, kValueFieldType, default_enum_value>(
198 : arena),
199 : default_entry_(down_cast<const EntryType*>(default_entry)) {}
200 :
201 : template <typename Key, typename T,
202 : WireFormatLite::FieldType kKeyFieldType,
203 : WireFormatLite::FieldType kValueFieldType,
204 : int default_enum_value>
205 : MapField<Key, T, kKeyFieldType, kValueFieldType,
206 : default_enum_value>::~MapField() {}
207 :
208 : template <typename Key, typename T,
209 : WireFormatLite::FieldType kKeyFieldType,
210 : WireFormatLite::FieldType kValueFieldType,
211 : int default_enum_value>
212 : int
213 : MapField<Key, T, kKeyFieldType, kValueFieldType,
214 : default_enum_value>::size() const {
215 : MapFieldBase::SyncMapWithRepeatedField();
216 : return MapFieldLiteType::GetInternalMap().size();
217 : }
218 :
219 : template <typename Key, typename T,
220 : WireFormatLite::FieldType kKeyFieldType,
221 : WireFormatLite::FieldType kValueFieldType,
222 : int default_enum_value>
223 : void
224 : MapField<Key, T, kKeyFieldType, kValueFieldType,
225 : default_enum_value>::Clear() {
226 : MapFieldBase::SyncMapWithRepeatedField();
227 : MapFieldLiteType::MutableInternalMap()->clear();
228 : MapFieldBase::SetMapDirty();
229 : }
230 :
231 : template <typename Key, typename T,
232 : WireFormatLite::FieldType kKeyFieldType,
233 : WireFormatLite::FieldType kValueFieldType,
234 : int default_enum_value>
235 : void MapField<Key, T, kKeyFieldType, kValueFieldType,
236 : default_enum_value>::SetMapIteratorValue(
237 : MapIterator* map_iter) const {
238 : const Map<Key, T>& map = GetMap();
239 : typename Map<Key, T>::const_iterator iter =
240 : TypeDefinedMapFieldBase<Key, T>::InternalGetIterator(map_iter);
241 : if (iter == map.end()) return;
242 : SetMapKey(&map_iter->key_, iter->first);
243 : map_iter->value_.SetValue(&iter->second);
244 : }
245 :
246 : template <typename Key, typename T,
247 : WireFormatLite::FieldType kKeyFieldType,
248 : WireFormatLite::FieldType kValueFieldType,
249 : int default_enum_value>
250 : bool MapField<Key, T, kKeyFieldType, kValueFieldType,
251 : default_enum_value>::ContainsMapKey(
252 : const MapKey& map_key) const {
253 : const Map<Key, T>& map = GetMap();
254 : const Key& key = UnwrapMapKey<Key>(map_key);
255 : typename Map<Key, T>::const_iterator iter = map.find(key);
256 : return iter != map.end();
257 : }
258 :
259 : template <typename Key, typename T,
260 : WireFormatLite::FieldType kKeyFieldType,
261 : WireFormatLite::FieldType kValueFieldType,
262 : int default_enum_value>
263 : bool MapField<Key, T, kKeyFieldType, kValueFieldType,
264 : default_enum_value>::InsertMapValue(const MapKey& map_key,
265 : MapValueRef* val) {
266 : Map<Key, T>* map = MutableMap();
267 : bool result = false;
268 : const Key& key = UnwrapMapKey<Key>(map_key);
269 : if (map->end() == map->find(key)) {
270 : result = true;
271 : }
272 : val->SetValue(&((*map)[key]));
273 : return result;
274 : }
275 :
276 : template <typename Key, typename T,
277 : WireFormatLite::FieldType kKeyFieldType,
278 : WireFormatLite::FieldType kValueFieldType,
279 : int default_enum_value>
280 : bool MapField<Key, T, kKeyFieldType, kValueFieldType,
281 : default_enum_value>::DeleteMapValue(
282 : const MapKey& map_key) {
283 : const Key& key = UnwrapMapKey<Key>(map_key);
284 : return MutableMap()->erase(key);
285 : }
286 :
287 : template <typename Key, typename T,
288 : WireFormatLite::FieldType kKeyFieldType,
289 : WireFormatLite::FieldType kValueFieldType,
290 : int default_enum_value>
291 : const Map<Key, T>&
292 : MapField<Key, T, kKeyFieldType, kValueFieldType,
293 : default_enum_value>::GetMap() const {
294 : MapFieldBase::SyncMapWithRepeatedField();
295 : return MapFieldLiteType::GetInternalMap();
296 : }
297 :
298 : template <typename Key, typename T,
299 : WireFormatLite::FieldType kKeyFieldType,
300 : WireFormatLite::FieldType kValueFieldType,
301 : int default_enum_value>
302 : Map<Key, T>*
303 : MapField<Key, T, kKeyFieldType, kValueFieldType,
304 : default_enum_value>::MutableMap() {
305 : MapFieldBase::SyncMapWithRepeatedField();
306 : Map<Key, T>* result = MapFieldLiteType::MutableInternalMap();
307 : MapFieldBase::SetMapDirty();
308 : return result;
309 : }
310 :
311 : template <typename Key, typename T,
312 : WireFormatLite::FieldType kKeyFieldType,
313 : WireFormatLite::FieldType kValueFieldType,
314 : int default_enum_value>
315 : void
316 : MapField<Key, T, kKeyFieldType, kValueFieldType,
317 : default_enum_value>::MergeFrom(
318 : const MapFieldLiteType& other) {
319 : const MapField& down_other = down_cast<const MapField&>(other);
320 : MapFieldBase::SyncMapWithRepeatedField();
321 : down_other.SyncMapWithRepeatedField();
322 : MapFieldLiteType::MergeFrom(other);
323 : MapFieldBase::SetMapDirty();
324 : }
325 :
326 : template <typename Key, typename T,
327 : WireFormatLite::FieldType kKeyFieldType,
328 : WireFormatLite::FieldType kValueFieldType,
329 : int default_enum_value>
330 : void
331 : MapField<Key, T, kKeyFieldType, kValueFieldType,
332 : default_enum_value>::Swap(
333 : MapFieldLiteType* other) {
334 : MapField* down_other = down_cast<MapField*>(other);
335 : std::swap(MapFieldBase::repeated_field_, down_other->repeated_field_);
336 : MapFieldLiteType::Swap(other);
337 : std::swap(MapFieldBase::state_, down_other->state_);
338 : }
339 :
340 : template <typename Key, typename T,
341 : WireFormatLite::FieldType kKeyFieldType,
342 : WireFormatLite::FieldType kValueFieldType,
343 : int default_enum_value>
344 : void
345 : MapField<Key, T, kKeyFieldType, kValueFieldType,
346 : default_enum_value>::SetEntryDescriptor(
347 : const Descriptor** descriptor) {
348 : MapFieldBase::entry_descriptor_ = descriptor;
349 : }
350 :
351 : template <typename Key, typename T,
352 : WireFormatLite::FieldType kKeyFieldType,
353 : WireFormatLite::FieldType kValueFieldType,
354 : int default_enum_value>
355 : void
356 : MapField<Key, T, kKeyFieldType, kValueFieldType,
357 : default_enum_value>::SetAssignDescriptorCallback(void (*callback)()) {
358 : MapFieldBase::assign_descriptor_callback_ = callback;
359 : }
360 :
361 : template <typename Key, typename T,
362 : WireFormatLite::FieldType kKeyFieldType,
363 : WireFormatLite::FieldType kValueFieldType,
364 : int default_enum_value>
365 : const Map<Key, T>&
366 : MapField<Key, T, kKeyFieldType, kValueFieldType,
367 : default_enum_value>::GetInternalMap() const {
368 : return MapFieldLiteType::GetInternalMap();
369 : }
370 :
371 : template <typename Key, typename T,
372 : WireFormatLite::FieldType kKeyFieldType,
373 : WireFormatLite::FieldType kValueFieldType,
374 : int default_enum_value>
375 : Map<Key, T>*
376 : MapField<Key, T, kKeyFieldType, kValueFieldType,
377 : default_enum_value>::MutableInternalMap() {
378 : return MapFieldLiteType::MutableInternalMap();
379 : }
380 :
381 : template <typename Key, typename T,
382 : WireFormatLite::FieldType kKeyFieldType,
383 : WireFormatLite::FieldType kValueFieldType,
384 : int default_enum_value>
385 : void
386 : MapField<Key, T, kKeyFieldType, kValueFieldType,
387 : default_enum_value>::SyncRepeatedFieldWithMapNoLock() const {
388 : if (MapFieldBase::repeated_field_ == NULL) {
389 : if (MapFieldBase::arena_ == NULL) {
390 : MapFieldBase::repeated_field_ = new RepeatedPtrField<Message>();
391 : } else {
392 : MapFieldBase::repeated_field_ =
393 : Arena::CreateMessage<RepeatedPtrField<Message> >(
394 : MapFieldBase::arena_);
395 : }
396 : }
397 : const Map<Key, T>& map = GetInternalMap();
398 : RepeatedPtrField<EntryType>* repeated_field =
399 : reinterpret_cast<RepeatedPtrField<EntryType>*>(
400 : MapFieldBase::repeated_field_);
401 :
402 : repeated_field->Clear();
403 :
404 : for (typename Map<Key, T>::const_iterator it = map.begin();
405 : it != map.end(); ++it) {
406 : InitDefaultEntryOnce();
407 : GOOGLE_CHECK(default_entry_ != NULL);
408 : EntryType* new_entry =
409 : down_cast<EntryType*>(default_entry_->New(MapFieldBase::arena_));
410 : repeated_field->AddAllocated(new_entry);
411 : (*new_entry->mutable_key()) = it->first;
412 : (*new_entry->mutable_value()) = it->second;
413 : }
414 : }
415 :
416 : template <typename Key, typename T,
417 : WireFormatLite::FieldType kKeyFieldType,
418 : WireFormatLite::FieldType kValueFieldType,
419 : int default_enum_value>
420 : void
421 : MapField<Key, T, kKeyFieldType, kValueFieldType,
422 : default_enum_value>::SyncMapWithRepeatedFieldNoLock() const {
423 : Map<Key, T>* map = const_cast<MapField*>(this)->MutableInternalMap();
424 : RepeatedPtrField<EntryType>* repeated_field =
425 : reinterpret_cast<RepeatedPtrField<EntryType>*>(
426 : MapFieldBase::repeated_field_);
427 : GOOGLE_CHECK(MapFieldBase::repeated_field_ != NULL);
428 : map->clear();
429 : for (typename RepeatedPtrField<EntryType>::iterator it =
430 : repeated_field->begin(); it != repeated_field->end(); ++it) {
431 : // Cast is needed because Map's api and internal storage is different when
432 : // value is enum. For enum, we cannot cast an int to enum. Thus, we have to
433 : // copy value. For other types, they have same exposed api type and internal
434 : // stored type. We should not introduce value copy for them. We achieve this
435 : // by casting to value for enum while casting to reference for other types.
436 : (*map)[it->key()] = static_cast<CastValueType>(it->value());
437 : }
438 : }
439 :
440 : template <typename Key, typename T,
441 : WireFormatLite::FieldType kKeyFieldType,
442 : WireFormatLite::FieldType kValueFieldType,
443 : int default_enum_value>
444 : int
445 : MapField<Key, T, kKeyFieldType, kValueFieldType,
446 : default_enum_value>::SpaceUsedExcludingSelfNoLock() const {
447 : int size = 0;
448 : if (MapFieldBase::repeated_field_ != NULL) {
449 : size += MapFieldBase::repeated_field_->SpaceUsedExcludingSelf();
450 : }
451 : Map<Key, T>* map = const_cast<MapField*>(this)->MutableInternalMap();
452 : size += sizeof(*map);
453 : for (typename Map<Key, T>::iterator it = map->begin();
454 : it != map->end(); ++it) {
455 : size += KeyTypeHandler::SpaceUsedInMap(it->first);
456 : size += ValueTypeHandler::SpaceUsedInMap(it->second);
457 : }
458 : return size;
459 : }
460 :
461 : template <typename Key, typename T,
462 : WireFormatLite::FieldType kKeyFieldType,
463 : WireFormatLite::FieldType kValueFieldType,
464 : int default_enum_value>
465 : void
466 : MapField<Key, T, kKeyFieldType, kValueFieldType,
467 : default_enum_value>::InitDefaultEntryOnce()
468 : const {
469 : if (default_entry_ == NULL) {
470 : MapFieldBase::InitMetadataOnce();
471 : GOOGLE_CHECK(*MapFieldBase::entry_descriptor_ != NULL);
472 : default_entry_ = down_cast<const EntryType*>(
473 : MessageFactory::generated_factory()->GetPrototype(
474 : *MapFieldBase::entry_descriptor_));
475 : }
476 : }
477 :
478 : } // namespace internal
479 : } // namespace protobuf
480 :
481 : } // namespace google
482 : #endif // GOOGLE_PROTOBUF_MAP_FIELD_INL_H__
|