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/builtin.hpp> |
13 | #include <boost/type_erasure/operators.hpp> |
14 | #include <boost/type_erasure/any_cast.hpp> |
15 | #include <boost/mpl/vector.hpp> |
16 | |
17 | #define BOOST_TEST_MAIN |
18 | #include <boost/test/unit_test.hpp> |
19 | |
20 | using namespace boost::type_erasure; |
21 | |
22 | template<class T = _self> |
23 | struct common : ::boost::mpl::vector< |
24 | copy_constructible<T>, |
25 | typeid_<T> |
26 | > {}; |
27 | |
28 | typedef any<boost::mpl::vector<common<> > > any1_type; |
29 | |
30 | struct test_class |
31 | { |
32 | int i; |
33 | }; |
34 | |
35 | test_class operator+(const test_class& lhs, const any1_type& rhs) |
36 | { |
37 | test_class result = { .i: lhs.i + any_cast<int>(arg: rhs) }; |
38 | return result; |
39 | } |
40 | |
41 | BOOST_AUTO_TEST_CASE(test_basic) |
42 | { |
43 | typedef boost::mpl::vector<common<>, addable<_self, any1_type> > test_concept; |
44 | any1_type a1(1); |
45 | test_class v = { .i: 2 }; |
46 | any<test_concept> x(v); |
47 | any<test_concept> y(x + a1); |
48 | BOOST_CHECK_EQUAL(any_cast<test_class>(y).i, 3); |
49 | } |
50 | |
51 | BOOST_AUTO_TEST_CASE(test_relaxed) |
52 | { |
53 | typedef boost::mpl::vector<common<_a>, addable<_a, any1_type>, relaxed> test_concept; |
54 | typedef boost::mpl::vector<common<_b>, addable<_b, any1_type>, relaxed> dest_concept; |
55 | any1_type a1(1); |
56 | test_class v = { .i: 2 }; |
57 | any<test_concept, _a> x(v); |
58 | any<test_concept, _a> y(x + a1); |
59 | BOOST_CHECK_EQUAL(any_cast<test_class>(y).i, 3); |
60 | |
61 | any<dest_concept, _b> z(x); |
62 | } |
63 | |