pointer_cast_test.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // pointer_cast_test.cpp - a test for boost/pointer_cast.hpp
  3. //
  4. // Copyright (c) 2005 Ion Gaztanaga
  5. // Copyright (c) 2005 Peter Dimov
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See
  8. // accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #include <boost/config.hpp>
  12. #include <boost/pointer_cast.hpp>
  13. #include <boost/shared_ptr.hpp>
  14. #include <boost/scoped_ptr.hpp>
  15. #include <boost/get_pointer.hpp>
  16. #include <boost/detail/lightweight_test.hpp>
  17. namespace
  18. {
  19. // Let's create these inheritance relationship:
  20. //
  21. // base base2
  22. // | |
  23. // derived
  24. // |
  25. // derived_derived
  26. //
  27. class base
  28. {
  29. public:
  30. virtual ~base(){}
  31. int filler [5];
  32. };
  33. class base2
  34. {
  35. public:
  36. virtual ~base2(){}
  37. int filler [5];
  38. };
  39. class derived
  40. : public base, public base2
  41. {
  42. int filler [5];
  43. };
  44. class derived_derived
  45. : public derived
  46. {
  47. int filler [5];
  48. };
  49. // And now some simple check functions
  50. #if !defined( BOOST_NO_RTTI )
  51. template <class BasePtr>
  52. bool check_dynamic_pointer_cast(const BasePtr &ptr)
  53. {
  54. //Check that dynamic_pointer_cast versus dynamic_cast
  55. return
  56. //Correct cast with dynamic_pointer_cast
  57. boost::get_pointer(boost::dynamic_pointer_cast<derived>(ptr)) ==
  58. //Correct cast with dynamic_cast
  59. dynamic_cast<derived*>(boost::get_pointer(ptr))
  60. &&
  61. //Incorrect cast with dynamic_pointer_cast
  62. boost::get_pointer(boost::dynamic_pointer_cast<derived_derived>(ptr)) ==
  63. //Incorrect cast with dynamic_cast
  64. dynamic_cast<derived_derived*>(boost::get_pointer(ptr));
  65. }
  66. #endif
  67. template <class BasePtr>
  68. bool check_static_pointer_cast(const BasePtr &ptr)
  69. {
  70. return
  71. //Cast base -> derived -> base2 using static_pointer_cast
  72. boost::get_pointer(
  73. boost::static_pointer_cast<base2>(
  74. boost::static_pointer_cast<derived>(ptr))) ==
  75. //Now the same with static_cast
  76. static_cast<base2*>(static_cast<derived*>(boost::get_pointer(ptr)));
  77. }
  78. template <class BasePtr>
  79. bool check_const_pointer_cast(const BasePtr &ptr)
  80. {
  81. return
  82. //Unconst and const again using const_pointer_cast
  83. boost::get_pointer(
  84. boost::const_pointer_cast<const base>
  85. (boost::const_pointer_cast<base>(ptr))) ==
  86. //Now the same with const_cast
  87. const_cast<const base*>(const_cast<base*>(boost::get_pointer(ptr)));
  88. }
  89. template <class BasePtr>
  90. void check_all_casts(const BasePtr &ptr)
  91. {
  92. #if !defined( BOOST_NO_RTTI )
  93. BOOST_TEST( check_dynamic_pointer_cast( ptr ) );
  94. #endif
  95. BOOST_TEST( check_static_pointer_cast( ptr ) );
  96. BOOST_TEST( check_const_pointer_cast( ptr ) );
  97. }
  98. }
  99. int main()
  100. {
  101. boost::shared_ptr<base> boost_shared(new derived);
  102. base *plain = boost_shared.get();
  103. check_all_casts(boost_shared);
  104. check_all_casts(plain);
  105. return boost::report_errors();
  106. }