Deshim VirtualExecutor in folly
[hiphop-php.git] / hphp / util / match.h
blob2993566ef222e35d8abfa6acd28ada19dcd06b20
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 #pragma once
18 #include <type_traits>
19 #include <utility>
21 #include <boost/variant.hpp>
23 #include <folly/DiscriminatedPtr.h>
25 namespace HPHP {
27 //////////////////////////////////////////////////////////////////////
30 * This is a utility for short-hand visitors (using lambdas) with
31 * boost::apply_visitor.
33 * Usage e.g.:
35 * match<return_type>(
36 * thing,
37 * [&] (TypeA a) { ... },
38 * [&] (TypeB b) { ... }
39 * );
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...> {
50 using L::operator();
51 using visitor<Ret,Lambdas...>::operator();
52 visitor(L l, Lambdas&&... lambdas)
53 : L(l)
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;
61 using L::operator();
62 /* implicit */ visitor(L l) : L(l) {}
65 template<class Ret> struct visitor<Ret> {
66 typedef Ret result_type;
67 visitor() {}
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
96 >::value,
97 Ret>::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
109 >::value,
110 Ret>::type
111 match(Var& v, Funcs&&... funcs) {
112 return v.apply(
113 match_detail::make_visitor<Ret>(std::forward<Funcs>(funcs)...)
117 //////////////////////////////////////////////////////////////////////