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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 it != m->end(); | 50 it != m->end(); |
51 ++it, ++i) { | 51 ++it, ++i) { |
52 (*keys)[i] = it->first; | 52 (*keys)[i] = it->first; |
53 (*values)[i] = it->second; | 53 (*values)[i] = it->second; |
54 } | 54 } |
55 } | 55 } |
56 static inline void Finalize(std::map<KeyStorageType, ValueStorageType>* m) {} | 56 static inline void Finalize(std::map<KeyStorageType, ValueStorageType>* m) {} |
57 static inline ValueRefType at(std::map<KeyStorageType, ValueStorageType>* m, | 57 static inline ValueRefType at(std::map<KeyStorageType, ValueStorageType>* m, |
58 KeyForwardType key) { | 58 KeyForwardType key) { |
59 // We don't have C++11 library support yet, so we have to emulate the crash | 59 // We don't have C++11 library support yet, so we have to emulate the crash |
60 // on a non-existant key. | 60 // on a non-existent key. |
61 auto it = m->find(key); | 61 auto it = m->find(key); |
62 MOJO_CHECK(it != m->end()); | 62 MOJO_CHECK(it != m->end()); |
63 return it->second; | 63 return it->second; |
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-existent 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 GetOrInsert( |
| 75 std::map<KeyStorageType, ValueStorageType>* m, |
| 76 KeyForwardType key) { |
| 77 // This is the backing for the index operator (operator[]). |
| 78 return (*m)[key]; |
| 79 } |
74 static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m, | 80 static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m, |
75 KeyForwardType key, | 81 KeyForwardType key, |
76 ValueForwardType value) { | 82 ValueForwardType value) { |
77 m->insert(std::make_pair(key, value)); | 83 m->insert(std::make_pair(key, value)); |
78 } | 84 } |
79 static inline KeyConstRefType GetKey( | 85 static inline KeyConstRefType GetKey( |
80 const typename std::map<KeyStorageType, ValueStorageType>::const_iterator& | 86 const typename std::map<KeyStorageType, ValueStorageType>::const_iterator& |
81 it) { | 87 it) { |
82 return it->first; | 88 return it->first; |
83 } | 89 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 (*values)[i] = GetValue(it).Pass(); | 145 (*values)[i] = GetValue(it).Pass(); |
140 } | 146 } |
141 } | 147 } |
142 static inline void Finalize(std::map<KeyStorageType, ValueStorageType>* m) { | 148 static inline void Finalize(std::map<KeyStorageType, ValueStorageType>* m) { |
143 for (auto& pair : *m) | 149 for (auto& pair : *m) |
144 reinterpret_cast<Value*>(pair.second.buf)->~Value(); | 150 reinterpret_cast<Value*>(pair.second.buf)->~Value(); |
145 } | 151 } |
146 static inline ValueRefType at(std::map<KeyStorageType, ValueStorageType>* m, | 152 static inline ValueRefType at(std::map<KeyStorageType, ValueStorageType>* m, |
147 KeyForwardType key) { | 153 KeyForwardType key) { |
148 // We don't have C++11 library support yet, so we have to emulate the crash | 154 // We don't have C++11 library support yet, so we have to emulate the crash |
149 // on a non-existant key. | 155 // on a non-existent key. |
150 auto it = m->find(key); | 156 auto it = m->find(key); |
151 MOJO_CHECK(it != m->end()); | 157 MOJO_CHECK(it != m->end()); |
152 return GetValue(it); | 158 return GetValue(it); |
153 } | 159 } |
154 static inline ValueConstRefType at( | 160 static inline ValueConstRefType at( |
155 const std::map<KeyStorageType, ValueStorageType>* m, | 161 const std::map<KeyStorageType, ValueStorageType>* m, |
156 KeyForwardType key) { | 162 KeyForwardType key) { |
157 // We don't have C++11 library support yet, so we have to emulate the crash | 163 // We don't have C++11 library support yet, so we have to emulate the crash |
158 // on a non-existant key. | 164 // on a non-existent key. |
159 auto it = m->find(key); | 165 auto it = m->find(key); |
160 MOJO_CHECK(it != m->end()); | 166 MOJO_CHECK(it != m->end()); |
161 return GetValue(it); | 167 return GetValue(it); |
162 } | 168 } |
| 169 static inline ValueRefType GetOrInsert( |
| 170 std::map<KeyStorageType, ValueStorageType>* m, |
| 171 KeyForwardType key) { |
| 172 // This is the backing for the index operator (operator[]). |
| 173 auto it = m->find(key); |
| 174 if (it == m->end()) { |
| 175 it = m->insert(std::make_pair(key, ValueStorageType())).first; |
| 176 new (it->second.buf) Value(); |
| 177 } |
| 178 |
| 179 return GetValue(it); |
| 180 } |
163 static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m, | 181 static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m, |
164 KeyForwardType key, | 182 KeyForwardType key, |
165 ValueRefType value) { | 183 ValueRefType value) { |
166 // STL insert() doesn't insert |value| if |key| is already part of |m|. We | 184 // 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 | 185 // 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. | 186 // have to do a manual check so that we don't overwrite an existing object. |
169 auto it = m->find(key); | 187 auto it = m->find(key); |
170 if (it == m->end()) | 188 if (it == m->end()) |
171 new ((*m)[key].buf) Value(value.Pass()); | 189 new ((*m)[key].buf) Value(value.Pass()); |
172 } | 190 } |
(...skipping 18 matching lines...) Expand all Loading... |
191 dst->clear(); | 209 dst->clear(); |
192 for (auto it = src.begin(); it != src.end(); ++it) | 210 for (auto it = src.begin(); it != src.end(); ++it) |
193 new ((*dst)[it->first].buf) Value(GetValue(it).Clone()); | 211 new ((*dst)[it->first].buf) Value(GetValue(it).Clone()); |
194 } | 212 } |
195 }; | 213 }; |
196 | 214 |
197 } // namespace internal | 215 } // namespace internal |
198 } // namespace mojo | 216 } // namespace mojo |
199 | 217 |
200 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_INTERNAL_H_ | 218 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_INTERNAL_H_ |
OLD | NEW |