OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_LIB_MAP_INTERNAL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_INTERNAL_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_INTERNAL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_INTERNAL_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <map> | 9 #include <map> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "mojo/public/cpp/bindings/array.h" | 12 #include "mojo/public/cpp/bindings/array.h" |
13 #include "mojo/public/cpp/bindings/lib/template_util.h" | 13 #include "mojo/public/cpp/bindings/lib/template_util.h" |
14 | 14 |
15 namespace mojo { | 15 namespace mojo { |
16 namespace internal { | 16 namespace internal { |
17 | 17 |
18 template <typename Key, typename Value, bool kValueIsMoveOnlyType> | 18 template <typename Key, typename Value, bool kValueIsMoveOnlyType> |
19 struct MapTraits {}; | 19 struct MapTraits {}; |
20 | 20 |
21 // Defines traits of a map for which Value is not a move-only type. | 21 // Defines traits of a map for which Value is not a move-only type. |
22 template <typename Key, typename Value> | 22 template <typename Key, typename Value> |
23 struct MapTraits<Key, Value, false> { | 23 struct MapTraits<Key, Value, false> { |
24 // Map keys can't be move only types. | 24 static inline void Clone(const std::map<Key, Value>& src, |
25 static_assert(!internal::IsMoveOnlyType<Key>::value, | 25 std::map<Key, Value>* dst) { |
26 "Map keys cannot be move only types."); | |
27 | |
28 typedef Key KeyStorageType; | |
29 typedef Key& KeyRefType; | |
30 typedef const Key& KeyConstRefType; | |
31 typedef KeyConstRefType KeyForwardType; | |
32 | |
33 typedef Value ValueStorageType; | |
34 typedef Value& ValueRefType; | |
35 typedef const Value& ValueConstRefType; | |
36 typedef ValueConstRefType ValueForwardType; | |
37 | |
38 static inline void InitializeFrom( | |
39 std::map<KeyStorageType, ValueStorageType>* m, | |
40 mojo::Array<Key> keys, | |
41 mojo::Array<Value> values) { | |
42 for (size_t i = 0; i < keys.size(); ++i) | |
43 Insert(m, keys[i], values[i]); | |
44 } | |
45 static inline void Decompose(std::map<KeyStorageType, ValueStorageType>* m, | |
46 mojo::Array<Key>* keys, | |
47 mojo::Array<Value>* values) { | |
48 keys->resize(m->size()); | |
49 values->resize(m->size()); | |
50 int i = 0; | |
51 for (typename std::map<KeyStorageType, ValueStorageType>::iterator | |
52 it = m->begin(); | |
53 it != m->end(); | |
54 ++it, ++i) { | |
55 (*keys)[i] = it->first; | |
56 (*values)[i] = it->second; | |
57 } | |
58 } | |
59 static inline void Finalize(std::map<KeyStorageType, ValueStorageType>* m) {} | |
60 static inline ValueRefType at(std::map<KeyStorageType, ValueStorageType>* m, | |
61 KeyForwardType key) { | |
62 // We don't have C++11 library support yet, so we have to emulate the crash | |
63 // on a non-existent key. | |
64 auto it = m->find(key); | |
65 MOJO_CHECK(it != m->end()); | |
66 return it->second; | |
67 } | |
68 static inline ValueConstRefType at( | |
69 const std::map<KeyStorageType, ValueStorageType>* m, | |
70 KeyForwardType key) { | |
71 // We don't have C++11 library support yet, so we have to emulate the crash | |
72 // on a non-existent key. | |
73 auto it = m->find(key); | |
74 MOJO_CHECK(it != m->end()); | |
75 return it->second; | |
76 } | |
77 static inline ValueRefType GetOrInsert( | |
78 std::map<KeyStorageType, ValueStorageType>* m, | |
79 KeyForwardType key) { | |
80 // This is the backing for the index operator (operator[]). | |
81 return (*m)[key]; | |
82 } | |
83 static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m, | |
84 KeyForwardType key, | |
85 ValueForwardType value) { | |
86 m->insert(std::make_pair(key, value)); | |
87 } | |
88 static inline KeyConstRefType GetKey( | |
89 const typename std::map<KeyStorageType, ValueStorageType>::const_iterator& | |
90 it) { | |
91 return it->first; | |
92 } | |
93 static inline ValueConstRefType GetValue( | |
94 const typename std::map<KeyStorageType, ValueStorageType>::const_iterator& | |
95 it) { | |
96 return it->second; | |
97 } | |
98 static inline ValueRefType GetValue( | |
99 const typename std::map<KeyStorageType, ValueStorageType>::iterator& it) { | |
100 return it->second; | |
101 } | |
102 static inline void Clone( | |
103 const std::map<KeyStorageType, ValueStorageType>& src, | |
104 std::map<KeyStorageType, ValueStorageType>* dst) { | |
105 dst->clear(); | 26 dst->clear(); |
106 for (auto it = src.begin(); it != src.end(); ++it) | 27 for (auto it = src.begin(); it != src.end(); ++it) |
107 dst->insert(*it); | 28 dst->insert(*it); |
108 } | 29 } |
109 }; | 30 }; |
110 | 31 |
111 // Defines traits of a map for which Value is a move-only type. | 32 // Defines traits of a map for which Value is a move-only type. |
112 template <typename Key, typename Value> | 33 template <typename Key, typename Value> |
113 struct MapTraits<Key, Value, true> { | 34 struct MapTraits<Key, Value, true> { |
114 // Map keys can't be move only types. | 35 static inline void Clone(const std::map<Key, Value>& src, |
115 static_assert(!internal::IsMoveOnlyType<Key>::value, | 36 std::map<Key, Value>* dst) { |
116 "Map keys cannot be move only types."); | |
117 | |
118 typedef Key KeyStorageType; | |
119 typedef Key& KeyRefType; | |
120 typedef const Key& KeyConstRefType; | |
121 typedef KeyConstRefType KeyForwardType; | |
122 | |
123 struct ValueStorageType { | |
124 // Make 8-byte aligned. | |
125 char buf[sizeof(Value) + (8 - (sizeof(Value) % 8)) % 8]; | |
126 }; | |
127 typedef Value& ValueRefType; | |
128 typedef const Value& ValueConstRefType; | |
129 typedef Value ValueForwardType; | |
130 | |
131 static inline void InitializeFrom( | |
132 std::map<KeyStorageType, ValueStorageType>* m, | |
133 mojo::Array<Key> keys, | |
134 mojo::Array<Value> values) { | |
135 for (size_t i = 0; i < keys.size(); ++i) | |
136 Insert(m, keys[i], values[i]); | |
137 } | |
138 static inline void Decompose(std::map<KeyStorageType, ValueStorageType>* m, | |
139 mojo::Array<Key>* keys, | |
140 mojo::Array<Value>* values) { | |
141 keys->resize(m->size()); | |
142 values->resize(m->size()); | |
143 int i = 0; | |
144 for (typename std::map<KeyStorageType, ValueStorageType>::iterator | |
145 it = m->begin(); | |
146 it != m->end(); | |
147 ++it, ++i) { | |
148 (*keys)[i] = it->first; | |
149 (*values)[i] = std::move(GetValue(it)); | |
150 } | |
151 } | |
152 static inline void Finalize(std::map<KeyStorageType, ValueStorageType>* m) { | |
153 for (auto& pair : *m) | |
154 reinterpret_cast<Value*>(pair.second.buf)->~Value(); | |
155 } | |
156 static inline ValueRefType at(std::map<KeyStorageType, ValueStorageType>* m, | |
157 KeyForwardType key) { | |
158 // We don't have C++11 library support yet, so we have to emulate the crash | |
159 // on a non-existent key. | |
160 auto it = m->find(key); | |
161 MOJO_CHECK(it != m->end()); | |
162 return GetValue(it); | |
163 } | |
164 static inline ValueConstRefType at( | |
165 const std::map<KeyStorageType, ValueStorageType>* m, | |
166 KeyForwardType key) { | |
167 // We don't have C++11 library support yet, so we have to emulate the crash | |
168 // on a non-existent key. | |
169 auto it = m->find(key); | |
170 MOJO_CHECK(it != m->end()); | |
171 return GetValue(it); | |
172 } | |
173 static inline ValueRefType GetOrInsert( | |
174 std::map<KeyStorageType, ValueStorageType>* m, | |
175 KeyForwardType key) { | |
176 // This is the backing for the index operator (operator[]). | |
177 auto it = m->find(key); | |
178 if (it == m->end()) { | |
179 it = m->insert(std::make_pair(key, ValueStorageType())).first; | |
180 new (it->second.buf) Value(); | |
181 } | |
182 | |
183 return GetValue(it); | |
184 } | |
185 static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m, | |
186 KeyForwardType key, | |
187 ValueRefType value) { | |
188 // STL insert() doesn't insert |value| if |key| is already part of |m|. We | |
189 // have to use operator[] to initialize into the storage buffer, but we | |
190 // have to do a manual check so that we don't overwrite an existing object. | |
191 auto it = m->find(key); | |
192 if (it == m->end()) | |
193 new ((*m)[key].buf) Value(std::move(value)); | |
194 } | |
195 static inline KeyConstRefType GetKey( | |
196 const typename std::map<KeyStorageType, ValueStorageType>::const_iterator& | |
197 it) { | |
198 return it->first; | |
199 } | |
200 static inline ValueConstRefType GetValue( | |
201 const typename std::map<KeyStorageType, ValueStorageType>::const_iterator& | |
202 it) { | |
203 return *reinterpret_cast<const Value*>(it->second.buf); | |
204 } | |
205 static inline ValueRefType GetValue( | |
206 const typename std::map<KeyStorageType, ValueStorageType>::iterator& it) { | |
207 return *reinterpret_cast<Value*>(it->second.buf); | |
208 } | |
209 static inline void Clone( | |
210 const std::map<KeyStorageType, ValueStorageType>& src, | |
211 std::map<KeyStorageType, ValueStorageType>* dst) { | |
212 Finalize(dst); | |
213 dst->clear(); | 37 dst->clear(); |
214 for (auto it = src.begin(); it != src.end(); ++it) | 38 for (auto it = src.begin(); it != src.end(); ++it) |
215 new ((*dst)[it->first].buf) Value(GetValue(it).Clone()); | 39 dst->insert(std::make_pair(it->first, it->second.Clone())); |
216 } | 40 } |
217 }; | 41 }; |
218 | 42 |
219 } // namespace internal | 43 } // namespace internal |
220 } // namespace mojo | 44 } // namespace mojo |
221 | 45 |
222 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_INTERNAL_H_ | 46 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_INTERNAL_H_ |
OLD | NEW |