reveal.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. namespace reveal_test {
  14. #define CONDITIONAL_FUNCTION(n) \
  15. struct t ## n {}; \
  16. struct f ## n \
  17. { \
  18. constexpr int operator()(t ## n) const \
  19. { \
  20. return n; \
  21. } \
  22. };
  23. CONDITIONAL_FUNCTION(1)
  24. CONDITIONAL_FUNCTION(2)
  25. CONDITIONAL_FUNCTION(3)
  26. typedef boost::hof::first_of_adaptor<f1, f2, f3> f_type;
  27. static constexpr boost::hof::static_<f_type> f = {};
  28. BOOST_HOF_TEST_CASE()
  29. {
  30. BOOST_HOF_TEST_CHECK(boost::hof::reveal(f)(t1()) == 1);
  31. BOOST_HOF_TEST_CHECK(boost::hof::reveal(f)(t2()) == 2);
  32. BOOST_HOF_TEST_CHECK(boost::hof::reveal(f)(t3()) == 3);
  33. static_assert(boost::hof::is_invocable<boost::hof::reveal_adaptor<f_type>, t1>::value, "Invocable");
  34. static_assert(boost::hof::is_invocable<boost::hof::reveal_adaptor<f_type>, t2>::value, "Invocable");
  35. static_assert(boost::hof::is_invocable<boost::hof::reveal_adaptor<f_type>, t3>::value, "Invocable");
  36. static_assert(!boost::hof::is_invocable<boost::hof::reveal_adaptor<f_type>, int>::value, "Invocable");
  37. // boost::hof::reveal(f)(1);
  38. }
  39. #ifndef _MSC_VER
  40. static constexpr auto lam = boost::hof::first_of(
  41. BOOST_HOF_STATIC_LAMBDA(t1)
  42. {
  43. return 1;
  44. },
  45. BOOST_HOF_STATIC_LAMBDA(t2)
  46. {
  47. return 2;
  48. },
  49. BOOST_HOF_STATIC_LAMBDA(t3)
  50. {
  51. return 3;
  52. }
  53. );
  54. BOOST_HOF_TEST_CASE()
  55. {
  56. STATIC_ASSERT_EMPTY(lam);
  57. STATIC_ASSERT_EMPTY(boost::hof::reveal(lam));
  58. BOOST_HOF_TEST_CHECK(boost::hof::reveal(lam)(t1()) == 1);
  59. BOOST_HOF_TEST_CHECK(boost::hof::reveal(lam)(t2()) == 2);
  60. BOOST_HOF_TEST_CHECK(boost::hof::reveal(lam)(t3()) == 3);
  61. // boost::hof::reveal(lam)(1);
  62. // lam(1);
  63. }
  64. #endif
  65. BOOST_HOF_STATIC_LAMBDA_FUNCTION(static_fun) = boost::hof::first_of(
  66. [](t1)
  67. {
  68. return 1;
  69. },
  70. [](t2)
  71. {
  72. return 2;
  73. },
  74. [](t3)
  75. {
  76. return 3;
  77. }
  78. );
  79. BOOST_HOF_TEST_CASE()
  80. {
  81. #ifndef _MSC_VER
  82. STATIC_ASSERT_EMPTY(static_fun);
  83. // STATIC_ASSERT_EMPTY(boost::hof::reveal(static_fun));
  84. #endif
  85. BOOST_HOF_TEST_CHECK(boost::hof::reveal(static_fun)(t1()) == 1);
  86. BOOST_HOF_TEST_CHECK(boost::hof::reveal(static_fun)(t2()) == 2);
  87. BOOST_HOF_TEST_CHECK(boost::hof::reveal(static_fun)(t3()) == 3);
  88. BOOST_HOF_TEST_CHECK(static_fun(t1()) == 1);
  89. BOOST_HOF_TEST_CHECK(static_fun(t2()) == 2);
  90. BOOST_HOF_TEST_CHECK(static_fun(t3()) == 3);
  91. // boost::hof::reveal(static_fun)(1);
  92. }
  93. struct integral_type
  94. {
  95. template<class T>
  96. BOOST_HOF_USING_TYPENAME(failure_alias, std::enable_if<std::is_integral<T>::value>::type);
  97. struct failure
  98. : boost::hof::as_failure<failure_alias>
  99. {};
  100. template<class T, class=typename std::enable_if<std::is_integral<T>::value>::type>
  101. constexpr T operator()(T x) const
  102. {
  103. return x;
  104. }
  105. };
  106. struct foo {};
  107. struct dont_catch {};
  108. struct catch_all
  109. {
  110. template<class T>
  111. BOOST_HOF_USING_TYPENAME(failure_alias, std::enable_if<!std::is_same<T, dont_catch>::value>::type);
  112. struct failure
  113. : boost::hof::as_failure<failure_alias>
  114. {};
  115. template<class T, class=typename std::enable_if<!std::is_same<T, dont_catch>::value>::type>
  116. constexpr int operator()(T) const
  117. {
  118. return -1;
  119. }
  120. };
  121. static constexpr boost::hof::reveal_adaptor<boost::hof::first_of_adaptor<integral_type, catch_all>> check_failure = {};
  122. BOOST_HOF_TEST_CASE()
  123. {
  124. BOOST_HOF_TEST_CHECK(check_failure(5) == 5);
  125. BOOST_HOF_TEST_CHECK(check_failure(foo()) == -1);
  126. static_assert(!boost::hof::is_invocable<decltype(check_failure), dont_catch>::value, "Invocable");
  127. static_assert(!boost::hof::is_invocable<decltype(check_failure), int, int>::value, "Invocable");
  128. // check_failure(dont_catch());
  129. }
  130. }