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

Side by Side Diff: mojo/public/cpp/bindings/lib/map_internal.h

Issue 611633002: mojom: Add associative arrays to the mojom language. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove usage of the new feature in mojom files. Will be readded in a later patch. 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_LIB_MAP_INTERNAL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_INTERNAL_H_
7
8 #include <map>
9
10 #include "mojo/public/cpp/bindings/array.h"
11 #include "mojo/public/cpp/bindings/lib/bindings_internal.h"
12
13 namespace mojo {
14 namespace internal {
15
16 template <typename Key, typename Value, bool kValueIsMoveOnlyType>
17 struct MapTraits {};
18
19 template <typename Key, typename Value>
20 struct MapTraits<Key, Value, false> {
21 // Map keys can't be move only types.
22 static_assert(!internal::IsMoveOnlyType<Key>::value,
23 "Map keys can not be move only types.");
24
25 typedef Key KeyStorageType;
26 typedef Key& KeyRefType;
27 typedef const Key& KeyConstRefType;
28 typedef KeyConstRefType KeyForwardType;
29
30 typedef Value ValueStorageType;
31 typedef Value& ValueRefType;
32 typedef const Value& ValueConstRefType;
33 typedef ValueConstRefType ValueForwardType;
34
35 static inline void InitializeFrom(
36 std::map<KeyStorageType, ValueStorageType>* m,
37 mojo::Array<Key> keys,
38 mojo::Array<Value> values) {
39 for (size_t i = 0; i < keys.size(); ++i)
40 Insert(m, keys[i], values[i]);
41 }
42 static inline void Decompose(std::map<KeyStorageType, ValueStorageType>* m,
43 mojo::Array<Key>* keys,
44 mojo::Array<Value>* values) {
45 keys->resize(m->size());
46 values->resize(m->size());
47 int i = 0;
48 for (typename std::map<KeyStorageType, ValueStorageType>::iterator
49 it = m->begin();
50 it != m->end();
51 ++it, ++i) {
52 (*keys)[i] = it->first;
53 (*values)[i] = it->second;
54 }
55 }
56 static inline void Finalize(std::map<KeyStorageType, ValueStorageType>* m) {}
57 static inline ValueRefType at(std::map<KeyStorageType, ValueStorageType>* m,
58 KeyForwardType key) {
59 // We don't have C++11 library support yet, so we have to emulate the crash
60 // on a non-existant key.
61 auto it = m->find(key);
62 MOJO_CHECK(it != m->end());
63 return it->second;
64 }
65 static inline ValueConstRefType at(
66 const std::map<KeyStorageType, ValueStorageType>* m,
67 KeyForwardType key) {
68 // We don't have C++11 library support yet, so we have to emulate the crash
69 // on a non-existant key.
70 auto it = m->find(key);
71 MOJO_CHECK(it != m->end());
72 return it->second;
73 }
74 static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m,
75 KeyForwardType key,
76 ValueForwardType value) {
77 m->insert(std::make_pair(key, value));
78 }
79 static inline KeyConstRefType GetKey(
80 const typename std::map<KeyStorageType, ValueStorageType>::const_iterator&
81 it) {
82 return it->first;
83 }
84 static inline ValueConstRefType GetValue(
85 const typename std::map<KeyStorageType, ValueStorageType>::const_iterator&
86 it) {
87 return it->second;
88 }
89 static inline ValueRefType GetValue(
90 const typename std::map<KeyStorageType, ValueStorageType>::iterator& it) {
91 return it->second;
92 }
93 static inline void Clone(
94 const std::map<KeyStorageType, ValueStorageType>& src,
95 std::map<KeyStorageType, ValueStorageType>* dst) {
96 dst->clear();
97 for (auto it = src.begin(); it != src.end(); ++it)
98 dst->insert(*it);
99 }
100 };
101
102 template <typename Key, typename Value>
103 struct MapTraits<Key, Value, true> {
104 // Map keys can't be move only types.
105 static_assert(!internal::IsMoveOnlyType<Key>::value,
106 "Map keys can not be move only types.");
107
108 typedef Key KeyStorageType;
109 typedef Key& KeyRefType;
110 typedef const Key& KeyConstRefType;
111 typedef KeyConstRefType KeyForwardType;
112
113 struct ValueStorageType {
114 // Make 8-byte aligned.
115 char buf[sizeof(Value) + (8 - (sizeof(Value) % 8)) % 8];
116 };
117 typedef Value& ValueRefType;
118 typedef const Value& ValueConstRefType;
119 typedef Value ValueForwardType;
120
121 static inline void InitializeFrom(
122 std::map<KeyStorageType, ValueStorageType>* m,
123 mojo::Array<Key> keys,
124 mojo::Array<Value> values) {
125 for (size_t i = 0; i < keys.size(); ++i)
126 Insert(m, keys[i], values[i]);
127 }
128 static inline void Decompose(std::map<KeyStorageType, ValueStorageType>* m,
129 mojo::Array<Key>* keys,
130 mojo::Array<Value>* values) {
131 keys->resize(m->size());
132 values->resize(m->size());
133 int i = 0;
134 for (typename std::map<KeyStorageType, ValueStorageType>::iterator
135 it = m->begin();
136 it != m->end();
137 ++it, ++i) {
138 (*keys)[i] = it->first;
139 (*values)[i] = GetValue(it).Pass();
140 }
141 }
142 static inline void Finalize(std::map<KeyStorageType, ValueStorageType>* m) {
143 for (auto& pair : *m)
144 reinterpret_cast<Value*>(pair.second.buf)->~Value();
145 }
146 static inline ValueRefType at(std::map<KeyStorageType, ValueStorageType>* m,
147 KeyForwardType key) {
148 // We don't have C++11 library support yet, so we have to emulate the crash
149 // on a non-existant key.
150 auto it = m->find(key);
151 MOJO_CHECK(it != m->end());
152 return GetValue(it);
153 }
154 static inline ValueConstRefType at(
155 const std::map<KeyStorageType, ValueStorageType>* m,
156 KeyForwardType key) {
157 // We don't have C++11 library support yet, so we have to emulate the crash
158 // on a non-existant key.
159 auto it = m->find(key);
160 MOJO_CHECK(it != m->end());
161 return GetValue(it);
162 }
163 static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m,
164 KeyForwardType key,
165 ValueRefType value) {
166 // STL insert() doesn't insert |value| if |key| is already part of |m|. We
167 // have to use operator[] to initialize into the storage buffer, but we
168 // have to do a manual check so that we don't overwrite an existing object.
169 auto it = m->find(key);
170 if (it == m->end())
171 new ((*m)[key].buf) Value(value.Pass());
172 }
173 static inline KeyConstRefType GetKey(
174 const typename std::map<KeyStorageType, ValueStorageType>::const_iterator&
175 it) {
176 return it->first;
177 }
178 static inline ValueConstRefType GetValue(
179 const typename std::map<KeyStorageType, ValueStorageType>::const_iterator&
180 it) {
181 return *reinterpret_cast<const Value*>(it->second.buf);
182 }
183 static inline ValueRefType GetValue(
184 const typename std::map<KeyStorageType, ValueStorageType>::iterator& it) {
185 return *reinterpret_cast<Value*>(it->second.buf);
186 }
187 static inline void Clone(
188 const std::map<KeyStorageType, ValueStorageType>& src,
189 std::map<KeyStorageType, ValueStorageType>* dst) {
190 Finalize(dst);
191 dst->clear();
192 for (auto it = src.begin(); it != src.end(); ++it)
193 new ((*dst)[it->first].buf) Value(GetValue(it).Clone());
194 }
195 };
196
197 } // namespace internal
198 } // namespace mojo
199
200 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_INTERNAL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698