test_nested.cpp 1.6 KB

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