| 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 | |
| 10 #include <map> | 9 #include <map> |
| 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 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 mojo::Array<Key>* keys, | 139 mojo::Array<Key>* keys, |
| 140 mojo::Array<Value>* values) { | 140 mojo::Array<Value>* values) { |
| 141 keys->resize(m->size()); | 141 keys->resize(m->size()); |
| 142 values->resize(m->size()); | 142 values->resize(m->size()); |
| 143 int i = 0; | 143 int i = 0; |
| 144 for (typename std::map<KeyStorageType, ValueStorageType>::iterator | 144 for (typename std::map<KeyStorageType, ValueStorageType>::iterator |
| 145 it = m->begin(); | 145 it = m->begin(); |
| 146 it != m->end(); | 146 it != m->end(); |
| 147 ++it, ++i) { | 147 ++it, ++i) { |
| 148 (*keys)[i] = it->first; | 148 (*keys)[i] = it->first; |
| 149 (*values)[i] = GetValue(it).Pass(); | 149 (*values)[i] = std::move(GetValue(it)); |
| 150 } | 150 } |
| 151 } | 151 } |
| 152 static inline void Finalize(std::map<KeyStorageType, ValueStorageType>* m) { | 152 static inline void Finalize(std::map<KeyStorageType, ValueStorageType>* m) { |
| 153 for (auto& pair : *m) | 153 for (auto& pair : *m) |
| 154 reinterpret_cast<Value*>(pair.second.buf)->~Value(); | 154 reinterpret_cast<Value*>(pair.second.buf)->~Value(); |
| 155 } | 155 } |
| 156 static inline ValueRefType at(std::map<KeyStorageType, ValueStorageType>* m, | 156 static inline ValueRefType at(std::map<KeyStorageType, ValueStorageType>* m, |
| 157 KeyForwardType key) { | 157 KeyForwardType key) { |
| 158 // We don't have C++11 library support yet, so we have to emulate the crash | 158 // We don't have C++11 library support yet, so we have to emulate the crash |
| 159 // on a non-existent key. | 159 // on a non-existent key. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 183 return GetValue(it); | 183 return GetValue(it); |
| 184 } | 184 } |
| 185 static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m, | 185 static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m, |
| 186 KeyForwardType key, | 186 KeyForwardType key, |
| 187 ValueRefType value) { | 187 ValueRefType value) { |
| 188 // STL insert() doesn't insert |value| if |key| is already part of |m|. We | 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 | 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. | 190 // have to do a manual check so that we don't overwrite an existing object. |
| 191 auto it = m->find(key); | 191 auto it = m->find(key); |
| 192 if (it == m->end()) | 192 if (it == m->end()) |
| 193 new ((*m)[key].buf) Value(value.Pass()); | 193 new ((*m)[key].buf) Value(std::move(value)); |
| 194 } | 194 } |
| 195 static inline KeyConstRefType GetKey( | 195 static inline KeyConstRefType GetKey( |
| 196 const typename std::map<KeyStorageType, ValueStorageType>::const_iterator& | 196 const typename std::map<KeyStorageType, ValueStorageType>::const_iterator& |
| 197 it) { | 197 it) { |
| 198 return it->first; | 198 return it->first; |
| 199 } | 199 } |
| 200 static inline ValueConstRefType GetValue( | 200 static inline ValueConstRefType GetValue( |
| 201 const typename std::map<KeyStorageType, ValueStorageType>::const_iterator& | 201 const typename std::map<KeyStorageType, ValueStorageType>::const_iterator& |
| 202 it) { | 202 it) { |
| 203 return *reinterpret_cast<const Value*>(it->second.buf); | 203 return *reinterpret_cast<const Value*>(it->second.buf); |
| 204 } | 204 } |
| 205 static inline ValueRefType GetValue( | 205 static inline ValueRefType GetValue( |
| 206 const typename std::map<KeyStorageType, ValueStorageType>::iterator& it) { | 206 const typename std::map<KeyStorageType, ValueStorageType>::iterator& it) { |
| 207 return *reinterpret_cast<Value*>(it->second.buf); | 207 return *reinterpret_cast<Value*>(it->second.buf); |
| 208 } | 208 } |
| 209 static inline void Clone( | 209 static inline void Clone( |
| 210 const std::map<KeyStorageType, ValueStorageType>& src, | 210 const std::map<KeyStorageType, ValueStorageType>& src, |
| 211 std::map<KeyStorageType, ValueStorageType>* dst) { | 211 std::map<KeyStorageType, ValueStorageType>* dst) { |
| 212 Finalize(dst); | 212 Finalize(dst); |
| 213 dst->clear(); | 213 dst->clear(); |
| 214 for (auto it = src.begin(); it != src.end(); ++it) | 214 for (auto it = src.begin(); it != src.end(); ++it) |
| 215 new ((*dst)[it->first].buf) Value(GetValue(it).Clone()); | 215 new ((*dst)[it->first].buf) Value(GetValue(it).Clone()); |
| 216 } | 216 } |
| 217 }; | 217 }; |
| 218 | 218 |
| 219 } // namespace internal | 219 } // namespace internal |
| 220 } // namespace mojo | 220 } // namespace mojo |
| 221 | 221 |
| 222 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_INTERNAL_H_ | 222 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_INTERNAL_H_ |
| OLD | NEW |