shared_from_this_test.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include <boost/config.hpp>
  2. #if defined(BOOST_MSVC)
  3. #pragma warning(disable: 4786) // identifier truncated in debug info
  4. #pragma warning(disable: 4710) // function not inlined
  5. #pragma warning(disable: 4711) // function selected for automatic inline expansion
  6. #pragma warning(disable: 4514) // unreferenced inline removed
  7. #endif
  8. #if defined(__GNUC__) && __GNUC__ > 4
  9. # pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
  10. #endif
  11. //
  12. // shared_from_this_test.cpp
  13. //
  14. // Copyright (c) 2002, 2003 Peter Dimov
  15. //
  16. // Distributed under the Boost Software License, Version 1.0. (See
  17. // accompanying file LICENSE_1_0.txt or copy at
  18. // http://www.boost.org/LICENSE_1_0.txt)
  19. //
  20. #include <boost/enable_shared_from_this.hpp>
  21. #include <boost/shared_ptr.hpp>
  22. #include <boost/detail/lightweight_test.hpp>
  23. //
  24. class X
  25. {
  26. public:
  27. virtual void f() = 0;
  28. protected:
  29. ~X() {}
  30. };
  31. class Y
  32. {
  33. public:
  34. virtual boost::shared_ptr<X> getX() = 0;
  35. protected:
  36. ~Y() {}
  37. };
  38. boost::shared_ptr<Y> createY();
  39. void test()
  40. {
  41. boost::shared_ptr<Y> py = createY();
  42. BOOST_TEST(py.get() != 0);
  43. BOOST_TEST(py.use_count() == 1);
  44. try
  45. {
  46. boost::shared_ptr<X> px = py->getX();
  47. BOOST_TEST(px.get() != 0);
  48. BOOST_TEST(py.use_count() == 2);
  49. px->f();
  50. #if !defined( BOOST_NO_RTTI )
  51. boost::shared_ptr<Y> py2 = boost::dynamic_pointer_cast<Y>(px);
  52. BOOST_TEST(py.get() == py2.get());
  53. BOOST_TEST(!(py < py2 || py2 < py));
  54. BOOST_TEST(py.use_count() == 3);
  55. #endif
  56. }
  57. catch( boost::bad_weak_ptr const& )
  58. {
  59. BOOST_ERROR( "py->getX() failed" );
  60. }
  61. }
  62. void test2();
  63. void test3();
  64. int main()
  65. {
  66. test();
  67. test2();
  68. test3();
  69. return boost::report_errors();
  70. }
  71. // virtual inheritance to stress the implementation
  72. // (prevents Y* -> impl*, enable_shared_from_this<impl>* -> impl* casts)
  73. class impl: public X, public virtual Y, public virtual boost::enable_shared_from_this<impl>
  74. {
  75. public:
  76. virtual void f()
  77. {
  78. }
  79. virtual boost::shared_ptr<X> getX()
  80. {
  81. boost::shared_ptr<impl> pi = shared_from_this();
  82. BOOST_TEST(pi.get() == this);
  83. return pi;
  84. }
  85. };
  86. // intermediate impl2 to stress the implementation
  87. class impl2: public impl
  88. {
  89. };
  90. boost::shared_ptr<Y> createY()
  91. {
  92. boost::shared_ptr<Y> pi(new impl2);
  93. return pi;
  94. }
  95. void test2()
  96. {
  97. boost::shared_ptr<Y> pi(static_cast<impl2*>(0));
  98. }
  99. //
  100. struct V: public boost::enable_shared_from_this<V>
  101. {
  102. };
  103. void test3()
  104. {
  105. boost::shared_ptr<V> p(new V);
  106. try
  107. {
  108. boost::shared_ptr<V> q = p->shared_from_this();
  109. BOOST_TEST(p == q);
  110. BOOST_TEST(!(p < q) && !(q < p));
  111. }
  112. catch( boost::bad_weak_ptr const & )
  113. {
  114. BOOST_ERROR( "p->shared_from_this() failed" );
  115. }
  116. V v2(*p);
  117. try
  118. {
  119. boost::shared_ptr<V> r = v2.shared_from_this();
  120. BOOST_ERROR("v2.shared_from_this() failed to throw");
  121. }
  122. catch( boost::bad_weak_ptr const & )
  123. {
  124. }
  125. try
  126. {
  127. *p = V();
  128. boost::shared_ptr<V> r = p->shared_from_this();
  129. BOOST_TEST(p == r);
  130. BOOST_TEST(!(p < r) && !(r < p));
  131. }
  132. catch( boost::bad_weak_ptr const & )
  133. {
  134. BOOST_ERROR("p->shared_from_this() threw bad_weak_ptr after *p = V()");
  135. }
  136. }