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

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

Powered by Google App Engine
This is Rietveld 408576698