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

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: 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,
19 map_key_cant_be_move_only);
20
21 typedef Key KeyStorageType;
22 typedef Key& KeyRefType;
23 typedef const Key& KeyConstRef;
24 typedef KeyConstRef KeyForwardType;
25
26 typedef Value ValueStorageType;
27 typedef Value& ValueRefType;
28 typedef const Value& ValueConstRef;
29 typedef ValueConstRef 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,
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 };
62
63 template <typename Key, typename Value> struct MapTraits<Key, Value, true> {
64 // Map keys can't be move only types.
65 MOJO_COMPILE_ASSERT(!internal::IsMoveOnlyType<Key>::value,
66 map_key_cant_be_move_only);
67
68 typedef Key KeyStorageType;
69 typedef Key& KeyRefType;
70 typedef const Key& KeyConstRefType;
71 typedef KeyConstRefType KeyForwardType;
72
73 struct ValueStorageType {
74 // Make 8-byte aligned.
75 char buf[sizeof(Value) + (8 - (sizeof(Value) % 8)) % 8];
76 };
77 typedef Value& ValueRefType;
78 typedef const Value& ValueConstRefType;
79 typedef Value ValueForwardType;
80
81 static inline void InitializeFrom(
82 std::map<KeyStorageType, ValueStorageType>* m,
83 mojo::Array<Key> keys,
84 mojo::Array<Value> values) {
85 for (size_t i = 0; i < keys.size(); ++i)
86 new ((*m)[keys[i]].buf) Value(values[i].Pass());
87 }
88 static inline void Decompose(
89 std::map<KeyStorageType, ValueStorageType>* m,
90 mojo::Array<Key>* keys,
91 mojo::Array<Value>* values) {
92 keys->resize(m->size());
93 values->resize(m->size());
94 int i = 0;
95 for (typename std::map<KeyStorageType, ValueStorageType>::iterator it =
96 m->begin(); it != m->end(); ++it, ++i) {
97 (*keys)[i] = it->first;
98 (*values)[i] = reinterpret_cast<Value*>(it->second.buf)->Pass();
99 }
100 }
101 static inline void Finalize(std::map<KeyStorageType, ValueStorageType>* m) {
102 for (auto& pair : *m)
103 reinterpret_cast<Value*>(pair.second.buf)->~Value();
104 }
105 static inline ValueRefType at(std::map<KeyStorageType, ValueStorageType>* m,
106 KeyForwardType key) {
107 // We don't have C++11 library support yet, so we have to emulate the crash
108 // on a non-existant key.
109 auto it = m->find(key);
110 MOJO_CHECK(it != m->end());
111 return *reinterpret_cast<Value*>(it->second.buf);
112 }
113 static inline void Insert(std::map<KeyStorageType, ValueStorageType>* m,
114 KeyForwardType key, ValueRefType value) {
115 // STL insert() doesn't insert |value| if |key| is already part of |m|. We
116 // have to use operator[] to initialize into the storage buffer, but we
117 // have to do a manual check so that we don't overwrite an existing object.
118 auto it = m->find(key);
119 if (it == m->end())
120 new ((*m)[key].buf) Value(value.Pass());
121 }
122 };
123
124 } // namespace internal
125 } // namespace mojo
126
127 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_INTERNAL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698