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