Fix crash when trying to change staticness of a property
[hiphop-php.git] / hphp / util / match.h
blob477125e6f778e0b6ade0a29d922d264a8c0eabd6
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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_
19 #include <utility>
21 #include <boost/variant.hpp>
23 namespace HPHP {
25 //////////////////////////////////////////////////////////////////////
28 * This is a utility for short-hand visitors (using lambdas) with
29 * boost::apply_visitor.
31 * Usage e.g.:
33 * match<return_type>(
34 * thing,
35 * [&] (TypeA a) { ... },
36 * [&] (TypeB b) { ... }
37 * );
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...> {
48 using L::operator();
49 using visitor<Ret,Lambdas...>::operator();
50 visitor(L l, Lambdas&&... lambdas)
51 : L(l)
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;
59 using L::operator();
60 /* implicit */ visitor(L l) : L(l) {}
63 template<class Ret> struct visitor<Ret> {
64 typedef Ret result_type;
65 visitor() {}
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)...),
79 std::forward<Var>(v)
83 //////////////////////////////////////////////////////////////////////
87 #endif