Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: mojo/public/cpp/bindings/map.h

Issue 611633002: mojom: Add associative arrays to the mojom language. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moved test classes to their own shared file. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/cpp/bindings/lib/validation_errors.cc ('k') | mojo/public/cpp/bindings/string.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // Used to mark an empty map as non-null for serialization purposes.
77 void mark_non_null() { is_null_ = false; }
78
79 // Inserts a key-value pair into the map. Like std::map, this does not insert
80 // |value| if |key| is already a member of the map.
81 void insert(KeyForwardType key, ValueForwardType value) {
82 is_null_ = false;
83 Traits::Insert(&map_, key, value);
84 }
85
86 ValueRefType at(KeyForwardType key) { return Traits::at(&map_, key); }
87 ValueConstRefType at(KeyForwardType key) const {
88 return Traits::at(&map_, key);
89 }
90
91 void Swap(Map<Key, Value>* other) {
92 std::swap(is_null_, other->is_null_);
93 map_.swap(other->map_);
94 }
95 void Swap(std::map<Key, Value>* other) {
96 is_null_ = false;
97 map_.swap(*other);
98 }
99
100 // This moves all values in the map to a set of parallel arrays. This action
101 // is destructive because we can have move-only objects as values; therefore
102 // we can't have copy semantics here.
103 void DecomposeMapTo(mojo::Array<Key>* keys, mojo::Array<Value>* values) {
104 Traits::Decompose(&map_, keys, values);
105 Traits::Finalize(&map_);
106 map_.clear();
107 is_null_ = true;
108 }
109
110 // Please note that calling this method will fail compilation if the value
111 // type cannot be cloned (which usually means that it is a Mojo handle type or
112 // a type contains Mojo handles).
113 Map Clone() const {
114 Map result;
115 result.is_null_ = is_null_;
116 Traits::Clone(map_, &result.map_);
117 return result.Pass();
118 }
119
120 class ConstMapIterator {
121 public:
122 ConstMapIterator(
123 const typename std::map<KeyStorageType,
124 ValueStorageType>::const_iterator& it)
125 : it_(it) {}
126
127 KeyConstRefType GetKey() { return Traits::GetKey(it_); }
128 ValueConstRefType GetValue() { return Traits::GetValue(it_); }
129
130 ConstMapIterator& operator++() {
131 it_++;
132 return *this;
133 }
134 bool operator!=(const ConstMapIterator& rhs) const {
135 return it_ != rhs.it_;
136 }
137 bool operator==(const ConstMapIterator& rhs) const {
138 return it_ == rhs.it_;
139 }
140
141 private:
142 typename std::map<KeyStorageType, ValueStorageType>::const_iterator it_;
143 };
144
145 // Provide read-only iteration over map members.
146 ConstMapIterator begin() const { return ConstMapIterator(map_.begin()); }
147 ConstMapIterator end() const { return ConstMapIterator(map_.end()); }
148
149 ConstMapIterator find(KeyForwardType key) const {
150 return ConstMapIterator(map_.find(key));
151 }
152
153 private:
154 typedef std::map<KeyStorageType, ValueStorageType> Map::*Testable;
155
156 public:
157 operator Testable() const { return is_null_ ? 0 : &Map::map_; }
158
159 private:
160 void Take(Map* other) {
161 reset();
162 Swap(other);
163 }
164
165 std::map<KeyStorageType, ValueStorageType> map_;
166 bool is_null_;
167 };
168
169 template <typename MojoKey,
170 typename MojoValue,
171 typename STLKey,
172 typename STLValue>
173 struct TypeConverter<Map<MojoKey, MojoValue>, std::map<STLKey, STLValue>> {
174 static Map<MojoKey, MojoValue> Convert(
175 const std::map<STLKey, STLValue>& input) {
176 Map<MojoKey, MojoValue> result;
177 result.mark_non_null();
178 for (auto& pair : input) {
179 result.insert(TypeConverter<MojoKey, STLKey>::Convert(pair.first),
180 TypeConverter<MojoValue, STLValue>::Convert(pair.second));
181 }
182 return result.Pass();
183 }
184 };
185
186 template <typename MojoKey,
187 typename MojoValue,
188 typename STLKey,
189 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_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/validation_errors.cc ('k') | mojo/public/cpp/bindings/string.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698