test_dereference.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. BOOST_AUTO_TEST_CASE(test_basic)
  24. {
  25. typedef ::boost::mpl::vector<common<>, dereferenceable<int&> > test_concept;
  26. int i;
  27. any<test_concept> x(&i);
  28. BOOST_CHECK_EQUAL(&*x, &i);
  29. }
  30. BOOST_AUTO_TEST_CASE(test_any_result)
  31. {
  32. typedef ::boost::mpl::vector<common<>, common<_a>, dereferenceable<_a&> > test_concept;
  33. typedef ::boost::mpl::map<
  34. ::boost::mpl::pair<_self, int*>,
  35. ::boost::mpl::pair<_a, int>
  36. > types;
  37. int i;
  38. any<test_concept> x(&i, make_binding<types>());
  39. any<test_concept, _a&> y(*x);
  40. BOOST_CHECK_EQUAL(any_cast<int*>(&y), &i);
  41. }
  42. BOOST_AUTO_TEST_CASE(test_any_result_const)
  43. {
  44. typedef ::boost::mpl::vector<common<>, common<_a>, dereferenceable<const _a&> > test_concept;
  45. typedef ::boost::mpl::map<
  46. ::boost::mpl::pair<_self, const int*>,
  47. ::boost::mpl::pair<_a, int>
  48. > types;
  49. const int i = 0;
  50. any<test_concept> x(&i, make_binding<types>());
  51. any<test_concept, const _a&> y(*x);
  52. BOOST_CHECK_EQUAL(any_cast<const int*>(&y), &i);
  53. }