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

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: Fix gn build. 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 "mojo/public/cpp/bindings/lib/array_internal.h"
9
10 namespace mojo {
11 namespace internal {
12
13 template <typename Key, typename Value, bool kValueIsMoveOnlyType>
14 struct MapTraits {};
15
16 template <typename Key, typename Value> struct MapTraits<Key, Value, false> {
17 // Map keys can't be move only types.
18 MOJO_COMPILE_ASSERT(!internal::IsMoveOnlyType<Key>::value,
yzshen1 2014/10/03 18:03:37 (optional) you can use the corresponding C++11 fea
19 map_key_cant_be_move_only);
20
21 typedef Key KeyStorageType;
22 typedef Key& KeyRefType;
23 typedef const Key& KeyConstRefType;
24 typedef KeyConstRefType KeyForwardType;
25
26 typedef Value ValueStorageType;
27 typedef Value& ValueRefType;
28 typedef const Value& ValueConstRefType;
29 typedef ValueConstRefType ValueForwardType;
30
31 static inline void InitializeFrom(
32 std::map<KeyStorageType, ValueStorageType>* m,
33 mojo::Array<Key> keys,
34 mojo::Array<Value> values) {
35 for (size_t i = 0; i < keys.size(); ++i)
36 Insert(m, keys[i], values[i]);
37 }
38 static inline void Decompose(
39 std::map<KeyStorageType, ValueStorageType>* m,
40 mojo::Array<Key>* keys,
41 mojo::Array<Value>* values) {
42 keys->resize(m->size());
43 values->resize(m->size());
44 int i = 0;
45 for (typename std::map<KeyStorageType, ValueStorageType>::iterator it =
46 m->begin(); it != m->end(); ++it, ++i) {
47 (*keys)[i] = it->first;
48 (*values)[i] = it->second;
49 }
50 }
51 static inline void Finalize(std::map<KeyStorageType, ValueStorageType>* m) {
52 }
53 static inline ValueRefType at(std::map<KeyStorageType, ValueStorageType>* m,
yzshen1 2014/10/03 18:03:37 Does it also need a const version? ValueConstRefTy
54 KeyForwardType key) {
55 return (*m)[key];
56 }
57 static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m,
58 KeyForwardType key, ValueForwardType value) {
59 m->insert(std::make_pair(key, value));
60 }
61 static inline KeyConstRefType GetKey(
62 const typename std::map<KeyStorageType,
63 ValueStorageType>::const_iterator& it) {
yzshen1 2014/10/03 18:03:37 (optional) I feel that it is easy to read if Value
64 return it->first;
65 }
66 static inline ValueConstRefType GetValue(
67 const typename std::map<KeyStorageType,
68 ValueStorageType>::const_iterator& it) {
69 return it->second;
70 }
71 };
72
73 template <typename Key, typename Value> struct MapTraits<Key, Value, true> {
74 // Map keys can't be move only types.
75 MOJO_COMPILE_ASSERT(!internal::IsMoveOnlyType<Key>::value,
76 map_key_cant_be_move_only);
77
78 typedef Key KeyStorageType;
79 typedef Key& KeyRefType;
80 typedef const Key& KeyConstRefType;
81 typedef KeyConstRefType KeyForwardType;
82
83 struct ValueStorageType {
84 // Make 8-byte aligned.
85 char buf[sizeof(Value) + (8 - (sizeof(Value) % 8)) % 8];
86 };
87 typedef Value& ValueRefType;
88 typedef const Value& ValueConstRefType;
89 typedef Value ValueForwardType;
90
91 static inline void InitializeFrom(
92 std::map<KeyStorageType, ValueStorageType>* m,
93 mojo::Array<Key> keys,
94 mojo::Array<Value> values) {
95 for (size_t i = 0; i < keys.size(); ++i)
96 new ((*m)[keys[i]].buf) Value(values[i].Pass());
97 }
98 static inline void Decompose(
99 std::map<KeyStorageType, ValueStorageType>* m,
100 mojo::Array<Key>* keys,
101 mojo::Array<Value>* values) {
102 keys->resize(m->size());
103 values->resize(m->size());
104 int i = 0;
105 for (typename std::map<KeyStorageType, ValueStorageType>::iterator it =
106 m->begin(); it != m->end(); ++it, ++i) {
107 (*keys)[i] = it->first;
108 (*values)[i] = reinterpret_cast<Value*>(it->second.buf)->Pass();
109 }
110 }
111 static inline void Finalize(std::map<KeyStorageType, ValueStorageType>* m) {
112 for (auto& pair : *m)
113 reinterpret_cast<Value*>(pair.second.buf)->~Value();
114 }
115 static inline ValueRefType at(std::map<KeyStorageType, ValueStorageType>* m,
116 KeyForwardType key) {
117 // We don't have C++11 library support yet, so we have to emulate the crash
118 // on a non-existant key.
119 auto it = m->find(key);
120 MOJO_CHECK(it != m->end());
121 return *reinterpret_cast<Value*>(it->second.buf);
122 }
123 static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m,
124 KeyForwardType key, ValueRefType value) {
125 // STL insert() doesn't insert |value| if |key| is already part of |m|. We
126 // have to use operator[] to initialize into the storage buffer, but we
127 // have to do a manual check so that we don't overwrite an existing object.
128 auto it = m->find(key);
129 if (it == m->end())
130 new ((*m)[key].buf) Value(value.Pass());
131 }
132 static inline KeyConstRefType GetKey(
133 const typename std::map<KeyStorageType,
134 ValueStorageType>::const_iterator& it) {
135 return it->first;
136 }
137 static inline ValueConstRefType GetValue(
138 const typename std::map<KeyStorageType,
139 ValueStorageType>::const_iterator& it) {
140 return *reinterpret_cast<const Value*>(it->second.buf);
141 }
142 };
143
144 } // namespace internal
145 } // namespace mojo
146
147 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_INTERNAL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698