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 template <typename Key, typename Value> | |
15 class Map { | |
16 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(Map, RValue) | |
17 public: | |
18 // Map keys can not be move only classes. | |
19 static_assert(!internal::IsMoveOnlyType<Key>::value, | |
20 "Map keys can not be move only types."); | |
21 | |
22 typedef internal::MapTraits<Key, | |
23 Value, | |
24 internal::IsMoveOnlyType<Value>::value> Traits; | |
25 typedef typename Traits::KeyStorageType KeyStorageType; | |
26 typedef typename Traits::KeyRefType KeyRefType; | |
27 typedef typename Traits::KeyConstRefType KeyConstRefType; | |
28 typedef typename Traits::KeyForwardType KeyForwardType; | |
29 | |
30 typedef typename Traits::ValueStorageType ValueStorageType; | |
31 typedef typename Traits::ValueRefType ValueRefType; | |
32 typedef typename Traits::ValueConstRefType ValueConstRefType; | |
33 typedef typename Traits::ValueForwardType ValueForwardType; | |
34 | |
35 typedef internal::Map_Data<typename internal::WrapperTraits<Key>::DataType, | |
36 typename internal::WrapperTraits<Value>::DataType> | |
37 Data_; | |
38 | |
39 Map() : is_null_(true) {} | |
40 | |
41 Map(mojo::Array<Key> keys, mojo::Array<Value> values) : is_null_(false) { | |
42 MOJO_DCHECK(keys.size() == values.size()); | |
43 Traits::InitializeFrom(&map_, keys.Pass(), values.Pass()); | |
44 } | |
45 | |
46 ~Map() { Traits::Finalize(&map_); } | |
47 | |
48 Map(RValue other) : is_null_(true) { Take(other.object); } | |
49 Map& operator=(RValue other) { | |
50 Take(other.object); | |
51 return *this; | |
52 } | |
53 | |
54 template <typename U> | |
55 static Map From(const U& other) { | |
56 return TypeConverter<Map, U>::Convert(other); | |
57 } | |
58 | |
59 template <typename U> | |
60 U To() const { | |
61 return TypeConverter<U, Map>::Convert(*this); | |
62 } | |
63 | |
64 void reset() { | |
65 if (!map_.empty()) { | |
66 Traits::Finalize(&map_); | |
67 map_.clear(); | |
68 } | |
69 is_null_ = true; | |
70 } | |
71 | |
72 bool is_null() const { return is_null_; } | |
73 | |
74 size_t size() const { return map_.size(); } | |
75 | |
76 // Inserts a key-value pair into the mpa. Like std::map, this does not insert | |
yzshen1
2014/10/08 21:58:33
mpa -> map
| |
77 // |value| if |key| is already a member of the map. | |
78 void insert(KeyForwardType key, ValueForwardType value) { | |
yzshen1
2014/10/08 21:58:33
Do we also want a remove ?
Elliot Glaysher
2014/10/08 23:40:49
I'm not sure. It was unused in this patch, and I'm
yzshen1
2014/10/09 17:41:58
That is fine. I am pretty sure people will want it
| |
79 is_null_ = false; | |
80 Traits::Insert(&map_, key, value); | |
81 } | |
82 | |
83 ValueRefType at(KeyForwardType key) { return Traits::at(&map_, key); } | |
84 ValueConstRefType at(KeyForwardType key) const { | |
85 return Traits::at(&map_, key); | |
86 } | |
87 | |
88 void Swap(Map<Key, Value>* other) { | |
89 std::swap(is_null_, other->is_null_); | |
90 map_.swap(other->map_); | |
91 } | |
92 void Swap(std::map<Key, Value>* other) { | |
93 is_null_ = false; | |
94 map_.swap(*other); | |
95 } | |
96 | |
97 // This moves all values in the map to a set of parallel arrays. This action | |
98 // is destructive because we can have move-only objects as values; therefore | |
99 // we can't have copy semantics here. | |
100 void DecomposeMapTo(mojo::Array<Key>* keys, mojo::Array<Value>* values) { | |
101 Traits::Decompose(&map_, keys, values); | |
102 Traits::Finalize(&map_); | |
103 map_.clear(); | |
104 is_null_ = true; | |
105 } | |
106 | |
107 // Please note that calling this method will fail compilation if the element | |
yzshen1
2014/10/08 21:58:33
nit: element -> value
| |
108 // type cannot be cloned (which usually means that it is a Mojo handle type or | |
109 // a type contains Mojo handles). | |
110 Map Clone() const { | |
111 Map result; | |
112 result.is_null_ = is_null_; | |
113 Traits::Clone(map_, &result.map_); | |
114 return result.Pass(); | |
115 } | |
116 | |
117 class ConstMapIterator { | |
118 public: | |
119 ConstMapIterator( | |
120 const typename std::map<KeyStorageType, | |
121 ValueStorageType>::const_iterator& it) | |
122 : it_(it) {} | |
123 | |
124 KeyConstRefType GetKey() { return Traits::GetKey(it_); } | |
125 ValueConstRefType GetValue() { return Traits::GetValue(it_); } | |
126 | |
127 ConstMapIterator& operator++() { | |
128 it_++; | |
129 return *this; | |
130 } | |
131 bool operator!=(const ConstMapIterator& rhs) const { | |
132 return it_ != rhs.it_; | |
133 } | |
134 bool operator==(const ConstMapIterator& rhs) const { | |
135 return it_ == rhs.it_; | |
136 } | |
137 | |
138 private: | |
139 typename std::map<KeyStorageType, ValueStorageType>::const_iterator it_; | |
140 }; | |
141 | |
142 // Provide read-only iteration over map members. | |
143 ConstMapIterator begin() const { return ConstMapIterator(map_.begin()); } | |
144 ConstMapIterator end() const { return ConstMapIterator(map_.end()); } | |
145 | |
146 ConstMapIterator find(KeyForwardType key) const { | |
147 return ConstMapIterator(map_.find(key)); | |
148 } | |
149 | |
150 private: | |
151 typedef std::map<KeyStorageType, ValueStorageType> Map::*Testable; | |
152 | |
153 public: | |
154 operator Testable() const { return is_null_ ? 0 : &Map::map_; } | |
155 | |
156 private: | |
157 void Take(Map* other) { | |
158 reset(); | |
159 Swap(other); | |
160 } | |
161 | |
162 std::map<KeyStorageType, ValueStorageType> map_; | |
163 bool is_null_; | |
164 }; | |
165 | |
166 template <typename MojoKey, | |
167 typename MojoValue, | |
168 typename STLKey, | |
169 typename STLValue> | |
170 struct TypeConverter<Map<MojoKey, MojoValue>, std::map<STLKey, STLValue>> { | |
171 static Map<MojoKey, MojoValue> Convert( | |
172 const std::map<STLKey, STLValue>& input) { | |
173 Map<MojoKey, MojoValue> result; | |
174 for (auto& pair : input) { | |
yzshen1
2014/10/08 21:58:33
In this case, an empty std::map is converted to a
Elliot Glaysher
2014/10/08 23:40:49
I've added an accessor to change |is_null_|. Do yo
yzshen1
2014/10/09 17:41:58
I am a little concerned that it may not be very ob
| |
175 result.insert(TypeConverter<MojoKey, STLKey>::Convert(pair.first), | |
176 TypeConverter<MojoValue, STLValue>::Convert(pair.second)); | |
177 } | |
178 return result.Pass(); | |
179 } | |
180 }; | |
181 | |
182 template <typename MojoKey, | |
183 typename MojoValue, | |
184 typename STLKey, | |
185 typename STLValue> | |
186 struct TypeConverter<std::map<STLKey, STLValue>, Map<MojoKey, MojoValue>> { | |
187 static std::map<STLKey, STLValue> Convert( | |
188 const Map<MojoKey, MojoValue>& input) { | |
189 std::map<STLKey, STLValue> result; | |
190 if (!input.is_null()) { | |
191 for (auto it = input.begin(); it != input.end(); ++it) { | |
192 result.insert(std::make_pair( | |
193 TypeConverter<STLKey, MojoKey>::Convert(it.GetKey()), | |
194 TypeConverter<STLValue, MojoValue>::Convert(it.GetValue()))); | |
195 } | |
196 } | |
197 return result; | |
198 } | |
199 }; | |
200 | |
201 } // namespace mojo | |
202 | |
203 #endif // MOJO_PUBLIC_CPP_BINDINGS_MAP_H_ | |
OLD | NEW |