OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_STL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_STL_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_STL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_STL_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
| 9 #include <unordered_map> |
9 | 10 |
10 #include "mojo/public/cpp/bindings/map_traits.h" | 11 #include "mojo/public/cpp/bindings/map_traits.h" |
11 | 12 |
12 namespace mojo { | 13 namespace mojo { |
13 | 14 |
14 template <typename K, typename V> | 15 template <typename K, typename V> |
15 struct MapTraits<std::map<K, V>> { | 16 struct MapTraits<std::map<K, V>> { |
16 using Key = K; | 17 using Key = K; |
17 using Value = V; | 18 using Value = V; |
18 using Iterator = typename std::map<K, V>::iterator; | 19 using Iterator = typename std::map<K, V>::iterator; |
(...skipping 30 matching lines...) Expand all Loading... |
49 return true; | 50 return true; |
50 } | 51 } |
51 static bool Insert(std::map<K, V>& input, const K& key, const V& value) { | 52 static bool Insert(std::map<K, V>& input, const K& key, const V& value) { |
52 input.insert(std::make_pair(key, value)); | 53 input.insert(std::make_pair(key, value)); |
53 return true; | 54 return true; |
54 } | 55 } |
55 | 56 |
56 static void SetToEmpty(std::map<K, V>* output) { output->clear(); } | 57 static void SetToEmpty(std::map<K, V>* output) { output->clear(); } |
57 }; | 58 }; |
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 static bool Insert(std::unordered_map<K, V>& input, const K& key, V&& value) { |
| 98 input.insert(std::make_pair(key, std::forward<V>(value))); |
| 99 return true; |
| 100 } |
| 101 static bool Insert(std::unordered_map<K, V>& input, |
| 102 const K& key, |
| 103 const V& value) { |
| 104 input.insert(std::make_pair(key, value)); |
| 105 return true; |
| 106 } |
| 107 |
| 108 static void SetToEmpty(std::unordered_map<K, V>* output) { output->clear(); } |
| 109 }; |
| 110 |
59 } // namespace mojo | 111 } // namespace mojo |
60 | 112 |
61 #endif // MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_STL_H_ | 113 #endif // MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_STL_H_ |
OLD | NEW |