OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_MAP_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_MAP_H_ |
| 7 |
| 8 #include <map> |
| 9 |
| 10 #include "mojo/public/cpp/bindings/lib/map_internal.h" |
| 11 |
| 12 namespace mojo { |
| 13 |
| 14 // Provides read-only access to map data. |
| 15 template <typename Key, typename Value> |
| 16 class Map { |
| 17 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(Map, RValue) |
| 18 public: |
| 19 // Map keys can not be move only classes. |
| 20 MOJO_COMPILE_ASSERT(!internal::IsMoveOnlyType<Key>::value, |
| 21 map_key_cant_be_move_only); |
| 22 |
| 23 typedef internal::MapTraits<Key, Value, |
| 24 internal::IsMoveOnlyType<Value>::value> Traits; |
| 25 typedef typename Traits::KeyStorageType KeyStorageType; |
| 26 typedef typename Traits::KeyForwardType KeyForwardType; |
| 27 typedef typename Traits::ValueStorageType ValueStorageType; |
| 28 typedef typename Traits::ValueRefType ValueRefType; |
| 29 typedef typename Traits::ValueForwardType ValueForwardType; |
| 30 |
| 31 Map() : is_null_(true) {} |
| 32 |
| 33 Map(mojo::Array<Key> keys, mojo::Array<Value> values) |
| 34 : is_null_(false) { |
| 35 MOJO_DCHECK(keys.size() == values.size()); |
| 36 Traits::InitializeFrom(&map_, keys.Pass(), values.Pass()); |
| 37 } |
| 38 |
| 39 ~Map() { |
| 40 Traits::Finalize(&map_); |
| 41 } |
| 42 |
| 43 Map(RValue other) : is_null_(true) { Take(other.object); } |
| 44 Map& operator=(RValue other) { |
| 45 Take(other.object); |
| 46 return *this; |
| 47 } |
| 48 |
| 49 template <typename U> |
| 50 static Map From(const U& other) { |
| 51 return TypeConverter<Map, U>::Convert(other); |
| 52 } |
| 53 |
| 54 template <typename U> |
| 55 U To() const { |
| 56 return TypeConverter<U, Map>::Convert(*this); |
| 57 } |
| 58 |
| 59 void reset() { |
| 60 if (!map_.empty()) { |
| 61 Traits::Finalize(&map_); |
| 62 map_.clear(); |
| 63 } |
| 64 is_null_ = true; |
| 65 } |
| 66 |
| 67 bool is_null() const { return is_null_; } |
| 68 |
| 69 size_t size() const { return map_.size(); } |
| 70 |
| 71 void insert(KeyForwardType key, ValueForwardType value) { |
| 72 is_null_ = false; |
| 73 Traits::Insert(&map_, key, value); |
| 74 } |
| 75 |
| 76 ValueRefType at(KeyForwardType key) { return Traits::at(&map_, key); } |
| 77 |
| 78 const std::map<KeyStorageType, ValueStorageType>& storage() const { |
| 79 return map_; |
| 80 } |
| 81 operator const std::map<KeyStorageType, ValueStorageType>&() const { |
| 82 return map_; |
| 83 } |
| 84 |
| 85 void Swap(Map<Key, Value>* other) { |
| 86 std::swap(is_null_, other->is_null_); |
| 87 map_.swap(other->map_); |
| 88 } |
| 89 void Swap(std::map<Key, Value>* other) { |
| 90 is_null_ = false; |
| 91 map_.swap(*other); |
| 92 } |
| 93 |
| 94 // This moves all values in the map to a set of parallel arrays. This action |
| 95 // is destructive because we can have move-only objects as values; therefore |
| 96 // we can't have copy semantics here. |
| 97 void DecomposeMapTo(mojo::Array<Key>* keys, mojo::Array<Value>* values) { |
| 98 Traits::Decompose(&map_, keys, values); |
| 99 Traits::Finalize(&map_); |
| 100 map_.clear(); |
| 101 is_null_ = true; |
| 102 } |
| 103 |
| 104 private: |
| 105 typedef std::map<KeyStorageType, ValueStorageType> Map::*Testable; |
| 106 |
| 107 public: |
| 108 operator Testable() const { return is_null_ ? 0 : &Map::map_; } |
| 109 |
| 110 private: |
| 111 void Take(Map* other) { |
| 112 reset(); |
| 113 Swap(other); |
| 114 } |
| 115 |
| 116 std::map<KeyStorageType, ValueStorageType> map_; |
| 117 bool is_null_; |
| 118 }; |
| 119 |
| 120 template <typename MojoKey, typename MojoValue, |
| 121 typename STLKey, typename STLValue> |
| 122 struct TypeConverter<Map<MojoKey, MojoValue>, std::map<STLKey, STLValue> > { |
| 123 static Map<MojoKey, MojoValue> Convert( |
| 124 const std::map<STLKey, STLValue>& input) { |
| 125 Map<MojoKey, MojoValue> result; |
| 126 for (auto& pair : input) { |
| 127 result.insert(TypeConverter<MojoKey, STLKey>::Convert(pair.first), |
| 128 TypeConverter<MojoValue, STLValue>::Convert(pair.second)); |
| 129 } |
| 130 return result.Pass(); |
| 131 } |
| 132 }; |
| 133 |
| 134 template <typename MojoKey, typename MojoValue, |
| 135 typename STLKey, typename STLValue> |
| 136 struct TypeConverter<std::map<STLKey, STLValue>, Map<MojoKey, MojoValue> > { |
| 137 static std::map<STLKey, STLValue> Convert( |
| 138 const Map<MojoKey, MojoValue>& input) { |
| 139 std::map<STLKey, STLValue> result; |
| 140 if (!input.is_null()) { |
| 141 // We come back to the problem of iteration. |
| 142 for (auto& pair : input.storage()) { |
| 143 result.insert(std::make_pair( |
| 144 TypeConverter<STLKey, MojoKey>::Convert(pair.first), |
| 145 TypeConverter<STLValue, MojoValue>::Convert(pair.second))); |
| 146 } |
| 147 } |
| 148 return result; |
| 149 } |
| 150 }; |
| 151 |
| 152 } // namespace mojo |
| 153 |
| 154 #endif // MOJO_PUBLIC_CPP_BINDINGS_MAP_H_ |
OLD | NEW |