OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ARRAY_DATA_VIEW_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_DATA_VIEW_H_ |
| 7 |
| 8 #include <type_traits> |
| 9 |
| 10 #include "mojo/public/cpp/bindings/array.h" |
| 11 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
| 12 #include "mojo/public/cpp/bindings/lib/serialization_context.h" |
| 13 #include "mojo/public/cpp/bindings/lib/serialization_forward.h" |
| 14 |
| 15 namespace mojo { |
| 16 namespace internal { |
| 17 |
| 18 template <typename T, typename EnableType = void> |
| 19 class ArrayDataViewImpl; |
| 20 |
| 21 template <typename T> |
| 22 class ArrayDataViewImpl< |
| 23 T, |
| 24 typename std::enable_if<BelongsTo<typename DataViewTraits<T>::MojomType, |
| 25 MojomTypeCategory::POD>::value>::type> { |
| 26 public: |
| 27 static_assert(std::is_same<T, typename DataViewTraits<T>::MojomType>::value, |
| 28 "DataView type mismatch"); |
| 29 |
| 30 using Data_ = typename MojomTypeTraits<Array<T>>::Data; |
| 31 |
| 32 ArrayDataViewImpl(Data_* data, SerializationContext* context) |
| 33 : data_(data), context_(context) {} |
| 34 |
| 35 T operator[](size_t index) const { return data_->at(index); } |
| 36 |
| 37 const T* data() const { return data_->storage(); } |
| 38 |
| 39 protected: |
| 40 Data_* data_; |
| 41 SerializationContext* context_; |
| 42 }; |
| 43 |
| 44 template <typename T> |
| 45 class ArrayDataViewImpl<T, |
| 46 typename std::enable_if<BelongsTo< |
| 47 typename DataViewTraits<T>::MojomType, |
| 48 MojomTypeCategory::BOOLEAN>::value>::type> { |
| 49 public: |
| 50 static_assert(std::is_same<T, typename DataViewTraits<T>::MojomType>::value, |
| 51 "DataView type mismatch"); |
| 52 |
| 53 using Data_ = typename MojomTypeTraits<Array<T>>::Data; |
| 54 |
| 55 ArrayDataViewImpl(Data_* data, SerializationContext* context) |
| 56 : data_(data), context_(context) {} |
| 57 |
| 58 bool operator[](size_t index) const { return data_->at(index); } |
| 59 |
| 60 protected: |
| 61 Data_* data_; |
| 62 SerializationContext* context_; |
| 63 }; |
| 64 |
| 65 template <typename T> |
| 66 class ArrayDataViewImpl< |
| 67 T, |
| 68 typename std::enable_if<BelongsTo<typename DataViewTraits<T>::MojomType, |
| 69 MojomTypeCategory::ENUM>::value>::type> { |
| 70 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"); |
| 74 |
| 75 using Data_ = typename MojomTypeTraits<Array<T>>::Data; |
| 76 |
| 77 ArrayDataViewImpl(Data_* data, SerializationContext* context) |
| 78 : data_(data), context_(context) {} |
| 79 |
| 80 T operator[](size_t index) const { return static_cast<T>(data_->at(index)); } |
| 81 |
| 82 const T* data() const { return reinterpret_cast<const T*>(data_->storage()); } |
| 83 |
| 84 template <typename U> |
| 85 bool Read(size_t index, U* output) { |
| 86 return Deserialize<T>(data_->at(index), output); |
| 87 } |
| 88 |
| 89 protected: |
| 90 Data_* data_; |
| 91 SerializationContext* context_; |
| 92 }; |
| 93 |
| 94 template <typename T> |
| 95 class ArrayDataViewImpl< |
| 96 T, |
| 97 typename std::enable_if< |
| 98 BelongsTo<typename DataViewTraits<T>::MojomType, |
| 99 MojomTypeCategory::ASSOCIATED_INTERFACE | |
| 100 MojomTypeCategory::ASSOCIATED_INTERFACE_REQUEST | |
| 101 MojomTypeCategory::HANDLE | |
| 102 MojomTypeCategory::INTERFACE | |
| 103 MojomTypeCategory::INTERFACE_REQUEST>::value>::type> { |
| 104 public: |
| 105 static_assert(std::is_same<T, typename DataViewTraits<T>::MojomType>::value, |
| 106 "DataView type mismatch"); |
| 107 |
| 108 using Data_ = typename MojomTypeTraits<Array<T>>::Data; |
| 109 |
| 110 ArrayDataViewImpl(Data_* data, SerializationContext* context) |
| 111 : data_(data), context_(context) {} |
| 112 |
| 113 T Take(size_t index) { |
| 114 T result; |
| 115 bool ret = Deserialize<T>(&data_->at(index), &result, context_); |
| 116 DCHECK(ret); |
| 117 return result; |
| 118 } |
| 119 |
| 120 protected: |
| 121 Data_* data_; |
| 122 SerializationContext* context_; |
| 123 }; |
| 124 |
| 125 template <typename T> |
| 126 class ArrayDataViewImpl<T, |
| 127 typename std::enable_if<BelongsTo< |
| 128 typename DataViewTraits<T>::MojomType, |
| 129 MojomTypeCategory::ARRAY | MojomTypeCategory::MAP | |
| 130 MojomTypeCategory::STRING | |
| 131 MojomTypeCategory::STRUCT>::value>::type> { |
| 132 public: |
| 133 using Data_ = typename MojomTypeTraits< |
| 134 Array<typename DataViewTraits<T>::MojomType>>::Data; |
| 135 |
| 136 ArrayDataViewImpl(Data_* data, SerializationContext* context) |
| 137 : data_(data), context_(context) {} |
| 138 |
| 139 void GetDataView(size_t index, T* output) { |
| 140 *output = T(data_->at(index).Get(), context_); |
| 141 } |
| 142 |
| 143 template <typename U> |
| 144 bool Read(size_t index, U* output) { |
| 145 return Deserialize<typename DataViewTraits<T>::MojomType>( |
| 146 data_->at(index).Get(), output, context_); |
| 147 } |
| 148 |
| 149 protected: |
| 150 Data_* data_; |
| 151 SerializationContext* context_; |
| 152 }; |
| 153 |
| 154 template <typename T> |
| 155 class ArrayDataViewImpl< |
| 156 T, |
| 157 typename std::enable_if<BelongsTo<typename DataViewTraits<T>::MojomType, |
| 158 MojomTypeCategory::UNION>::value>::type> { |
| 159 public: |
| 160 using Data_ = typename MojomTypeTraits< |
| 161 Array<typename DataViewTraits<T>::MojomType>>::Data; |
| 162 |
| 163 ArrayDataViewImpl(Data_* data, SerializationContext* context) |
| 164 : data_(data), context_(context) {} |
| 165 |
| 166 void GetDataView(size_t index, T* output) { |
| 167 *output = T(&data_->at(index), context_); |
| 168 } |
| 169 |
| 170 template <typename U> |
| 171 bool Read(size_t index, U* output) { |
| 172 return Deserialize<typename DataViewTraits<T>::MojomType>(&data_->at(index), |
| 173 output, context_); |
| 174 } |
| 175 |
| 176 protected: |
| 177 Data_* data_; |
| 178 SerializationContext* context_; |
| 179 }; |
| 180 |
| 181 } // namespace internal |
| 182 |
| 183 template <typename K, typename V> |
| 184 class MapDataView; |
| 185 |
| 186 template <typename T> |
| 187 class ArrayDataView : public internal::ArrayDataViewImpl<T> { |
| 188 public: |
| 189 using Data_ = typename internal::ArrayDataViewImpl<T>::Data_; |
| 190 |
| 191 ArrayDataView() : internal::ArrayDataViewImpl<T>(nullptr, nullptr) {} |
| 192 |
| 193 ArrayDataView(Data_* data, internal::SerializationContext* context) |
| 194 : internal::ArrayDataViewImpl<T>(data, context) {} |
| 195 |
| 196 bool is_null() const { return !this->data_; } |
| 197 |
| 198 size_t size() const { return this->data_->size(); } |
| 199 |
| 200 // Methods to access elements are different for different element types. They |
| 201 // are inherited from internal::ArrayDataViewImpl: |
| 202 |
| 203 // POD types except boolean and enums: |
| 204 // T operator[](size_t index) const; |
| 205 // const T* data() const; |
| 206 |
| 207 // Boolean: |
| 208 // bool operator[](size_t index) const; |
| 209 |
| 210 // Enums: |
| 211 // T operator[](size_t index) const; |
| 212 // const T* data() const; |
| 213 // template <typename U> |
| 214 // bool Read(size_t index, U* output); |
| 215 |
| 216 // Handles and interfaces: |
| 217 // T Take(size_t index); |
| 218 |
| 219 // Object types: |
| 220 // void GetDataView(size_t index, T* output); |
| 221 // template <typename U> |
| 222 // bool Read(size_t index, U* output); |
| 223 |
| 224 private: |
| 225 template <typename K, typename V> |
| 226 friend class MapDataView; |
| 227 }; |
| 228 |
| 229 } // namespace mojo |
| 230 |
| 231 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_DATA_VIEW_H_ |
OLD | NEW |