2 +----------------------------------------------------------------------+
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 +----------------------------------------------------------------------+
18 #include <type_traits>
21 #include <boost/variant.hpp>
23 #include <folly/DiscriminatedPtr.h>
27 //////////////////////////////////////////////////////////////////////
30 * This is a utility for short-hand visitors (using lambdas) with
31 * boost::apply_visitor.
37 * [&] (TypeA a) { ... },
38 * [&] (TypeB b) { ... }
42 //////////////////////////////////////////////////////////////////////
44 namespace match_detail
{
46 template<class Ret
, class... Lambdas
> struct visitor
;
48 template<class Ret
, class L
, class... Lambdas
>
49 struct visitor
<Ret
,L
,Lambdas
...> : L
, visitor
<Ret
,Lambdas
...> {
51 using visitor
<Ret
,Lambdas
...>::operator();
52 visitor(L l
, Lambdas
&&... lambdas
)
54 , visitor
<Ret
,Lambdas
...>(std::forward
<Lambdas
>(lambdas
)...)
58 template<class Ret
, class L
>
59 struct visitor
<Ret
,L
> : L
{
60 typedef Ret result_type
;
62 /* implicit */ visitor(L l
) : L(l
) {}
65 template<class Ret
> struct visitor
<Ret
> {
66 typedef Ret result_type
;
70 template<class Ret
, class... Funcs
>
71 visitor
<Ret
,Funcs
...> make_visitor(Funcs
&&... funcs
) {
72 return { std::forward
<Funcs
>(funcs
)... };
75 template <typename T
> struct is_variant
{
76 static constexpr bool value
= false;
78 template <typename
... T
> struct is_variant
<boost::variant
<T
...>> {
79 static constexpr bool value
= true;
82 template <typename T
> struct is_discriminated_ptr
{
83 static constexpr bool value
= false;
85 template <typename
... T
>
86 struct is_discriminated_ptr
<folly::DiscriminatedPtr
<T
...>> {
87 static constexpr bool value
= true;
92 template<class Ret
, class Var
, class... Funcs
>
93 typename
std::enable_if
<
94 match_detail::is_variant
<
95 typename
std::remove_cv
<Var
>::type
98 match(Var
& v
, Funcs
&&... funcs
) {
99 return boost::apply_visitor(
100 match_detail::make_visitor
<Ret
>(std::forward
<Funcs
>(funcs
)...),
105 template<class Ret
, class Var
, class... Funcs
>
106 typename
std::enable_if
<
107 match_detail::is_discriminated_ptr
<
108 typename
std::remove_cv
<Var
>::type
111 match(Var
& v
, Funcs
&&... funcs
) {
113 match_detail::make_visitor
<Ret
>(std::forward
<Funcs
>(funcs
)...)
117 //////////////////////////////////////////////////////////////////////