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 COMPONENTS_SCHEDULER_PROMISES_TEMPLATE_HELPERS_H_ |
| 6 #define COMPONENTS_SCHEDULER_PROMISES_TEMPLATE_HELPERS_H_ |
| 7 |
| 8 #include <type_traits> |
| 9 |
| 10 namespace promise { |
| 11 namespace internal { |
| 12 class PromiseBase; |
| 13 |
| 14 template <typename R> |
| 15 class Promise; |
| 16 |
| 17 template <typename T, typename ReturnType> |
| 18 using enable_if_void_t = |
| 19 typename std::enable_if<std::is_void<T>::value, ReturnType>::type; |
| 20 |
| 21 template <typename T, typename ReturnType> |
| 22 using enable_if_not_void_t = |
| 23 typename std::enable_if<!std::is_void<T>::value, ReturnType>::type; |
| 24 |
| 25 // A paramater pack introspection template that can determine if the pack |
| 26 // contains |TypeBeingChecked|. |
| 27 template <typename TypeBeingChecked, typename Head, typename... Tail> |
| 28 struct check_any_have_type { |
| 29 enum { |
| 30 value = std::is_same<TypeBeingChecked, Head>::value || |
| 31 check_any_have_type<TypeBeingChecked, Tail...>::value |
| 32 }; |
| 33 }; |
| 34 |
| 35 template <typename TypeBeingChecked, typename Head> |
| 36 struct check_any_have_type<TypeBeingChecked, Head> { |
| 37 enum { value = std::is_same<TypeBeingChecked, Head>::value }; |
| 38 }; |
| 39 |
| 40 // A paramater pack introspection template that can determine if the pack |
| 41 // only contains |RequiredType|. |
| 42 template <typename RequiredType, typename Head, typename... Tail> |
| 43 struct check_all_same_type { |
| 44 enum { |
| 45 value = std::is_same<RequiredType, Head>::value && |
| 46 check_all_same_type<RequiredType, Tail...>::value |
| 47 }; |
| 48 }; |
| 49 |
| 50 template <typename RequiredType, typename Head> |
| 51 struct check_all_same_type<RequiredType, Head> { |
| 52 enum { value = std::is_same<RequiredType, Head>::value }; |
| 53 }; |
| 54 |
| 55 // This template is for returning the return type of the Nth promise in a |
| 56 // parameter pack of scoped_ref_ptrs<Promise<R>> |
| 57 template <std::size_t Index, typename T, typename... Ts> |
| 58 struct nth_promise_impl { |
| 59 using Type = typename nth_promise_impl<Index - 1, Ts...>::Type; |
| 60 }; |
| 61 |
| 62 template <typename T, typename... Ts> |
| 63 struct nth_promise_impl<0, T, Ts...> { |
| 64 using Type = typename T::ReturnType; |
| 65 }; |
| 66 |
| 67 // This template is for calling |GetResolved| on the Nth promise in a |
| 68 // parameter pack of scoped_ref_ptrs<Promise<R>> where the actual promises are |
| 69 // held in a std::vector<scoped_refptr<PromiseBase>>. |
| 70 template <std::size_t Index, typename... Promises> |
| 71 struct PromiseResolver { |
| 72 using ReturnType = typename nth_promise_impl<Index, Promises...>::Type; |
| 73 |
| 74 static ReturnType GetResolved( |
| 75 std::vector<scoped_refptr<PromiseBase>>& promises) { |
| 76 return std::move(static_cast<Promise<ReturnType>*>(promises[Index].get()) |
| 77 ->GetResolved()); |
| 78 } |
| 79 }; |
| 80 |
| 81 // This template is for generating a parameter pack of N indicies. |
| 82 // When C++14 is allowed we can replace with std::integer_sequence. |
| 83 template <size_t... Ns> |
| 84 struct indices { |
| 85 typedef indices<Ns..., sizeof...(Ns)> next; |
| 86 }; |
| 87 |
| 88 template <size_t N> |
| 89 struct make_indices { |
| 90 typedef typename make_indices<N - 1>::type::next type; |
| 91 }; |
| 92 |
| 93 template <> |
| 94 struct make_indices<0> { |
| 95 typedef indices<> type; |
| 96 }; |
| 97 |
| 98 } // namespace internal |
| 99 } // namespace promise |
| 100 |
| 101 #endif // COMPONENTS_SCHEDULER_PROMISES_TEMPLATE_HELPERS_H_ |
OLD | NEW |