]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/smart_ptr/test/shared_from_this_test.cpp
1 #include <boost/config.hpp>
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
10 #if defined(__GNUC__) && __GNUC__ > 4
11 # pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
15 // shared_from_this_test.cpp
17 // Copyright (c) 2002, 2003 Peter Dimov
19 // Distributed under the Boost Software License, Version 1.0. (See
20 // accompanying file LICENSE_1_0.txt or copy at
21 // http://www.boost.org/LICENSE_1_0.txt)
25 #include <boost/enable_shared_from_this.hpp>
26 #include <boost/shared_ptr.hpp>
28 #include <boost/detail/lightweight_test.hpp>
47 virtual boost::shared_ptr
<X
> getX() = 0;
54 boost::shared_ptr
<Y
> createY();
58 boost::shared_ptr
<Y
> py
= createY();
59 BOOST_TEST(py
.get() != 0);
60 BOOST_TEST(py
.use_count() == 1);
64 boost::shared_ptr
<X
> px
= py
->getX();
65 BOOST_TEST(px
.get() != 0);
66 BOOST_TEST(py
.use_count() == 2);
70 #if !defined( BOOST_NO_RTTI )
71 boost::shared_ptr
<Y
> py2
= boost::dynamic_pointer_cast
<Y
>(px
);
72 BOOST_TEST(py
.get() == py2
.get());
73 BOOST_TEST(!(py
< py2
|| py2
< py
));
74 BOOST_TEST(py
.use_count() == 3);
77 catch( boost::bad_weak_ptr
const& )
79 BOOST_ERROR( "py->getX() failed" );
91 return boost::report_errors();
94 // virtual inheritance to stress the implementation
95 // (prevents Y* -> impl*, enable_shared_from_this<impl>* -> impl* casts)
97 class impl
: public X
, public virtual Y
, public virtual boost::enable_shared_from_this
<impl
>
105 virtual boost::shared_ptr
<X
> getX()
107 boost::shared_ptr
<impl
> pi
= shared_from_this();
108 BOOST_TEST(pi
.get() == this);
113 // intermediate impl2 to stress the implementation
115 class impl2
: public impl
119 boost::shared_ptr
<Y
> createY()
121 boost::shared_ptr
<Y
> pi(new impl2
);
127 boost::shared_ptr
<Y
> pi(static_cast<impl2
*>(0));
132 struct V
: public boost::enable_shared_from_this
<V
>
138 boost::shared_ptr
<V
> p(new V
);
142 boost::shared_ptr
<V
> q
= p
->shared_from_this();
144 BOOST_TEST(!(p
< q
) && !(q
< p
));
146 catch( boost::bad_weak_ptr
const & )
148 BOOST_ERROR( "p->shared_from_this() failed" );
155 boost::shared_ptr
<V
> r
= v2
.shared_from_this();
156 BOOST_ERROR("v2.shared_from_this() failed to throw");
158 catch( boost::bad_weak_ptr
const & )
165 boost::shared_ptr
<V
> r
= p
->shared_from_this();
167 BOOST_TEST(!(p
< r
) && !(r
< p
));
169 catch( boost::bad_weak_ptr
const & )
171 BOOST_ERROR("p->shared_from_this() threw bad_weak_ptr after *p = V()");