• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, typename Compare>
16 struct MapTraits<std::map<K, V, Compare>> {
17   using Key = K;
18   using Value = V;
19   using Iterator = typename std::map<K, V, Compare>::iterator;
20   using ConstIterator = typename std::map<K, V, Compare>::const_iterator;
21 
22   static bool IsNull(const std::map<K, V, Compare>& 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, Compare>* 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, Compare>& input) {
33     return input.size();
34   }
35 
36   static ConstIterator GetBegin(const std::map<K, V, Compare>& input) {
37     return input.begin();
38   }
39   static Iterator GetBegin(std::map<K, V, Compare>& input) {
40     return input.begin();
41   }
42 
43   static void AdvanceIterator(ConstIterator& iterator) { iterator++; }
44   static void AdvanceIterator(Iterator& iterator) { iterator++; }
45 
46   static const K& GetKey(Iterator& iterator) { return iterator->first; }
47   static const K& GetKey(ConstIterator& iterator) { return iterator->first; }
48 
49   static V& GetValue(Iterator& iterator) { return iterator->second; }
50   static const V& GetValue(ConstIterator& iterator) { return iterator->second; }
51 
52   static bool Insert(std::map<K, V, Compare>& input, const K& key, V&& value) {
53     input.insert(std::make_pair(key, std::forward<V>(value)));
54     return true;
55   }
56   static bool Insert(std::map<K, V, Compare>& input,
57                      const K& key,
58                      const V& value) {
59     input.insert(std::make_pair(key, value));
60     return true;
61   }
62 
63   static void SetToEmpty(std::map<K, V, Compare>* output) { output->clear(); }
64 };
65 
66 template <typename K, typename V>
67 struct MapTraits<std::unordered_map<K, V>> {
68   using Key = K;
69   using Value = V;
70   using Iterator = typename std::unordered_map<K, V>::iterator;
71   using ConstIterator = typename std::unordered_map<K, V>::const_iterator;
72 
73   static bool IsNull(const std::unordered_map<K, V>& input) {
74     // std::unordered_map<> is always converted to non-null mojom map.
75     return false;
76   }
77 
78   static void SetToNull(std::unordered_map<K, V>* output) {
79     // std::unordered_map<> doesn't support null state. Set it to empty instead.
80     output->clear();
81   }
82 
83   static size_t GetSize(const std::unordered_map<K, V>& input) {
84     return input.size();
85   }
86 
87   static ConstIterator GetBegin(const std::unordered_map<K, V>& input) {
88     return input.begin();
89   }
90   static Iterator GetBegin(std::unordered_map<K, V>& input) {
91     return input.begin();
92   }
93 
94   static void AdvanceIterator(ConstIterator& iterator) { iterator++; }
95   static void AdvanceIterator(Iterator& iterator) { iterator++; }
96 
97   static const K& GetKey(Iterator& iterator) { return iterator->first; }
98   static const K& GetKey(ConstIterator& iterator) { return iterator->first; }
99 
100   static V& GetValue(Iterator& iterator) { return iterator->second; }
101   static const V& GetValue(ConstIterator& iterator) { return iterator->second; }
102 
103   template <typename IK, typename IV>
104   static bool Insert(std::unordered_map<K, V>& input, IK&& key, IV&& value) {
105     input.insert(
106         std::make_pair(std::forward<IK>(key), std::forward<IV>(value)));
107     return true;
108   }
109 
110   static void SetToEmpty(std::unordered_map<K, V>* output) { output->clear(); }
111 };
112 
113 }  // namespace mojo
114 
115 #endif  // MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_STL_H_
116