2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2014 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 +----------------------------------------------------------------------+
16 #ifndef incl_HPHP_UTIL_MATCH_H_
17 #define incl_HPHP_UTIL_MATCH_H_
21 #include <boost/variant.hpp>
25 //////////////////////////////////////////////////////////////////////
28 * This is a utility for short-hand visitors (using lambdas) with
29 * boost::apply_visitor.
35 * [&] (TypeA a) { ... },
36 * [&] (TypeB b) { ... }
40 //////////////////////////////////////////////////////////////////////
42 namespace match_detail
{
44 template<class Ret
, class... Lambdas
> struct visitor
;
46 template<class Ret
, class L
, class... Lambdas
>
47 struct visitor
<Ret
,L
,Lambdas
...> : L
, visitor
<Ret
,Lambdas
...> {
49 using visitor
<Ret
,Lambdas
...>::operator();
50 visitor(L l
, Lambdas
&&... lambdas
)
52 , visitor
<Ret
,Lambdas
...>(std::forward
<Lambdas
>(lambdas
)...)
56 template<class Ret
, class L
>
57 struct visitor
<Ret
,L
> : L
{
58 typedef Ret result_type
;
60 /* implicit */ visitor(L l
) : L(l
) {}
63 template<class Ret
> struct visitor
<Ret
> {
64 typedef Ret result_type
;
68 template<class Ret
, class... Funcs
>
69 visitor
<Ret
,Funcs
...> make_visitor(Funcs
&&... funcs
) {
70 return { std::forward
<Funcs
>(funcs
)... };
75 template<class Ret
, class Var
, class... Funcs
>
76 Ret
match(Var
&& v
, Funcs
&&... funcs
) {
77 return boost::apply_visitor(
78 match_detail::make_visitor
<Ret
>(std::forward
<Funcs
>(funcs
)...),
83 //////////////////////////////////////////////////////////////////////