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

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

Issue 2259283003: Mojo C++ bindings: share DataView class between chromium and blink variants. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@92_change_traits_param
Patch Set: . Created 4 years, 3 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/BUILD.gn ('k') | mojo/public/cpp/bindings/interface_data_view.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ARRAY_DATA_VIEW_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ARRAY_DATA_VIEW_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_DATA_VIEW_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_DATA_VIEW_H_
7 7
8 #include <type_traits> 8 #include <type_traits>
9 9
10 #include "mojo/public/cpp/bindings/array.h" 10 #include "mojo/public/cpp/bindings/lib/array_internal.h"
11 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" 11 #include "mojo/public/cpp/bindings/lib/bindings_internal.h"
12 #include "mojo/public/cpp/bindings/lib/serialization_context.h" 12 #include "mojo/public/cpp/bindings/lib/serialization_context.h"
13 #include "mojo/public/cpp/bindings/lib/serialization_forward.h" 13 #include "mojo/public/cpp/bindings/lib/serialization_forward.h"
14 14
15 namespace mojo { 15 namespace mojo {
16 namespace internal { 16 namespace internal {
17 17
18 template <typename T, typename EnableType = void> 18 template <typename T, typename EnableType = void>
19 class ArrayDataViewImpl; 19 class ArrayDataViewImpl;
20 20
21 template <typename T> 21 template <typename T>
22 class ArrayDataViewImpl< 22 class ArrayDataViewImpl<
23 T, 23 T,
24 typename std::enable_if<BelongsTo<typename DataViewTraits<T>::MojomType, 24 typename std::enable_if<
25 MojomTypeCategory::POD>::value>::type> { 25 BelongsTo<T, MojomTypeCategory::POD>::value>::type> {
26 public: 26 public:
27 static_assert(std::is_same<T, typename DataViewTraits<T>::MojomType>::value, 27 using Data_ = typename MojomTypeTraits<ArrayDataView<T>>::Data;
28 "DataView type mismatch");
29
30 using Data_ = typename MojomTypeTraits<Array<T>>::Data;
31 28
32 ArrayDataViewImpl(Data_* data, SerializationContext* context) 29 ArrayDataViewImpl(Data_* data, SerializationContext* context)
33 : data_(data), context_(context) {} 30 : data_(data), context_(context) {}
34 31
35 T operator[](size_t index) const { return data_->at(index); } 32 T operator[](size_t index) const { return data_->at(index); }
36 33
37 const T* data() const { return data_->storage(); } 34 const T* data() const { return data_->storage(); }
38 35
39 protected: 36 protected:
40 Data_* data_; 37 Data_* data_;
41 SerializationContext* context_; 38 SerializationContext* context_;
42 }; 39 };
43 40
44 template <typename T> 41 template <typename T>
45 class ArrayDataViewImpl<T, 42 class ArrayDataViewImpl<
46 typename std::enable_if<BelongsTo< 43 T,
47 typename DataViewTraits<T>::MojomType, 44 typename std::enable_if<
48 MojomTypeCategory::BOOLEAN>::value>::type> { 45 BelongsTo<T, MojomTypeCategory::BOOLEAN>::value>::type> {
49 public: 46 public:
50 static_assert(std::is_same<T, typename DataViewTraits<T>::MojomType>::value, 47 using Data_ = typename MojomTypeTraits<ArrayDataView<T>>::Data;
51 "DataView type mismatch");
52
53 using Data_ = typename MojomTypeTraits<Array<T>>::Data;
54 48
55 ArrayDataViewImpl(Data_* data, SerializationContext* context) 49 ArrayDataViewImpl(Data_* data, SerializationContext* context)
56 : data_(data), context_(context) {} 50 : data_(data), context_(context) {}
57 51
58 bool operator[](size_t index) const { return data_->at(index); } 52 bool operator[](size_t index) const { return data_->at(index); }
59 53
60 protected: 54 protected:
61 Data_* data_; 55 Data_* data_;
62 SerializationContext* context_; 56 SerializationContext* context_;
63 }; 57 };
64 58
65 template <typename T> 59 template <typename T>
66 class ArrayDataViewImpl< 60 class ArrayDataViewImpl<
67 T, 61 T,
68 typename std::enable_if<BelongsTo<typename DataViewTraits<T>::MojomType, 62 typename std::enable_if<
69 MojomTypeCategory::ENUM>::value>::type> { 63 BelongsTo<T, MojomTypeCategory::ENUM>::value>::type> {
70 public: 64 public:
71 static_assert(std::is_same<T, typename DataViewTraits<T>::MojomType>::value,
72 "DataView type mismatch");
73 static_assert(sizeof(T) == sizeof(int32_t), "Unexpected enum size"); 65 static_assert(sizeof(T) == sizeof(int32_t), "Unexpected enum size");
74 66
75 using Data_ = typename MojomTypeTraits<Array<T>>::Data; 67 using Data_ = typename MojomTypeTraits<ArrayDataView<T>>::Data;
76 68
77 ArrayDataViewImpl(Data_* data, SerializationContext* context) 69 ArrayDataViewImpl(Data_* data, SerializationContext* context)
78 : data_(data), context_(context) {} 70 : data_(data), context_(context) {}
79 71
80 T operator[](size_t index) const { return static_cast<T>(data_->at(index)); } 72 T operator[](size_t index) const { return static_cast<T>(data_->at(index)); }
81 73
82 const T* data() const { return reinterpret_cast<const T*>(data_->storage()); } 74 const T* data() const { return reinterpret_cast<const T*>(data_->storage()); }
83 75
84 template <typename U> 76 template <typename U>
85 bool Read(size_t index, U* output) { 77 bool Read(size_t index, U* output) {
86 return Deserialize<T>(data_->at(index), output); 78 return Deserialize<T>(data_->at(index), output);
87 } 79 }
88 80
89 protected: 81 protected:
90 Data_* data_; 82 Data_* data_;
91 SerializationContext* context_; 83 SerializationContext* context_;
92 }; 84 };
93 85
94 template <typename T> 86 template <typename T>
95 class ArrayDataViewImpl< 87 class ArrayDataViewImpl<
96 T, 88 T,
97 typename std::enable_if< 89 typename std::enable_if<
98 BelongsTo<typename DataViewTraits<T>::MojomType, 90 BelongsTo<T,
99 MojomTypeCategory::ASSOCIATED_INTERFACE | 91 MojomTypeCategory::ASSOCIATED_INTERFACE |
100 MojomTypeCategory::ASSOCIATED_INTERFACE_REQUEST | 92 MojomTypeCategory::ASSOCIATED_INTERFACE_REQUEST |
101 MojomTypeCategory::HANDLE |
102 MojomTypeCategory::INTERFACE | 93 MojomTypeCategory::INTERFACE |
103 MojomTypeCategory::INTERFACE_REQUEST>::value>::type> { 94 MojomTypeCategory::INTERFACE_REQUEST>::value>::type> {
104 public: 95 public:
105 static_assert(std::is_same<T, typename DataViewTraits<T>::MojomType>::value, 96 using Data_ = typename MojomTypeTraits<ArrayDataView<T>>::Data;
106 "DataView type mismatch");
107
108 using Data_ = typename MojomTypeTraits<Array<T>>::Data;
109 97
110 ArrayDataViewImpl(Data_* data, SerializationContext* context) 98 ArrayDataViewImpl(Data_* data, SerializationContext* context)
111 : data_(data), context_(context) {} 99 : data_(data), context_(context) {}
100
101 template <typename U>
102 U Take(size_t index) {
103 U result;
104 bool ret = Deserialize<T>(&data_->at(index), &result, context_);
105 DCHECK(ret);
106 return result;
107 }
108
109 protected:
110 Data_* data_;
111 SerializationContext* context_;
112 };
113
114 template <typename T>
115 class ArrayDataViewImpl<
116 T,
117 typename std::enable_if<
118 BelongsTo<T, MojomTypeCategory::HANDLE>::value>::type> {
119 public:
120 using Data_ = typename MojomTypeTraits<ArrayDataView<T>>::Data;
121
122 ArrayDataViewImpl(Data_* data, SerializationContext* context)
123 : data_(data), context_(context) {}
112 124
113 T Take(size_t index) { 125 T Take(size_t index) {
114 T result; 126 T result;
115 bool ret = Deserialize<T>(&data_->at(index), &result, context_); 127 bool ret = Deserialize<T>(&data_->at(index), &result, context_);
116 DCHECK(ret); 128 DCHECK(ret);
117 return result; 129 return result;
118 } 130 }
119 131
120 protected: 132 protected:
121 Data_* data_; 133 Data_* data_;
122 SerializationContext* context_; 134 SerializationContext* context_;
123 }; 135 };
124 136
125 template <typename T> 137 template <typename T>
126 class ArrayDataViewImpl<T, 138 class ArrayDataViewImpl<T,
127 typename std::enable_if<BelongsTo< 139 typename std::enable_if<BelongsTo<
128 typename DataViewTraits<T>::MojomType, 140 T,
129 MojomTypeCategory::ARRAY | MojomTypeCategory::MAP | 141 MojomTypeCategory::ARRAY | MojomTypeCategory::MAP |
130 MojomTypeCategory::STRING | 142 MojomTypeCategory::STRING |
131 MojomTypeCategory::STRUCT>::value>::type> { 143 MojomTypeCategory::STRUCT>::value>::type> {
132 public: 144 public:
133 using Data_ = typename MojomTypeTraits< 145 using Data_ = typename MojomTypeTraits<ArrayDataView<T>>::Data;
134 Array<typename DataViewTraits<T>::MojomType>>::Data;
135 146
136 ArrayDataViewImpl(Data_* data, SerializationContext* context) 147 ArrayDataViewImpl(Data_* data, SerializationContext* context)
137 : data_(data), context_(context) {} 148 : data_(data), context_(context) {}
138 149
139 void GetDataView(size_t index, T* output) { 150 void GetDataView(size_t index, T* output) {
140 *output = T(data_->at(index).Get(), context_); 151 *output = T(data_->at(index).Get(), context_);
141 } 152 }
142 153
143 template <typename U> 154 template <typename U>
144 bool Read(size_t index, U* output) { 155 bool Read(size_t index, U* output) {
145 return Deserialize<typename DataViewTraits<T>::MojomType>( 156 return Deserialize<T>(data_->at(index).Get(), output, context_);
146 data_->at(index).Get(), output, context_);
147 } 157 }
148 158
149 protected: 159 protected:
150 Data_* data_; 160 Data_* data_;
151 SerializationContext* context_; 161 SerializationContext* context_;
152 }; 162 };
153 163
154 template <typename T> 164 template <typename T>
155 class ArrayDataViewImpl< 165 class ArrayDataViewImpl<
156 T, 166 T,
157 typename std::enable_if<BelongsTo<typename DataViewTraits<T>::MojomType, 167 typename std::enable_if<
158 MojomTypeCategory::UNION>::value>::type> { 168 BelongsTo<T, MojomTypeCategory::UNION>::value>::type> {
159 public: 169 public:
160 using Data_ = typename MojomTypeTraits< 170 using Data_ = typename MojomTypeTraits<ArrayDataView<T>>::Data;
161 Array<typename DataViewTraits<T>::MojomType>>::Data;
162 171
163 ArrayDataViewImpl(Data_* data, SerializationContext* context) 172 ArrayDataViewImpl(Data_* data, SerializationContext* context)
164 : data_(data), context_(context) {} 173 : data_(data), context_(context) {}
165 174
166 void GetDataView(size_t index, T* output) { 175 void GetDataView(size_t index, T* output) {
167 *output = T(&data_->at(index), context_); 176 *output = T(&data_->at(index), context_);
168 } 177 }
169 178
170 template <typename U> 179 template <typename U>
171 bool Read(size_t index, U* output) { 180 bool Read(size_t index, U* output) {
172 return Deserialize<typename DataViewTraits<T>::MojomType>(&data_->at(index), 181 return Deserialize<T>(&data_->at(index), output, context_);
173 output, context_);
174 } 182 }
175 183
176 protected: 184 protected:
177 Data_* data_; 185 Data_* data_;
178 SerializationContext* context_; 186 SerializationContext* context_;
179 }; 187 };
180 188
181 } // namespace internal 189 } // namespace internal
182 190
183 template <typename K, typename V> 191 template <typename K, typename V>
184 class MapDataView; 192 class MapDataView;
185 193
186 template <typename T> 194 template <typename T>
187 class ArrayDataView : public internal::ArrayDataViewImpl<T> { 195 class ArrayDataView : public internal::ArrayDataViewImpl<T> {
188 public: 196 public:
197 using Element = T;
189 using Data_ = typename internal::ArrayDataViewImpl<T>::Data_; 198 using Data_ = typename internal::ArrayDataViewImpl<T>::Data_;
190 199
191 ArrayDataView() : internal::ArrayDataViewImpl<T>(nullptr, nullptr) {} 200 ArrayDataView() : internal::ArrayDataViewImpl<T>(nullptr, nullptr) {}
192 201
193 ArrayDataView(Data_* data, internal::SerializationContext* context) 202 ArrayDataView(Data_* data, internal::SerializationContext* context)
194 : internal::ArrayDataViewImpl<T>(data, context) {} 203 : internal::ArrayDataViewImpl<T>(data, context) {}
195 204
196 bool is_null() const { return !this->data_; } 205 bool is_null() const { return !this->data_; }
197 206
198 size_t size() const { return this->data_->size(); } 207 size_t size() const { return this->data_->size(); }
199 208
200 // Methods to access elements are different for different element types. They 209 // Methods to access elements are different for different element types. They
201 // are inherited from internal::ArrayDataViewImpl: 210 // are inherited from internal::ArrayDataViewImpl:
202 211
203 // POD types except boolean and enums: 212 // POD types except boolean and enums:
204 // T operator[](size_t index) const; 213 // T operator[](size_t index) const;
205 // const T* data() const; 214 // const T* data() const;
206 215
207 // Boolean: 216 // Boolean:
208 // bool operator[](size_t index) const; 217 // bool operator[](size_t index) const;
209 218
210 // Enums: 219 // Enums:
211 // T operator[](size_t index) const; 220 // T operator[](size_t index) const;
212 // const T* data() const; 221 // const T* data() const;
213 // template <typename U> 222 // template <typename U>
214 // bool Read(size_t index, U* output); 223 // bool Read(size_t index, U* output);
215 224
216 // Handles and interfaces: 225 // Handles:
217 // T Take(size_t index); 226 // T Take(size_t index);
218 227
228 // Interfaces:
229 // template <typename U>
230 // U Take(size_t index);
231
219 // Object types: 232 // Object types:
220 // void GetDataView(size_t index, T* output); 233 // void GetDataView(size_t index, T* output);
221 // template <typename U> 234 // template <typename U>
222 // bool Read(size_t index, U* output); 235 // bool Read(size_t index, U* output);
223 236
224 private: 237 private:
225 template <typename K, typename V> 238 template <typename K, typename V>
226 friend class MapDataView; 239 friend class MapDataView;
227 }; 240 };
228 241
229 } // namespace mojo 242 } // namespace mojo
230 243
231 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_DATA_VIEW_H_ 244 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_DATA_VIEW_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/interface_data_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698