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 <map> | 8 #include <map> |
9 | 9 |
10 #include "mojo/public/cpp/bindings/array.h" | 10 #include "mojo/public/cpp/bindings/array.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 } | 64 } |
65 static inline ValueConstRefType at( | 65 static inline ValueConstRefType at( |
66 const std::map<KeyStorageType, ValueStorageType>* m, | 66 const std::map<KeyStorageType, ValueStorageType>* m, |
67 KeyForwardType key) { | 67 KeyForwardType key) { |
68 // We don't have C++11 library support yet, so we have to emulate the crash | 68 // We don't have C++11 library support yet, so we have to emulate the crash |
69 // on a non-existant key. | 69 // on a non-existant key. |
70 auto it = m->find(key); | 70 auto it = m->find(key); |
71 MOJO_CHECK(it != m->end()); | 71 MOJO_CHECK(it != m->end()); |
72 return it->second; | 72 return it->second; |
73 } | 73 } |
74 static inline ValueRefType Index( | |
viettrungluu
2014/10/15 23:17:23
Hmmm. I still find the name "Index" a bit perplexi
Elliot Glaysher
2014/10/15 23:30:44
Because it is the backing for the index operator.
| |
75 std::map<KeyStorageType, ValueStorageType>* m, | |
76 KeyForwardType key) { | |
77 return (*m)[key]; | |
78 } | |
74 static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m, | 79 static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m, |
75 KeyForwardType key, | 80 KeyForwardType key, |
76 ValueForwardType value) { | 81 ValueForwardType value) { |
77 m->insert(std::make_pair(key, value)); | 82 m->insert(std::make_pair(key, value)); |
78 } | 83 } |
79 static inline KeyConstRefType GetKey( | 84 static inline KeyConstRefType GetKey( |
80 const typename std::map<KeyStorageType, ValueStorageType>::const_iterator& | 85 const typename std::map<KeyStorageType, ValueStorageType>::const_iterator& |
81 it) { | 86 it) { |
82 return it->first; | 87 return it->first; |
83 } | 88 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 // We don't have C++11 library support yet, so we have to emulate the crash | 153 // We don't have C++11 library support yet, so we have to emulate the crash |
149 // on a non-existant key. | 154 // on a non-existant key. |
150 auto it = m->find(key); | 155 auto it = m->find(key); |
151 MOJO_CHECK(it != m->end()); | 156 MOJO_CHECK(it != m->end()); |
152 return GetValue(it); | 157 return GetValue(it); |
153 } | 158 } |
154 static inline ValueConstRefType at( | 159 static inline ValueConstRefType at( |
155 const std::map<KeyStorageType, ValueStorageType>* m, | 160 const std::map<KeyStorageType, ValueStorageType>* m, |
156 KeyForwardType key) { | 161 KeyForwardType key) { |
157 // We don't have C++11 library support yet, so we have to emulate the crash | 162 // We don't have C++11 library support yet, so we have to emulate the crash |
158 // on a non-existant key. | 163 // on a non-existant key. |
viettrungluu
2014/10/15 23:17:23
drive-by retroactive correction: "existant" -> "ex
| |
159 auto it = m->find(key); | 164 auto it = m->find(key); |
160 MOJO_CHECK(it != m->end()); | 165 MOJO_CHECK(it != m->end()); |
161 return GetValue(it); | 166 return GetValue(it); |
162 } | 167 } |
168 static inline ValueRefType Index( | |
169 std::map<KeyStorageType, ValueStorageType>* m, | |
170 KeyForwardType key) { | |
171 auto it = m->find(key); | |
172 if (it == m->end()) { | |
173 it = m->insert(std::make_pair(key, ValueStorageType())).first; | |
174 new (it->second.buf) Value(); | |
175 } | |
176 | |
177 return GetValue(it); | |
178 } | |
163 static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m, | 179 static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m, |
164 KeyForwardType key, | 180 KeyForwardType key, |
165 ValueRefType value) { | 181 ValueRefType value) { |
166 // STL insert() doesn't insert |value| if |key| is already part of |m|. We | 182 // STL insert() doesn't insert |value| if |key| is already part of |m|. We |
167 // have to use operator[] to initialize into the storage buffer, but we | 183 // have to use operator[] to initialize into the storage buffer, but we |
168 // have to do a manual check so that we don't overwrite an existing object. | 184 // have to do a manual check so that we don't overwrite an existing object. |
169 auto it = m->find(key); | 185 auto it = m->find(key); |
170 if (it == m->end()) | 186 if (it == m->end()) |
171 new ((*m)[key].buf) Value(value.Pass()); | 187 new ((*m)[key].buf) Value(value.Pass()); |
172 } | 188 } |
(...skipping 18 matching lines...) Expand all Loading... | |
191 dst->clear(); | 207 dst->clear(); |
192 for (auto it = src.begin(); it != src.end(); ++it) | 208 for (auto it = src.begin(); it != src.end(); ++it) |
193 new ((*dst)[it->first].buf) Value(GetValue(it).Clone()); | 209 new ((*dst)[it->first].buf) Value(GetValue(it).Clone()); |
194 } | 210 } |
195 }; | 211 }; |
196 | 212 |
197 } // namespace internal | 213 } // namespace internal |
198 } // namespace mojo | 214 } // namespace mojo |
199 | 215 |
200 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_INTERNAL_H_ | 216 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_INTERNAL_H_ |
OLD | NEW |