Toplevel entrypoints for classes/traits/interfaces
[hiphop-php.git] / hphp / util / type-traits.h
blob259b0855033a8339938de768a0694cd21feef0d7
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #pragma once
19 #include <type_traits>
20 #include <functional>
22 namespace HPHP {
24 ///////////////////////////////////////////////////////////////////////////////
26 template<typename T>
27 constexpr auto identity(T&& t) noexcept -> decltype(std::forward<T>(t)) {
28 return std::forward<T>(t);
31 ///////////////////////////////////////////////////////////////////////////////
34 * Is `T' the same type as `U' or any type in `Tail...'?
36 template<class T, class U, class... Tail> struct is_any;
38 template<class T, class U>
39 struct is_any<T,U> : std::is_same<T,U> {};
41 template<class T, class U, class V, class... Tail>
42 struct is_any<T,U,V,Tail...> : std::integral_constant<
43 bool,
44 std::is_same<T,U>::value || is_any<T,V,Tail...>::value
45 > {};
47 ///////////////////////////////////////////////////////////////////////////////
50 * Identity functor.
52 template<typename T> struct ident { using type = T; };
53 template<typename T> using ident_t = typename ident<T>::type;
56 * Stateless tuple for parameter unpacking.
58 template<typename...> struct pack {};
61 * Clone of std::conjunction<>.
63 template<class...> struct conjunction : std::true_type { };
64 template<class B1> struct conjunction<B1> : B1 { };
65 template<class B1, class... Bn>
66 struct conjunction<B1, Bn...>
67 : std::conditional_t<bool(B1::value), conjunction<Bn...>, B1> {};
70 * Clone of std::disjunction<>.
72 template<class...> struct disjunction : std::false_type { };
73 template<class B1> struct disjunction<B1> : B1 { };
74 template<class B1, class... Bn>
75 struct disjunction<B1, Bn...>
76 : std::conditional_t<bool(B1::value), B1, disjunction<Bn...>> {};
79 * Helper for appending a type parameter to a template class's variadic
80 * parameter list.
82 template<typename Head, typename Tail> struct cons;
83 template<template<typename...> class List, typename T, typename... Args>
84 struct cons<T, List<Args...>> {
85 using type = List<T, Args...>;
88 ///////////////////////////////////////////////////////////////////////////////
91 * Whether `T' and `U' are the same type, with the outermost const-qualifier
92 * stripped away.
94 template<class T, class U>
95 using is_same_upto_const = std::is_same<
96 typename std::remove_const<T>::type,
97 typename std::remove_const<U>::type
100 namespace detail {
102 template<class... Ts> struct maybe_const_pred;
104 template<>
105 struct maybe_const_pred<> : std::true_type {};
107 template<class T>
108 struct maybe_const_pred<T> : std::true_type {};
110 template<class T, class U, class... Tail>
111 struct maybe_const_pred<T, U, Tail...> : std::integral_constant<
112 bool,
113 is_same_upto_const<T,U>::value &&
114 maybe_const_pred<Tail...>::value
115 > {};
117 template<class... Ts> struct maybe_const_result;
119 template<>
120 struct maybe_const_result<> { using type = void; };
122 template<class R>
123 struct maybe_const_result<R> { using type = R; };
125 template<class T, class U, class... Tail>
126 struct maybe_const_result<T, U, Tail...> : maybe_const_result<Tail...> {};
131 * Pattern for templatizing functions to take an optionally-const argument.
132 * Writing
134 * template<class T1, class T2, ...>
135 * maybe_const<T1, K1, T2, K2, ..., R>::type
137 * is equivalent to
139 * template<class T1, class T2, ...>
140 * std::enable_if<
141 * is_same_upto_const<T1, K1>::value &&
142 * is_same_upto_const<T2, K2>::value &&
143 * ...,
145 * >::type
147 template<class T, class K, class... Tail>
148 using maybe_const = std::enable_if<
149 detail::maybe_const_pred<T, K, Tail...>::value,
150 typename detail::maybe_const_result<T, K, Tail...>::type
153 template <typename Fn, typename ... Args>
154 struct is_invocable : public std::is_constructible<
155 std::function<void(Args ...)>, Fn&&
156 > {};
158 ///////////////////////////////////////////////////////////////////////////////