type_erased_abstract.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Boost.Range library
  2. //
  3. // Copyright Neil Groves 2014. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. #include <boost/range/adaptor/type_erased.hpp>
  9. #include "type_erased_test.hpp"
  10. #include <boost/test/unit_test.hpp>
  11. #include <vector>
  12. namespace boost_range_adaptor_type_erased_test
  13. {
  14. namespace
  15. {
  16. class dummy_interface
  17. {
  18. public:
  19. virtual ~dummy_interface() { }
  20. virtual void test() = 0;
  21. protected:
  22. dummy_interface() { }
  23. private:
  24. dummy_interface(const dummy_interface&);
  25. void operator=(const dummy_interface&);
  26. };
  27. class dummy_impl
  28. : public dummy_interface
  29. {
  30. public:
  31. dummy_impl() { }
  32. dummy_impl(const dummy_impl&) { }
  33. dummy_impl& operator=(const dummy_impl&) { return *this; }
  34. virtual void test() { }
  35. };
  36. typedef boost::any_range<
  37. dummy_interface,
  38. boost::random_access_traversal_tag,
  39. dummy_interface&,
  40. std::ptrdiff_t
  41. > any_interface_range;
  42. struct foo_dummy_interface_fn
  43. {
  44. void operator()(dummy_interface& iface)
  45. {
  46. iface.test();
  47. }
  48. };
  49. void foo_test_dummy_interface_range(any_interface_range rng)
  50. {
  51. std::for_each(boost::begin(rng), boost::end(rng),
  52. foo_dummy_interface_fn());
  53. }
  54. void test_type_erased_abstract()
  55. {
  56. std::vector<dummy_impl> v(10);
  57. any_interface_range r(v);
  58. foo_test_dummy_interface_range(r);
  59. foo_test_dummy_interface_range(any_interface_range(v));
  60. }
  61. } // anonymous namespace
  62. } // namespace boost_range_adaptor_type_erased_test
  63. boost::unit_test::test_suite*
  64. init_unit_test_suite(int, char*[])
  65. {
  66. boost::unit_test::test_suite* test
  67. = BOOST_TEST_SUITE("RangeTestSuite.adaptor.type_erased_abstract");
  68. test->add(
  69. BOOST_TEST_CASE(
  70. &boost_range_adaptor_type_erased_test::test_type_erased_abstract));
  71. return test;
  72. }