| 1 | // Boost.TypeErasure library |
| 2 | // |
| 3 | // Copyright 2011 Steven Watanabe |
| 4 | // |
| 5 | // Distributed under the Boost Software License Version 1.0. (See |
| 6 | // accompanying file LICENSE_1_0.txt or copy at |
| 7 | // http://www.boost.org/LICENSE_1_0.txt) |
| 8 | // |
| 9 | // $Id$ |
| 10 | |
| 11 | #include <boost/type_erasure/any.hpp> |
| 12 | #include <boost/type_erasure/free.hpp> |
| 13 | #include <boost/type_erasure/builtin.hpp> |
| 14 | #include <boost/mpl/vector.hpp> |
| 15 | #include <boost/mpl/transform.hpp> |
| 16 | #include <boost/mpl/back_inserter.hpp> |
| 17 | #include <boost/mpl/range_c.hpp> |
| 18 | |
| 19 | #define BOOST_TEST_MAIN |
| 20 | #include <boost/test/unit_test.hpp> |
| 21 | |
| 22 | using namespace boost::type_erasure; |
| 23 | namespace mpl = boost::mpl; |
| 24 | |
| 25 | template<int N> |
| 26 | struct tester |
| 27 | { |
| 28 | int value; |
| 29 | }; |
| 30 | |
| 31 | int func() { return 0; } |
| 32 | |
| 33 | template<int N, class... T> |
| 34 | int func(tester<N> t0, T... t) |
| 35 | { |
| 36 | return t0.value + func(t...); |
| 37 | } |
| 38 | |
| 39 | BOOST_TYPE_ERASURE_FREE((has_func), func) |
| 40 | |
| 41 | template<class T = _self> |
| 42 | struct common : mpl::vector< |
| 43 | copy_constructible<T> |
| 44 | > {}; |
| 45 | |
| 46 | BOOST_AUTO_TEST_CASE(test_arity) |
| 47 | { |
| 48 | tester<0> t = { .value: 1 }; |
| 49 | any<mpl::vector<common<>, has_func<int(_self, _self, _self, _self, _self, _self)> > > x(t); |
| 50 | int i = func(t: x, t: x, t: x, t: x, t: x, t: x); |
| 51 | BOOST_TEST(i == 6); |
| 52 | } |
| 53 | |
| 54 | BOOST_AUTO_TEST_CASE(test_null_arity) |
| 55 | { |
| 56 | any<mpl::vector<common<>, has_func<int(_self, _self, _self, _self, _self, _self)>, relaxed> > x; |
| 57 | BOOST_CHECK_THROW(func(x, x, x, x, x , x), boost::type_erasure::bad_function_call); |
| 58 | } |
| 59 | |
| 60 | template<class T0, class... T> |
| 61 | struct my_concept |
| 62 | { |
| 63 | static int apply(T0 t0) { return func(t0); } |
| 64 | }; |
| 65 | |
| 66 | BOOST_AUTO_TEST_CASE(test_template_arity) |
| 67 | { |
| 68 | typedef my_concept<_self, int, int, int, int, int, int> concept1; |
| 69 | tester<0> t = { .value: 1 }; |
| 70 | any<mpl::vector<common<>, concept1> > x(t); |
| 71 | int i = call(f: concept1(), arg&: x); |
| 72 | BOOST_TEST(i == 1); |
| 73 | } |
| 74 | |
| 75 | template<class T> |
| 76 | struct make_funcN |
| 77 | { |
| 78 | typedef has_func<int(_self, tester<T::value>)> type; |
| 79 | }; |
| 80 | |
| 81 | BOOST_AUTO_TEST_CASE(test_vtable_size) |
| 82 | { |
| 83 | tester<0> t = { .value: 1 }; |
| 84 | any<mpl::vector< |
| 85 | common<>, |
| 86 | mpl::transform<mpl::range_c<int, 1, 60>, |
| 87 | make_funcN<mpl::_1>, |
| 88 | mpl::back_inserter< boost::mp11::mp_list<> > |
| 89 | >::type |
| 90 | > > x(t); |
| 91 | tester<7> t1 = { .value: 2 }; |
| 92 | int i = func(t: x, t: t1); |
| 93 | BOOST_TEST(i == 3); |
| 94 | } |
| 95 | |