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

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

Issue 814543006: Move //mojo/{public, edk} underneath //third_party (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 11 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/message.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(Map)
17
18 public:
19 // Map keys can not be move only classes.
20 static_assert(!internal::IsMoveOnlyType<Key>::value,
21 "Map keys can not be move only types.");
22
23 typedef internal::MapTraits<Key,
24 Value,
25 internal::IsMoveOnlyType<Value>::value> Traits;
26 typedef typename Traits::KeyStorageType KeyStorageType;
27 typedef typename Traits::KeyRefType KeyRefType;
28 typedef typename Traits::KeyConstRefType KeyConstRefType;
29 typedef typename Traits::KeyForwardType KeyForwardType;
30
31 typedef typename Traits::ValueStorageType ValueStorageType;
32 typedef typename Traits::ValueRefType ValueRefType;
33 typedef typename Traits::ValueConstRefType ValueConstRefType;
34 typedef typename Traits::ValueForwardType ValueForwardType;
35
36 typedef internal::Map_Data<typename internal::WrapperTraits<Key>::DataType,
37 typename internal::WrapperTraits<Value>::DataType>
38 Data_;
39
40 Map() : is_null_(true) {}
41
42 Map(mojo::Array<Key> keys, mojo::Array<Value> values) : is_null_(false) {
43 MOJO_DCHECK(keys.size() == values.size());
44 Traits::InitializeFrom(&map_, keys.Pass(), values.Pass());
45 }
46
47 ~Map() { Traits::Finalize(&map_); }
48
49 Map(Map&& other) : is_null_(true) { Take(&other); }
50 Map& operator=(Map&& other) {
51 Take(&other);
52 return *this;
53 }
54
55 template <typename U>
56 static Map From(const U& other) {
57 return TypeConverter<Map, U>::Convert(other);
58 }
59
60 template <typename U>
61 U To() const {
62 return TypeConverter<U, Map>::Convert(*this);
63 }
64
65 void reset() {
66 if (!map_.empty()) {
67 Traits::Finalize(&map_);
68 map_.clear();
69 }
70 is_null_ = true;
71 }
72
73 bool is_null() const { return is_null_; }
74
75 size_t size() const { return map_.size(); }
76
77 // Used to mark an empty map as non-null for serialization purposes.
78 void mark_non_null() { is_null_ = false; }
79
80 // Inserts a key-value pair into the map. Like std::map, this does not insert
81 // |value| if |key| is already a member of the map.
82 void insert(KeyForwardType key, ValueForwardType value) {
83 is_null_ = false;
84 Traits::Insert(&map_, key, value);
85 }
86
87 ValueRefType at(KeyForwardType key) { return Traits::at(&map_, key); }
88 ValueConstRefType at(KeyForwardType key) const {
89 return Traits::at(&map_, key);
90 }
91
92 ValueRefType operator[](KeyForwardType key) {
93 is_null_ = false;
94 return Traits::GetOrInsert(&map_, key);
95 }
96
97 void Swap(Map<Key, Value>* other) {
98 std::swap(is_null_, other->is_null_);
99 map_.swap(other->map_);
100 }
101 void Swap(std::map<Key, Value>* other) {
102 is_null_ = false;
103 map_.swap(*other);
104 }
105
106 // This moves all values in the map to a set of parallel arrays. This action
107 // is destructive because we can have move-only objects as values; therefore
108 // we can't have copy semantics here.
109 void DecomposeMapTo(mojo::Array<Key>* keys, mojo::Array<Value>* values) {
110 Traits::Decompose(&map_, keys, values);
111 Traits::Finalize(&map_);
112 map_.clear();
113 is_null_ = true;
114 }
115
116 // Please note that calling this method will fail compilation if the value
117 // type cannot be cloned (which usually means that it is a Mojo handle type or
118 // a type contains Mojo handles).
119 Map Clone() const {
120 Map result;
121 result.is_null_ = is_null_;
122 Traits::Clone(map_, &result.map_);
123 return result.Pass();
124 }
125
126 bool Equals(const Map& other) const {
127 if (is_null() != other.is_null())
128 return false;
129 if (size() != other.size())
130 return false;
131 auto i = begin();
132 auto j = other.begin();
133 while (i != end()) {
134 if (i.GetKey() != j.GetKey())
135 return false;
136 if (!internal::ValueTraits<Value>::Equals(i.GetValue(), j.GetValue()))
137 return false;
138 ++i;
139 ++j;
140 }
141 return true;
142 }
143
144 class ConstMapIterator {
145 public:
146 ConstMapIterator(
147 const typename std::map<KeyStorageType,
148 ValueStorageType>::const_iterator& it)
149 : it_(it) {}
150
151 KeyConstRefType GetKey() { return Traits::GetKey(it_); }
152 ValueConstRefType GetValue() { return Traits::GetValue(it_); }
153
154 ConstMapIterator& operator++() {
155 it_++;
156 return *this;
157 }
158 bool operator!=(const ConstMapIterator& rhs) const {
159 return it_ != rhs.it_;
160 }
161 bool operator==(const ConstMapIterator& rhs) const {
162 return it_ == rhs.it_;
163 }
164
165 private:
166 typename std::map<KeyStorageType, ValueStorageType>::const_iterator it_;
167 };
168
169 // Provide read-only iteration over map members.
170 ConstMapIterator begin() const { return ConstMapIterator(map_.begin()); }
171 ConstMapIterator end() const { return ConstMapIterator(map_.end()); }
172
173 ConstMapIterator find(KeyForwardType key) const {
174 return ConstMapIterator(map_.find(key));
175 }
176
177 private:
178 typedef std::map<KeyStorageType, ValueStorageType> Map::*Testable;
179
180 public:
181 operator Testable() const { return is_null_ ? 0 : &Map::map_; }
182
183 private:
184 void Take(Map* other) {
185 reset();
186 Swap(other);
187 }
188
189 std::map<KeyStorageType, ValueStorageType> map_;
190 bool is_null_;
191 };
192
193 template <typename MojoKey,
194 typename MojoValue,
195 typename STLKey,
196 typename STLValue>
197 struct TypeConverter<Map<MojoKey, MojoValue>, std::map<STLKey, STLValue>> {
198 static Map<MojoKey, MojoValue> Convert(
199 const std::map<STLKey, STLValue>& input) {
200 Map<MojoKey, MojoValue> result;
201 result.mark_non_null();
202 for (auto& pair : input) {
203 result.insert(TypeConverter<MojoKey, STLKey>::Convert(pair.first),
204 TypeConverter<MojoValue, STLValue>::Convert(pair.second));
205 }
206 return result.Pass();
207 }
208 };
209
210 template <typename MojoKey,
211 typename MojoValue,
212 typename STLKey,
213 typename STLValue>
214 struct TypeConverter<std::map<STLKey, STLValue>, Map<MojoKey, MojoValue>> {
215 static std::map<STLKey, STLValue> Convert(
216 const Map<MojoKey, MojoValue>& input) {
217 std::map<STLKey, STLValue> result;
218 if (!input.is_null()) {
219 for (auto it = input.begin(); it != input.end(); ++it) {
220 result.insert(std::make_pair(
221 TypeConverter<STLKey, MojoKey>::Convert(it.GetKey()),
222 TypeConverter<STLValue, MojoValue>::Convert(it.GetValue())));
223 }
224 }
225 return result;
226 }
227 };
228
229 } // namespace mojo
230
231 #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/message.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698