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. | |
yzshen1
2014/10/03 18:03:38
What do you mean by it is read-only?
Elliot Glaysher
2014/10/03 21:11:49
This was copied verbatim from array.h, and isn't t
| |
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::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 Map() : is_null_(true) {} | |
36 | |
37 Map(mojo::Array<Key> keys, mojo::Array<Value> values) | |
38 : is_null_(false) { | |
39 MOJO_DCHECK(keys.size() == values.size()); | |
40 Traits::InitializeFrom(&map_, keys.Pass(), values.Pass()); | |
41 } | |
42 | |
43 ~Map() { | |
44 Traits::Finalize(&map_); | |
45 } | |
46 | |
47 Map(RValue other) : is_null_(true) { Take(other.object); } | |
48 Map& operator=(RValue other) { | |
49 Take(other.object); | |
50 return *this; | |
51 } | |
52 | |
53 template <typename U> | |
54 static Map From(const U& other) { | |
55 return TypeConverter<Map, U>::Convert(other); | |
56 } | |
57 | |
58 template <typename U> | |
59 U To() const { | |
60 return TypeConverter<U, Map>::Convert(*this); | |
61 } | |
62 | |
63 void reset() { | |
64 if (!map_.empty()) { | |
65 Traits::Finalize(&map_); | |
66 map_.clear(); | |
67 } | |
68 is_null_ = true; | |
69 } | |
70 | |
71 bool is_null() const { return is_null_; } | |
72 | |
73 size_t size() const { return map_.size(); } | |
74 | |
75 void insert(KeyForwardType key, ValueForwardType value) { | |
yzshen1
2014/10/03 18:03:38
Could you please comment about the behavior when |
| |
76 is_null_ = false; | |
77 Traits::Insert(&map_, key, value); | |
78 } | |
79 | |
80 ValueRefType at(KeyForwardType key) { return Traits::at(&map_, key); } | |
yzshen1
2014/10/03 18:03:38
I think it is good to have a const version.
| |
81 | |
82 void Swap(Map<Key, Value>* other) { | |
83 std::swap(is_null_, other->is_null_); | |
84 map_.swap(other->map_); | |
85 } | |
86 void Swap(std::map<Key, Value>* other) { | |
87 is_null_ = false; | |
88 map_.swap(*other); | |
89 } | |
90 | |
91 // This moves all values in the map to a set of parallel arrays. This action | |
92 // is destructive because we can have move-only objects as values; therefore | |
93 // we can't have copy semantics here. | |
94 void DecomposeMapTo(mojo::Array<Key>* keys, mojo::Array<Value>* values) { | |
95 Traits::Decompose(&map_, keys, values); | |
96 Traits::Finalize(&map_); | |
97 map_.clear(); | |
98 is_null_ = true; | |
99 } | |
100 | |
101 class ConstMapIterator { | |
102 public: | |
103 ConstMapIterator(const typename std::map<KeyStorageType, | |
104 ValueStorageType>::const_iterator& it) | |
105 : it_(it) {} | |
106 | |
107 KeyConstRefType GetKey() { | |
108 return Traits::GetKey(it_); | |
109 } | |
110 ValueConstRefType GetValue() { | |
111 return Traits::GetValue(it_); | |
112 } | |
113 | |
114 ConstMapIterator& operator++() { | |
115 it_++; | |
116 return *this; | |
117 } | |
118 bool operator!=(const ConstMapIterator& rhs) const { | |
119 return it_ != rhs.it_; | |
120 } | |
121 bool operator==(const ConstMapIterator& rhs) const { | |
122 return it_ == rhs.it_; | |
123 } | |
124 | |
125 private: | |
126 typename std::map<KeyStorageType, ValueStorageType>::const_iterator it_; | |
127 }; | |
128 | |
129 // Provide read-only iteration over map members. | |
130 ConstMapIterator begin() const { return ConstMapIterator(map_.begin()); } | |
131 ConstMapIterator end() const { return ConstMapIterator(map_.end()); } | |
132 | |
133 ConstMapIterator find(KeyForwardType key) const { | |
134 return ConstMapIterator(map_.find(key)); | |
135 } | |
136 | |
137 private: | |
138 typedef std::map<KeyStorageType, ValueStorageType> Map::*Testable; | |
139 | |
140 public: | |
141 operator Testable() const { return is_null_ ? 0 : &Map::map_; } | |
142 | |
143 private: | |
144 void Take(Map* other) { | |
145 reset(); | |
146 Swap(other); | |
147 } | |
148 | |
149 std::map<KeyStorageType, ValueStorageType> map_; | |
150 bool is_null_; | |
151 }; | |
152 | |
153 template <typename MojoKey, typename MojoValue, | |
154 typename STLKey, typename STLValue> | |
155 struct TypeConverter<Map<MojoKey, MojoValue>, std::map<STLKey, STLValue> > { | |
156 static Map<MojoKey, MojoValue> Convert( | |
157 const std::map<STLKey, STLValue>& input) { | |
158 Map<MojoKey, MojoValue> result; | |
159 for (auto& pair : input) { | |
160 result.insert(TypeConverter<MojoKey, STLKey>::Convert(pair.first), | |
161 TypeConverter<MojoValue, STLValue>::Convert(pair.second)); | |
162 } | |
163 return result.Pass(); | |
164 } | |
165 }; | |
166 | |
167 namespace internal { | |
168 | |
169 template <typename MojoKey, typename MojoValue, | |
170 typename STLKey, typename STLValue> | |
171 class MapTypeConverterCallback { | |
yzshen1
2014/10/03 18:03:38
What is this for? I didn't see any usage of it.
Elliot Glaysher
2014/10/03 21:11:49
Stray mark. I previously tried a templated Iterate
| |
172 public: | |
173 MapTypeConverterCallback(std::map<STLKey, STLValue>* result_in) | |
174 : result(result_in) {} | |
175 | |
176 void operator()(const MojoKey& key, const MojoValue& value) { | |
177 result->insert(std::make_pair( | |
178 TypeConverter<STLKey, MojoKey>::Convert(key), | |
179 TypeConverter<STLValue, MojoValue>::Convert(value))); | |
180 } | |
181 | |
182 private: | |
183 std::map<STLKey, STLValue>* result; | |
184 }; | |
185 | |
186 } // namespace internal | |
187 | |
188 template <typename MojoKey, typename MojoValue, | |
189 typename STLKey, typename STLValue> | |
190 struct TypeConverter<std::map<STLKey, STLValue>, Map<MojoKey, MojoValue> > { | |
191 static std::map<STLKey, STLValue> Convert( | |
192 const Map<MojoKey, MojoValue>& input) { | |
193 std::map<STLKey, STLValue> result; | |
194 if (!input.is_null()) { | |
195 for (auto it = input.begin(); it != input.end(); ++it) { | |
196 result.insert(std::make_pair( | |
197 TypeConverter<STLKey, MojoKey>::Convert(it.GetKey()), | |
198 TypeConverter<STLValue, MojoValue>::Convert(it.GetValue()))); | |
199 } | |
200 } | |
201 return result; | |
202 } | |
203 }; | |
204 | |
205 } // namespace mojo | |
206 | |
207 #endif // MOJO_PUBLIC_CPP_BINDINGS_MAP_H_ | |
OLD | NEW |