• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2017 Paul Fultz II
3     reveal.cpp
4     Distributed under the Boost Software License, Version 1.0. (See accompanying
5     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 #include "test.hpp"
8 #include <boost/hof/reveal.hpp>
9 #include <boost/hof/first_of.hpp>
10 #include <boost/hof/static.hpp>
11 #include <boost/hof/lambda.hpp>
12 #include <boost/hof/fix.hpp>
13 
14 namespace reveal_test {
15 
16 #define CONDITIONAL_FUNCTION(n) \
17 struct t ## n {}; \
18 struct f ## n \
19 { \
20     constexpr int operator()(t ## n) const \
21     { \
22         return n; \
23     } \
24 };
25 
26 CONDITIONAL_FUNCTION(1)
27 CONDITIONAL_FUNCTION(2)
28 CONDITIONAL_FUNCTION(3)
29 
30 typedef boost::hof::first_of_adaptor<f1, f2, f3> f_type;
31 static constexpr boost::hof::static_<f_type> f = {};
32 
BOOST_HOF_TEST_CASE()33 BOOST_HOF_TEST_CASE()
34 {
35     BOOST_HOF_TEST_CHECK(boost::hof::reveal(f)(t1()) == 1);
36     BOOST_HOF_TEST_CHECK(boost::hof::reveal(f)(t2()) == 2);
37     BOOST_HOF_TEST_CHECK(boost::hof::reveal(f)(t3()) == 3);
38 
39 
40     static_assert(boost::hof::is_invocable<boost::hof::reveal_adaptor<f_type>, t1>::value, "Invocable");
41     static_assert(boost::hof::is_invocable<boost::hof::reveal_adaptor<f_type>, t2>::value, "Invocable");
42     static_assert(boost::hof::is_invocable<boost::hof::reveal_adaptor<f_type>, t3>::value, "Invocable");
43 
44     static_assert(!boost::hof::is_invocable<boost::hof::reveal_adaptor<f_type>, int>::value, "Invocable");
45     // boost::hof::reveal(f)(1);
46 }
47 
48 #ifndef _MSC_VER
49 static constexpr auto lam = boost::hof::first_of(
50     BOOST_HOF_STATIC_LAMBDA(t1)
51     {
52         return 1;
53     },
54     BOOST_HOF_STATIC_LAMBDA(t2)
55     {
56         return 2;
57     },
58     BOOST_HOF_STATIC_LAMBDA(t3)
59     {
60         return 3;
61     }
62 );
63 
BOOST_HOF_TEST_CASE()64 BOOST_HOF_TEST_CASE()
65 {
66     STATIC_ASSERT_EMPTY(lam);
67     STATIC_ASSERT_EMPTY(boost::hof::reveal(lam));
68     BOOST_HOF_TEST_CHECK(boost::hof::reveal(lam)(t1()) == 1);
69     BOOST_HOF_TEST_CHECK(boost::hof::reveal(lam)(t2()) == 2);
70     BOOST_HOF_TEST_CHECK(boost::hof::reveal(lam)(t3()) == 3);
71 
72     // boost::hof::reveal(lam)(1);
73     // lam(1);
74 }
75 #endif
76 
77 BOOST_HOF_STATIC_LAMBDA_FUNCTION(static_fun) = boost::hof::first_of(
78     [](t1)
__anond38515bd0102(t1) 79     {
80         return 1;
81     },
82     [](t2)
__anond38515bd0202(t2) 83     {
84         return 2;
85     },
86     [](t3)
__anond38515bd0302(t3) 87     {
88         return 3;
89     }
90 );
91 
BOOST_HOF_TEST_CASE()92 BOOST_HOF_TEST_CASE()
93 {
94 #ifndef _MSC_VER
95     STATIC_ASSERT_EMPTY(static_fun);
96     // STATIC_ASSERT_EMPTY(boost::hof::reveal(static_fun));
97 #endif
98     BOOST_HOF_TEST_CHECK(boost::hof::reveal(static_fun)(t1()) == 1);
99     BOOST_HOF_TEST_CHECK(boost::hof::reveal(static_fun)(t2()) == 2);
100     BOOST_HOF_TEST_CHECK(boost::hof::reveal(static_fun)(t3()) == 3);
101 
102     BOOST_HOF_TEST_CHECK(static_fun(t1()) == 1);
103     BOOST_HOF_TEST_CHECK(static_fun(t2()) == 2);
104     BOOST_HOF_TEST_CHECK(static_fun(t3()) == 3);
105 
106     // boost::hof::reveal(static_fun)(1);
107 }
108 
109 struct integral_type
110 {
111     template<class T>
112     BOOST_HOF_USING_TYPENAME(failure_alias, std::enable_if<std::is_integral<T>::value>::type);
113 
114     struct failure
115     : boost::hof::as_failure<failure_alias>
116     {};
117 
118     template<class T, class=typename std::enable_if<std::is_integral<T>::value>::type>
operator ()reveal_test::integral_type119     constexpr T operator()(T x) const
120     {
121         return x;
122     }
123 };
124 struct foo {};
125 struct dont_catch {};
126 
127 struct catch_all
128 {
129     template<class T>
130     BOOST_HOF_USING_TYPENAME(failure_alias, std::enable_if<!std::is_same<T, dont_catch>::value>::type);
131 
132     struct failure
133     : boost::hof::as_failure<failure_alias>
134     {};
135 
136     template<class T, class=typename std::enable_if<!std::is_same<T, dont_catch>::value>::type>
operator ()reveal_test::catch_all137     constexpr int operator()(T) const
138     {
139         return -1;
140     }
141 };
142 
143 static constexpr boost::hof::reveal_adaptor<boost::hof::first_of_adaptor<integral_type, catch_all>> check_failure = {};
144 
145 
BOOST_HOF_TEST_CASE()146 BOOST_HOF_TEST_CASE()
147 {
148     BOOST_HOF_TEST_CHECK(check_failure(5) == 5);
149     BOOST_HOF_TEST_CHECK(check_failure(foo()) == -1);
150 
151     static_assert(!boost::hof::is_invocable<decltype(check_failure), dont_catch>::value, "Invocable");
152     static_assert(!boost::hof::is_invocable<decltype(check_failure), int, int>::value, "Invocable");
153 
154     // check_failure(dont_catch());
155 }
156 
157 }
158