Add progress_fn argument to MultiWorker.next
[hiphop-php.git] / hphp / util / match.h
blob71b378d825d9697cee593b491e54121920503e00
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 +----------------------------------------------------------------------+
16 #ifndef incl_HPHP_UTIL_MATCH_H_
17 #define incl_HPHP_UTIL_MATCH_H_
19 #include <type_traits>
20 #include <utility>
22 #include <boost/variant.hpp>
24 #include <folly/DiscriminatedPtr.h>
26 namespace HPHP {
28 //////////////////////////////////////////////////////////////////////
31 * This is a utility for short-hand visitors (using lambdas) with
32 * boost::apply_visitor.
34 * Usage e.g.:
36 * match<return_type>(
37 * thing,
38 * [&] (TypeA a) { ... },
39 * [&] (TypeB b) { ... }
40 * );
43 //////////////////////////////////////////////////////////////////////
45 namespace match_detail {
47 template<class Ret, class... Lambdas> struct visitor;
49 template<class Ret, class L, class... Lambdas>
50 struct visitor<Ret,L,Lambdas...> : L, visitor<Ret,Lambdas...> {
51 using L::operator();
52 using visitor<Ret,Lambdas...>::operator();
53 visitor(L l, Lambdas&&... lambdas)
54 : L(l)
55 , visitor<Ret,Lambdas...>(std::forward<Lambdas>(lambdas)...)
59 template<class Ret, class L>
60 struct visitor<Ret,L> : L {
61 typedef Ret result_type;
62 using L::operator();
63 /* implicit */ visitor(L l) : L(l) {}
66 template<class Ret> struct visitor<Ret> {
67 typedef Ret result_type;
68 visitor() {}
71 template<class Ret, class... Funcs>
72 visitor<Ret,Funcs...> make_visitor(Funcs&&... funcs) {
73 return { std::forward<Funcs>(funcs)... };
76 template <typename T> struct is_variant {
77 static constexpr bool value = false;
79 template <typename... T> struct is_variant<boost::variant<T...>> {
80 static constexpr bool value = true;
83 template <typename T> struct is_discriminated_ptr {
84 static constexpr bool value = false;
86 template <typename... T>
87 struct is_discriminated_ptr<folly::DiscriminatedPtr<T...>> {
88 static constexpr bool value = true;
93 template<class Ret, class Var, class... Funcs>
94 typename std::enable_if<
95 match_detail::is_variant<
96 typename std::remove_cv<Var>::type
97 >::value,
98 Ret>::type
99 match(Var& v, Funcs&&... funcs) {
100 return boost::apply_visitor(
101 match_detail::make_visitor<Ret>(std::forward<Funcs>(funcs)...),
106 template<class Ret, class Var, class... Funcs>
107 typename std::enable_if<
108 match_detail::is_discriminated_ptr<
109 typename std::remove_cv<Var>::type
110 >::value,
111 Ret>::type
112 match(Var& v, Funcs&&... funcs) {
113 return v.apply(
114 match_detail::make_visitor<Ret>(std::forward<Funcs>(funcs)...)
118 //////////////////////////////////////////////////////////////////////
122 #endif