| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_LIB_BINDINGS_INTERNAL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <functional> | 10 #include <functional> |
| 11 | 11 |
| 12 #include "base/template_util.h" | 12 #include "base/template_util.h" |
| 13 #include "mojo/public/cpp/bindings/interface_id.h" | 13 #include "mojo/public/cpp/bindings/interface_id.h" |
| 14 #include "mojo/public/cpp/bindings/lib/template_util.h" | 14 #include "mojo/public/cpp/bindings/lib/template_util.h" |
| 15 #include "mojo/public/cpp/system/core.h" | 15 #include "mojo/public/cpp/system/core.h" |
| 16 | 16 |
| 17 namespace mojo { | 17 namespace mojo { |
| 18 | 18 |
| 19 template <typename T> | 19 template <typename T> |
| 20 class Array; | |
| 21 | |
| 22 template <typename T> | |
| 23 class ArrayDataView; | 20 class ArrayDataView; |
| 24 | 21 |
| 25 template <typename T> | 22 template <typename T> |
| 26 class AssociatedInterfacePtrInfo; | 23 class AssociatedInterfacePtrInfoDataView; |
| 27 | 24 |
| 28 template <typename T> | 25 template <typename T> |
| 29 class AssociatedInterfaceRequest; | 26 class AssociatedInterfaceRequestDataView; |
| 30 | 27 |
| 31 template <typename T> | 28 template <typename T> |
| 32 class InterfacePtr; | 29 class InterfacePtrDataView; |
| 33 | 30 |
| 34 template <typename T> | 31 template <typename T> |
| 35 class InterfaceRequest; | 32 class InterfaceRequestDataView; |
| 36 | |
| 37 template <typename K, typename V> | |
| 38 class Map; | |
| 39 | 33 |
| 40 template <typename K, typename V> | 34 template <typename K, typename V> |
| 41 class MapDataView; | 35 class MapDataView; |
| 42 | 36 |
| 43 class NativeStruct; | |
| 44 | |
| 45 class NativeStructDataView; | 37 class NativeStructDataView; |
| 46 | 38 |
| 47 class String; | |
| 48 | |
| 49 class StringDataView; | 39 class StringDataView; |
| 50 | 40 |
| 51 template <typename T> | |
| 52 class StructPtr; | |
| 53 | |
| 54 template <typename T> | |
| 55 class InlinedStructPtr; | |
| 56 | |
| 57 using NativeStructPtr = StructPtr<NativeStruct>; | |
| 58 | |
| 59 namespace internal { | 41 namespace internal { |
| 60 | 42 |
| 61 // Please note that this is a different value than |mojo::kInvalidHandleValue|, | 43 // Please note that this is a different value than |mojo::kInvalidHandleValue|, |
| 62 // which is the "decoded" invalid handle. | 44 // which is the "decoded" invalid handle. |
| 63 const uint32_t kEncodedInvalidHandleValue = static_cast<uint32_t>(-1); | 45 const uint32_t kEncodedInvalidHandleValue = static_cast<uint32_t>(-1); |
| 64 | 46 |
| 65 // A serialized union always takes 16 bytes: | 47 // A serialized union always takes 16 bytes: |
| 66 // 4-byte size + 4-byte tag + 8-byte payload. | 48 // 4-byte size + 4-byte tag + 8-byte payload. |
| 67 const uint32_t kUnionDataSize = 16; | 49 const uint32_t kUnionDataSize = 16; |
| 68 | 50 |
| 69 template <typename T> | 51 template <typename T> |
| 70 class Array_Data; | 52 class Array_Data; |
| 71 | 53 |
| 72 template <typename K, typename V> | 54 template <typename K, typename V> |
| 73 class Map_Data; | 55 class Map_Data; |
| 74 | 56 |
| 57 class NativeStruct_Data; |
| 58 |
| 75 using String_Data = Array_Data<char>; | 59 using String_Data = Array_Data<char>; |
| 76 | 60 |
| 77 inline size_t Align(size_t size) { | 61 inline size_t Align(size_t size) { |
| 78 return (size + 7) & ~0x7; | 62 return (size + 7) & ~0x7; |
| 79 } | 63 } |
| 80 | 64 |
| 81 inline bool IsAligned(const void* ptr) { | 65 inline bool IsAligned(const void* ptr) { |
| 82 return !(reinterpret_cast<uintptr_t>(ptr) & 0x7); | 66 return !(reinterpret_cast<uintptr_t>(ptr) & 0x7); |
| 83 } | 67 } |
| 84 | 68 |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 | 209 |
| 226 template <typename T, bool is_enum = std::is_enum<T>::value> | 210 template <typename T, bool is_enum = std::is_enum<T>::value> |
| 227 struct MojomTypeTraits { | 211 struct MojomTypeTraits { |
| 228 using Data = T; | 212 using Data = T; |
| 229 using DataAsArrayElement = Data; | 213 using DataAsArrayElement = Data; |
| 230 | 214 |
| 231 static const MojomTypeCategory category = MojomTypeCategory::POD; | 215 static const MojomTypeCategory category = MojomTypeCategory::POD; |
| 232 }; | 216 }; |
| 233 | 217 |
| 234 template <typename T> | 218 template <typename T> |
| 235 struct MojomTypeTraits<Array<T>, false> { | 219 struct MojomTypeTraits<ArrayDataView<T>, false> { |
| 236 using Data = Array_Data<typename MojomTypeTraits<T>::DataAsArrayElement>; | 220 using Data = Array_Data<typename MojomTypeTraits<T>::DataAsArrayElement>; |
| 237 using DataAsArrayElement = Pointer<Data>; | 221 using DataAsArrayElement = Pointer<Data>; |
| 238 | 222 |
| 239 static const MojomTypeCategory category = MojomTypeCategory::ARRAY; | 223 static const MojomTypeCategory category = MojomTypeCategory::ARRAY; |
| 240 }; | 224 }; |
| 241 | 225 |
| 242 template <typename T> | 226 template <typename T> |
| 243 struct MojomTypeTraits<AssociatedInterfacePtrInfo<T>, false> { | 227 struct MojomTypeTraits<AssociatedInterfacePtrInfoDataView<T>, false> { |
| 244 using Data = AssociatedInterface_Data; | 228 using Data = AssociatedInterface_Data; |
| 245 using DataAsArrayElement = Data; | 229 using DataAsArrayElement = Data; |
| 246 | 230 |
| 247 static const MojomTypeCategory category = | 231 static const MojomTypeCategory category = |
| 248 MojomTypeCategory::ASSOCIATED_INTERFACE; | 232 MojomTypeCategory::ASSOCIATED_INTERFACE; |
| 249 }; | 233 }; |
| 250 | 234 |
| 251 template <typename T> | 235 template <typename T> |
| 252 struct MojomTypeTraits<AssociatedInterfaceRequest<T>, false> { | 236 struct MojomTypeTraits<AssociatedInterfaceRequestDataView<T>, false> { |
| 253 using Data = AssociatedInterfaceRequest_Data; | 237 using Data = AssociatedInterfaceRequest_Data; |
| 254 using DataAsArrayElement = Data; | 238 using DataAsArrayElement = Data; |
| 255 | 239 |
| 256 static const MojomTypeCategory category = | 240 static const MojomTypeCategory category = |
| 257 MojomTypeCategory::ASSOCIATED_INTERFACE_REQUEST; | 241 MojomTypeCategory::ASSOCIATED_INTERFACE_REQUEST; |
| 258 }; | 242 }; |
| 259 | 243 |
| 260 template <> | 244 template <> |
| 261 struct MojomTypeTraits<bool, false> { | 245 struct MojomTypeTraits<bool, false> { |
| 262 using Data = bool; | 246 using Data = bool; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 275 | 259 |
| 276 template <typename T> | 260 template <typename T> |
| 277 struct MojomTypeTraits<ScopedHandleBase<T>, false> { | 261 struct MojomTypeTraits<ScopedHandleBase<T>, false> { |
| 278 using Data = Handle_Data; | 262 using Data = Handle_Data; |
| 279 using DataAsArrayElement = Data; | 263 using DataAsArrayElement = Data; |
| 280 | 264 |
| 281 static const MojomTypeCategory category = MojomTypeCategory::HANDLE; | 265 static const MojomTypeCategory category = MojomTypeCategory::HANDLE; |
| 282 }; | 266 }; |
| 283 | 267 |
| 284 template <typename T> | 268 template <typename T> |
| 285 struct MojomTypeTraits<InterfacePtr<T>, false> { | 269 struct MojomTypeTraits<InterfacePtrDataView<T>, false> { |
| 286 using Data = Interface_Data; | 270 using Data = Interface_Data; |
| 287 using DataAsArrayElement = Data; | 271 using DataAsArrayElement = Data; |
| 288 | 272 |
| 289 static const MojomTypeCategory category = MojomTypeCategory::INTERFACE; | 273 static const MojomTypeCategory category = MojomTypeCategory::INTERFACE; |
| 290 }; | 274 }; |
| 291 | 275 |
| 292 template <typename T> | 276 template <typename T> |
| 293 struct MojomTypeTraits<InterfaceRequest<T>, false> { | 277 struct MojomTypeTraits<InterfaceRequestDataView<T>, false> { |
| 294 using Data = Handle_Data; | 278 using Data = Handle_Data; |
| 295 using DataAsArrayElement = Data; | 279 using DataAsArrayElement = Data; |
| 296 | 280 |
| 297 static const MojomTypeCategory category = | 281 static const MojomTypeCategory category = |
| 298 MojomTypeCategory::INTERFACE_REQUEST; | 282 MojomTypeCategory::INTERFACE_REQUEST; |
| 299 }; | 283 }; |
| 300 | 284 |
| 301 template <typename K, typename V> | 285 template <typename K, typename V> |
| 302 struct MojomTypeTraits<Map<K, V>, false> { | 286 struct MojomTypeTraits<MapDataView<K, V>, false> { |
| 303 using Data = Map_Data<typename MojomTypeTraits<K>::DataAsArrayElement, | 287 using Data = Map_Data<typename MojomTypeTraits<K>::DataAsArrayElement, |
| 304 typename MojomTypeTraits<V>::DataAsArrayElement>; | 288 typename MojomTypeTraits<V>::DataAsArrayElement>; |
| 305 using DataAsArrayElement = Pointer<Data>; | 289 using DataAsArrayElement = Pointer<Data>; |
| 306 | 290 |
| 307 static const MojomTypeCategory category = MojomTypeCategory::MAP; | 291 static const MojomTypeCategory category = MojomTypeCategory::MAP; |
| 308 }; | 292 }; |
| 309 | 293 |
| 310 template <> | 294 template <> |
| 311 struct MojomTypeTraits<String, false> { | 295 struct MojomTypeTraits<NativeStructDataView, false> { |
| 296 using Data = internal::NativeStruct_Data; |
| 297 using DataAsArrayElement = Pointer<Data>; |
| 298 |
| 299 static const MojomTypeCategory category = MojomTypeCategory::STRUCT; |
| 300 }; |
| 301 |
| 302 template <> |
| 303 struct MojomTypeTraits<StringDataView, false> { |
| 312 using Data = String_Data; | 304 using Data = String_Data; |
| 313 using DataAsArrayElement = Pointer<Data>; | 305 using DataAsArrayElement = Pointer<Data>; |
| 314 | 306 |
| 315 static const MojomTypeCategory category = MojomTypeCategory::STRING; | 307 static const MojomTypeCategory category = MojomTypeCategory::STRING; |
| 316 }; | 308 }; |
| 317 | 309 |
| 318 template <typename T> | |
| 319 struct MojomTypeTraits<StructPtr<T>, false> { | |
| 320 using Data = typename T::Data_; | |
| 321 using DataAsArrayElement = | |
| 322 typename std::conditional<IsUnionDataType<Data>::value, | |
| 323 Data, | |
| 324 Pointer<Data>>::type; | |
| 325 | |
| 326 static const MojomTypeCategory category = IsUnionDataType<Data>::value | |
| 327 ? MojomTypeCategory::UNION | |
| 328 : MojomTypeCategory::STRUCT; | |
| 329 }; | |
| 330 | |
| 331 template <typename T> | |
| 332 struct MojomTypeTraits<InlinedStructPtr<T>, false> { | |
| 333 using Data = typename T::Data_; | |
| 334 using DataAsArrayElement = | |
| 335 typename std::conditional<IsUnionDataType<Data>::value, | |
| 336 Data, | |
| 337 Pointer<Data>>::type; | |
| 338 | |
| 339 static const MojomTypeCategory category = IsUnionDataType<Data>::value | |
| 340 ? MojomTypeCategory::UNION | |
| 341 : MojomTypeCategory::STRUCT; | |
| 342 }; | |
| 343 | |
| 344 template <typename T, MojomTypeCategory categories> | 310 template <typename T, MojomTypeCategory categories> |
| 345 struct BelongsTo { | 311 struct BelongsTo { |
| 346 static const bool value = | 312 static const bool value = |
| 347 static_cast<uint32_t>(MojomTypeTraits<T>::category & categories) != 0; | 313 static_cast<uint32_t>(MojomTypeTraits<T>::category & categories) != 0; |
| 348 }; | 314 }; |
| 349 | 315 |
| 350 template <typename T> | 316 template <typename T> |
| 351 struct EnumHashImpl { | 317 struct EnumHashImpl { |
| 352 static_assert(std::is_enum<T>::value, "Incorrect hash function."); | 318 static_assert(std::is_enum<T>::value, "Incorrect hash function."); |
| 353 | 319 |
| 354 size_t operator()(T input) const { | 320 size_t operator()(T input) const { |
| 355 using UnderlyingType = typename base::underlying_type<T>::type; | 321 using UnderlyingType = typename base::underlying_type<T>::type; |
| 356 return std::hash<UnderlyingType>()(static_cast<UnderlyingType>(input)); | 322 return std::hash<UnderlyingType>()(static_cast<UnderlyingType>(input)); |
| 357 } | 323 } |
| 358 }; | 324 }; |
| 359 | 325 |
| 360 template <typename T> | |
| 361 struct DataViewTraits { | |
| 362 using MojomType = T; | |
| 363 }; | |
| 364 | |
| 365 template <typename T> | |
| 366 struct DataViewTraits<ArrayDataView<T>> { | |
| 367 using MojomType = Array<typename DataViewTraits<T>::MojomType>; | |
| 368 }; | |
| 369 | |
| 370 template <typename K, typename V> | |
| 371 struct DataViewTraits<MapDataView<K, V>> { | |
| 372 using MojomType = Map<typename DataViewTraits<K>::MojomType, | |
| 373 typename DataViewTraits<V>::MojomType>; | |
| 374 }; | |
| 375 | |
| 376 template <> | |
| 377 struct DataViewTraits<StringDataView> { | |
| 378 using MojomType = String; | |
| 379 }; | |
| 380 | |
| 381 template <> | |
| 382 struct DataViewTraits<NativeStructDataView> { | |
| 383 using MojomType = NativeStructPtr; | |
| 384 }; | |
| 385 | |
| 386 } // namespace internal | 326 } // namespace internal |
| 387 } // namespace mojo | 327 } // namespace mojo |
| 388 | 328 |
| 389 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_ | 329 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_ |
| OLD | NEW |