Chromium Code Reviews| Index: mojo/public/cpp/bindings/lib/map_internal.h |
| diff --git a/mojo/public/cpp/bindings/lib/map_internal.h b/mojo/public/cpp/bindings/lib/map_internal.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c79d235b79b167d06174c9761101f60bea5ad364 |
| --- /dev/null |
| +++ b/mojo/public/cpp/bindings/lib/map_internal.h |
| @@ -0,0 +1,147 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_INTERNAL_H_ |
| +#define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_INTERNAL_H_ |
| + |
| +#include "mojo/public/cpp/bindings/lib/array_internal.h" |
| + |
| +namespace mojo { |
| +namespace internal { |
| + |
| +template <typename Key, typename Value, bool kValueIsMoveOnlyType> |
| +struct MapTraits {}; |
| + |
| +template <typename Key, typename Value> struct MapTraits<Key, Value, false> { |
| + // Map keys can't be move only types. |
| + MOJO_COMPILE_ASSERT(!internal::IsMoveOnlyType<Key>::value, |
|
yzshen1
2014/10/03 18:03:37
(optional) you can use the corresponding C++11 fea
|
| + map_key_cant_be_move_only); |
| + |
| + typedef Key KeyStorageType; |
| + typedef Key& KeyRefType; |
| + typedef const Key& KeyConstRefType; |
| + typedef KeyConstRefType KeyForwardType; |
| + |
| + typedef Value ValueStorageType; |
| + typedef Value& ValueRefType; |
| + typedef const Value& ValueConstRefType; |
| + typedef ValueConstRefType ValueForwardType; |
| + |
| + static inline void InitializeFrom( |
| + std::map<KeyStorageType, ValueStorageType>* m, |
| + mojo::Array<Key> keys, |
| + mojo::Array<Value> values) { |
| + for (size_t i = 0; i < keys.size(); ++i) |
| + Insert(m, keys[i], values[i]); |
| + } |
| + static inline void Decompose( |
| + std::map<KeyStorageType, ValueStorageType>* m, |
| + mojo::Array<Key>* keys, |
| + mojo::Array<Value>* values) { |
| + keys->resize(m->size()); |
| + values->resize(m->size()); |
| + int i = 0; |
| + for (typename std::map<KeyStorageType, ValueStorageType>::iterator it = |
| + m->begin(); it != m->end(); ++it, ++i) { |
| + (*keys)[i] = it->first; |
| + (*values)[i] = it->second; |
| + } |
| + } |
| + static inline void Finalize(std::map<KeyStorageType, ValueStorageType>* m) { |
| + } |
| + static inline ValueRefType at(std::map<KeyStorageType, ValueStorageType>* m, |
|
yzshen1
2014/10/03 18:03:37
Does it also need a const version?
ValueConstRefTy
|
| + KeyForwardType key) { |
| + return (*m)[key]; |
| + } |
| + static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m, |
| + KeyForwardType key, ValueForwardType value) { |
| + m->insert(std::make_pair(key, value)); |
| + } |
| + static inline KeyConstRefType GetKey( |
| + const typename std::map<KeyStorageType, |
| + ValueStorageType>::const_iterator& it) { |
|
yzshen1
2014/10/03 18:03:37
(optional) I feel that it is easy to read if Value
|
| + return it->first; |
| + } |
| + static inline ValueConstRefType GetValue( |
| + const typename std::map<KeyStorageType, |
| + ValueStorageType>::const_iterator& it) { |
| + return it->second; |
| + } |
| +}; |
| + |
| +template <typename Key, typename Value> struct MapTraits<Key, Value, true> { |
| + // Map keys can't be move only types. |
| + MOJO_COMPILE_ASSERT(!internal::IsMoveOnlyType<Key>::value, |
| + map_key_cant_be_move_only); |
| + |
| + typedef Key KeyStorageType; |
| + typedef Key& KeyRefType; |
| + typedef const Key& KeyConstRefType; |
| + typedef KeyConstRefType KeyForwardType; |
| + |
| + struct ValueStorageType { |
| + // Make 8-byte aligned. |
| + char buf[sizeof(Value) + (8 - (sizeof(Value) % 8)) % 8]; |
| + }; |
| + typedef Value& ValueRefType; |
| + typedef const Value& ValueConstRefType; |
| + typedef Value ValueForwardType; |
| + |
| + static inline void InitializeFrom( |
| + std::map<KeyStorageType, ValueStorageType>* m, |
| + mojo::Array<Key> keys, |
| + mojo::Array<Value> values) { |
| + for (size_t i = 0; i < keys.size(); ++i) |
| + new ((*m)[keys[i]].buf) Value(values[i].Pass()); |
| + } |
| + static inline void Decompose( |
| + std::map<KeyStorageType, ValueStorageType>* m, |
| + mojo::Array<Key>* keys, |
| + mojo::Array<Value>* values) { |
| + keys->resize(m->size()); |
| + values->resize(m->size()); |
| + int i = 0; |
| + for (typename std::map<KeyStorageType, ValueStorageType>::iterator it = |
| + m->begin(); it != m->end(); ++it, ++i) { |
| + (*keys)[i] = it->first; |
| + (*values)[i] = reinterpret_cast<Value*>(it->second.buf)->Pass(); |
| + } |
| + } |
| + static inline void Finalize(std::map<KeyStorageType, ValueStorageType>* m) { |
| + for (auto& pair : *m) |
| + reinterpret_cast<Value*>(pair.second.buf)->~Value(); |
| + } |
| + static inline ValueRefType at(std::map<KeyStorageType, ValueStorageType>* m, |
| + KeyForwardType key) { |
| + // We don't have C++11 library support yet, so we have to emulate the crash |
| + // on a non-existant key. |
| + auto it = m->find(key); |
| + MOJO_CHECK(it != m->end()); |
| + return *reinterpret_cast<Value*>(it->second.buf); |
| + } |
| + static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m, |
| + KeyForwardType key, ValueRefType value) { |
| + // STL insert() doesn't insert |value| if |key| is already part of |m|. We |
| + // have to use operator[] to initialize into the storage buffer, but we |
| + // have to do a manual check so that we don't overwrite an existing object. |
| + auto it = m->find(key); |
| + if (it == m->end()) |
| + new ((*m)[key].buf) Value(value.Pass()); |
| + } |
| + static inline KeyConstRefType GetKey( |
| + const typename std::map<KeyStorageType, |
| + ValueStorageType>::const_iterator& it) { |
| + return it->first; |
| + } |
| + static inline ValueConstRefType GetValue( |
| + const typename std::map<KeyStorageType, |
| + ValueStorageType>::const_iterator& it) { |
| + return *reinterpret_cast<const Value*>(it->second.buf); |
| + } |
| +}; |
| + |
| +} // namespace internal |
| +} // namespace mojo |
| + |
| +#endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_INTERNAL_H_ |