| 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/tuple.hpp> |
| 13 | #include <boost/type_erasure/builtin.hpp> |
| 14 | #include <boost/type_erasure/operators.hpp> |
| 15 | #include <boost/type_erasure/any_cast.hpp> |
| 16 | #include <boost/mpl/vector.hpp> |
| 17 | |
| 18 | #define BOOST_TEST_MAIN |
| 19 | #include <boost/test/unit_test.hpp> |
| 20 | |
| 21 | using namespace boost::type_erasure; |
| 22 | |
| 23 | class no_destroy |
| 24 | { |
| 25 | protected: |
| 26 | ~no_destroy() {} |
| 27 | }; |
| 28 | |
| 29 | class with_destroy : public no_destroy |
| 30 | { |
| 31 | public: |
| 32 | ~with_destroy() {} |
| 33 | }; |
| 34 | |
| 35 | template<class T = _self> |
| 36 | struct common : ::boost::mpl::vector< |
| 37 | destructible<T>, |
| 38 | copy_constructible<T>, |
| 39 | typeid_<T> |
| 40 | > {}; |
| 41 | |
| 42 | BOOST_AUTO_TEST_CASE(test_basic) |
| 43 | { |
| 44 | typedef ::boost::mpl::vector<typeid_<> > test_concept; |
| 45 | with_destroy val; |
| 46 | any<test_concept, _self&> x(static_cast<no_destroy&>(val)); |
| 47 | no_destroy& ref = any_cast<no_destroy&>(arg&: x); |
| 48 | BOOST_CHECK_EQUAL(&ref, &val); |
| 49 | } |
| 50 | |
| 51 | BOOST_AUTO_TEST_CASE(test_increment) |
| 52 | { |
| 53 | typedef ::boost::mpl::vector<incrementable<> > test_concept; |
| 54 | int i = 0; |
| 55 | any<test_concept, _self&> x(i); |
| 56 | ++x; |
| 57 | BOOST_CHECK_EQUAL(i, 1); |
| 58 | } |
| 59 | |
| 60 | BOOST_AUTO_TEST_CASE(test_add) |
| 61 | { |
| 62 | typedef ::boost::mpl::vector<common<>, addable<> > test_concept; |
| 63 | int i = 1; |
| 64 | int j = 2; |
| 65 | any<test_concept, _self&> x(i); |
| 66 | any<test_concept, _self&> y(j); |
| 67 | any<test_concept, _self> z(x + y); |
| 68 | int k = any_cast<int>(arg&: z); |
| 69 | BOOST_CHECK_EQUAL(k, 3); |
| 70 | } |
| 71 | |
| 72 | BOOST_AUTO_TEST_CASE(test_mixed_add) |
| 73 | { |
| 74 | typedef ::boost::mpl::vector<common<>, addable<> > test_concept; |
| 75 | int i = 1; |
| 76 | int j = 2; |
| 77 | any<test_concept, _self&> x(i); |
| 78 | any<test_concept, _self> y(j); |
| 79 | any<test_concept, _self> z(x + y); |
| 80 | int k = any_cast<int>(arg&: z); |
| 81 | BOOST_CHECK_EQUAL(k, 3); |
| 82 | } |
| 83 | |