| Index: mojo/public/cpp/bindings/map.h
|
| diff --git a/mojo/public/cpp/bindings/map.h b/mojo/public/cpp/bindings/map.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a29557a6bc54e61f437427ed23136d94097a43f0
|
| --- /dev/null
|
| +++ b/mojo/public/cpp/bindings/map.h
|
| @@ -0,0 +1,154 @@
|
| +// 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_MAP_H_
|
| +#define MOJO_PUBLIC_CPP_BINDINGS_MAP_H_
|
| +
|
| +#include <map>
|
| +
|
| +#include "mojo/public/cpp/bindings/lib/map_internal.h"
|
| +
|
| +namespace mojo {
|
| +
|
| +// Provides read-only access to map data.
|
| +template <typename Key, typename Value>
|
| +class Map {
|
| + MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(Map, RValue)
|
| + public:
|
| + // Map keys can not be move only classes.
|
| + MOJO_COMPILE_ASSERT(!internal::IsMoveOnlyType<Key>::value,
|
| + map_key_cant_be_move_only);
|
| +
|
| + typedef internal::MapTraits<Key, Value,
|
| + internal::IsMoveOnlyType<Value>::value> Traits;
|
| + typedef typename Traits::KeyStorageType KeyStorageType;
|
| + typedef typename Traits::KeyForwardType KeyForwardType;
|
| + typedef typename Traits::ValueStorageType ValueStorageType;
|
| + typedef typename Traits::ValueRefType ValueRefType;
|
| + typedef typename Traits::ValueForwardType ValueForwardType;
|
| +
|
| + Map() : is_null_(true) {}
|
| +
|
| + Map(mojo::Array<Key> keys, mojo::Array<Value> values)
|
| + : is_null_(false) {
|
| + MOJO_DCHECK(keys.size() == values.size());
|
| + Traits::InitializeFrom(&map_, keys.Pass(), values.Pass());
|
| + }
|
| +
|
| + ~Map() {
|
| + Traits::Finalize(&map_);
|
| + }
|
| +
|
| + Map(RValue other) : is_null_(true) { Take(other.object); }
|
| + Map& operator=(RValue other) {
|
| + Take(other.object);
|
| + return *this;
|
| + }
|
| +
|
| + template <typename U>
|
| + static Map From(const U& other) {
|
| + return TypeConverter<Map, U>::Convert(other);
|
| + }
|
| +
|
| + template <typename U>
|
| + U To() const {
|
| + return TypeConverter<U, Map>::Convert(*this);
|
| + }
|
| +
|
| + void reset() {
|
| + if (!map_.empty()) {
|
| + Traits::Finalize(&map_);
|
| + map_.clear();
|
| + }
|
| + is_null_ = true;
|
| + }
|
| +
|
| + bool is_null() const { return is_null_; }
|
| +
|
| + size_t size() const { return map_.size(); }
|
| +
|
| + void insert(KeyForwardType key, ValueForwardType value) {
|
| + is_null_ = false;
|
| + Traits::Insert(&map_, key, value);
|
| + }
|
| +
|
| + ValueRefType at(KeyForwardType key) { return Traits::at(&map_, key); }
|
| +
|
| + const std::map<KeyStorageType, ValueStorageType>& storage() const {
|
| + return map_;
|
| + }
|
| + operator const std::map<KeyStorageType, ValueStorageType>&() const {
|
| + return map_;
|
| + }
|
| +
|
| + void Swap(Map<Key, Value>* other) {
|
| + std::swap(is_null_, other->is_null_);
|
| + map_.swap(other->map_);
|
| + }
|
| + void Swap(std::map<Key, Value>* other) {
|
| + is_null_ = false;
|
| + map_.swap(*other);
|
| + }
|
| +
|
| + // This moves all values in the map to a set of parallel arrays. This action
|
| + // is destructive because we can have move-only objects as values; therefore
|
| + // we can't have copy semantics here.
|
| + void DecomposeMapTo(mojo::Array<Key>* keys, mojo::Array<Value>* values) {
|
| + Traits::Decompose(&map_, keys, values);
|
| + Traits::Finalize(&map_);
|
| + map_.clear();
|
| + is_null_ = true;
|
| + }
|
| +
|
| + private:
|
| + typedef std::map<KeyStorageType, ValueStorageType> Map::*Testable;
|
| +
|
| + public:
|
| + operator Testable() const { return is_null_ ? 0 : &Map::map_; }
|
| +
|
| + private:
|
| + void Take(Map* other) {
|
| + reset();
|
| + Swap(other);
|
| + }
|
| +
|
| + std::map<KeyStorageType, ValueStorageType> map_;
|
| + bool is_null_;
|
| +};
|
| +
|
| +template <typename MojoKey, typename MojoValue,
|
| + typename STLKey, typename STLValue>
|
| +struct TypeConverter<Map<MojoKey, MojoValue>, std::map<STLKey, STLValue> > {
|
| + static Map<MojoKey, MojoValue> Convert(
|
| + const std::map<STLKey, STLValue>& input) {
|
| + Map<MojoKey, MojoValue> result;
|
| + for (auto& pair : input) {
|
| + result.insert(TypeConverter<MojoKey, STLKey>::Convert(pair.first),
|
| + TypeConverter<MojoValue, STLValue>::Convert(pair.second));
|
| + }
|
| + return result.Pass();
|
| + }
|
| +};
|
| +
|
| +template <typename MojoKey, typename MojoValue,
|
| + typename STLKey, typename STLValue>
|
| +struct TypeConverter<std::map<STLKey, STLValue>, Map<MojoKey, MojoValue> > {
|
| + static std::map<STLKey, STLValue> Convert(
|
| + const Map<MojoKey, MojoValue>& input) {
|
| + std::map<STLKey, STLValue> result;
|
| + if (!input.is_null()) {
|
| + // We come back to the problem of iteration.
|
| + for (auto& pair : input.storage()) {
|
| + result.insert(std::make_pair(
|
| + TypeConverter<STLKey, MojoKey>::Convert(pair.first),
|
| + TypeConverter<STLValue, MojoValue>::Convert(pair.second)));
|
| + }
|
| + }
|
| + return result;
|
| + }
|
| +};
|
| +
|
| +} // namespace mojo
|
| +
|
| +#endif // MOJO_PUBLIC_CPP_BINDINGS_MAP_H_
|
|
|