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

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

Powered by Google App Engine
This is Rietveld 408576698