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