1 // Copyright 2016 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_STL_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_STL_H_ 7 8 #include <map> 9 #include <unordered_map> 10 11 #include "mojo/public/cpp/bindings/map_traits.h" 12 13 namespace mojo { 14 15 template <typename K, typename V> 16 struct MapTraits<std::map<K, V>> { 17 using Key = K; 18 using Value = V; 19 using Iterator = typename std::map<K, V>::iterator; 20 using ConstIterator = typename std::map<K, V>::const_iterator; 21 22 static bool IsNull(const std::map<K, V>& input) { 23 // std::map<> is always converted to non-null mojom map. 24 return false; 25 } 26 27 static void SetToNull(std::map<K, V>* output) { 28 // std::map<> doesn't support null state. Set it to empty instead. 29 output->clear(); 30 } 31 32 static size_t GetSize(const std::map<K, V>& input) { return input.size(); } 33 34 static ConstIterator GetBegin(const std::map<K, V>& input) { 35 return input.begin(); 36 } 37 static Iterator GetBegin(std::map<K, V>& input) { return input.begin(); } 38 39 static void AdvanceIterator(ConstIterator& iterator) { iterator++; } 40 static void AdvanceIterator(Iterator& iterator) { iterator++; } 41 42 static const K& GetKey(Iterator& iterator) { return iterator->first; } 43 static const K& GetKey(ConstIterator& iterator) { return iterator->first; } 44 45 static V& GetValue(Iterator& iterator) { return iterator->second; } 46 static const V& GetValue(ConstIterator& iterator) { return iterator->second; } 47 48 static bool Insert(std::map<K, V>& input, const K& key, V&& value) { 49 input.insert(std::make_pair(key, std::forward<V>(value))); 50 return true; 51 } 52 static bool Insert(std::map<K, V>& input, const K& key, const V& value) { 53 input.insert(std::make_pair(key, value)); 54 return true; 55 } 56 57 static void SetToEmpty(std::map<K, V>* output) { output->clear(); } 58 }; 59 60 template <typename K, typename V> 61 struct MapTraits<std::unordered_map<K, V>> { 62 using Key = K; 63 using Value = V; 64 using Iterator = typename std::unordered_map<K, V>::iterator; 65 using ConstIterator = typename std::unordered_map<K, V>::const_iterator; 66 67 static bool IsNull(const std::unordered_map<K, V>& input) { 68 // std::unordered_map<> is always converted to non-null mojom map. 69 return false; 70 } 71 72 static void SetToNull(std::unordered_map<K, V>* output) { 73 // std::unordered_map<> doesn't support null state. Set it to empty instead. 74 output->clear(); 75 } 76 77 static size_t GetSize(const std::unordered_map<K, V>& input) { 78 return input.size(); 79 } 80 81 static ConstIterator GetBegin(const std::unordered_map<K, V>& input) { 82 return input.begin(); 83 } 84 static Iterator GetBegin(std::unordered_map<K, V>& input) { 85 return input.begin(); 86 } 87 88 static void AdvanceIterator(ConstIterator& iterator) { iterator++; } 89 static void AdvanceIterator(Iterator& iterator) { iterator++; } 90 91 static const K& GetKey(Iterator& iterator) { return iterator->first; } 92 static const K& GetKey(ConstIterator& iterator) { return iterator->first; } 93 94 static V& GetValue(Iterator& iterator) { return iterator->second; } 95 static const V& GetValue(ConstIterator& iterator) { return iterator->second; } 96 97 template <typename IK, typename IV> 98 static bool Insert(std::unordered_map<K, V>& input, IK&& key, IV&& value) { 99 input.insert( 100 std::make_pair(std::forward<IK>(key), std::forward<IV>(value))); 101 return true; 102 } 103 104 static void SetToEmpty(std::unordered_map<K, V>* output) { output->clear(); } 105 }; 106 107 } // namespace mojo 108 109 #endif // MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_STL_H_ 110