forked from boostorg/smart_ptr
Added weak_from_raw(), for use in conjunction with
enable_shared_from_raw base class. [SVN r57423]
This commit is contained in:
@ -21,6 +21,7 @@
|
|||||||
namespace boost
|
namespace boost
|
||||||
{
|
{
|
||||||
template<typename T> boost::shared_ptr<T> shared_from_raw(T *);
|
template<typename T> boost::shared_ptr<T> shared_from_raw(T *);
|
||||||
|
template<typename T> boost::weak_ptr<T> weak_from_raw(T *);
|
||||||
|
|
||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
@ -72,6 +73,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
template<class Y> friend class shared_ptr;
|
template<class Y> friend class shared_ptr;
|
||||||
template<typename T> friend boost::shared_ptr<T> shared_from_raw(T *);
|
template<typename T> friend boost::shared_ptr<T> shared_from_raw(T *);
|
||||||
|
template<typename T> friend boost::weak_ptr<T> weak_from_raw(T *);
|
||||||
template< class X, class Y > friend inline void detail::sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe );
|
template< class X, class Y > friend inline void detail::sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -114,9 +116,19 @@ private:
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
boost::shared_ptr<T> shared_from_raw(T *p)
|
boost::shared_ptr<T> shared_from_raw(T *p)
|
||||||
{
|
{
|
||||||
|
BOOST_ASSERT(p != 0);
|
||||||
return boost::shared_ptr<T>(p->enable_shared_from_raw::shared_from_this(), p);
|
return boost::shared_ptr<T>(p->enable_shared_from_raw::shared_from_this(), p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
boost::weak_ptr<T> weak_from_raw(T *p)
|
||||||
|
{
|
||||||
|
BOOST_ASSERT(p != 0);
|
||||||
|
boost::weak_ptr<T> result;
|
||||||
|
result._internal_aliasing_assign(p->enable_shared_from_raw::weak_this_, p);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
template< class X, class Y > inline void sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe )
|
template< class X, class Y > inline void sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe )
|
||||||
|
@ -183,10 +183,11 @@ public:
|
|||||||
pn.swap(other.pn);
|
pn.swap(other.pn);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _internal_assign(T * px2, boost::detail::shared_count const & pn2)
|
template<typename Y>
|
||||||
|
void _internal_aliasing_assign(weak_ptr<Y> const & r, T * px2)
|
||||||
{
|
{
|
||||||
px = px2;
|
px = px2;
|
||||||
pn = pn2;
|
pn = r.pn;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class Y> bool _internal_less(weak_ptr<Y> const & rhs) const
|
template<class Y> bool _internal_less(weak_ptr<Y> const & rhs) const
|
||||||
@ -225,6 +226,6 @@ template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)
|
|||||||
|
|
||||||
#ifdef BOOST_MSVC
|
#ifdef BOOST_MSVC
|
||||||
# pragma warning(pop)
|
# pragma warning(pop)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
|
#endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
|
||||||
|
@ -59,6 +59,7 @@ import testing ;
|
|||||||
[ run sp_recursive_assign_rv_test.cpp ]
|
[ run sp_recursive_assign_rv_test.cpp ]
|
||||||
[ run sp_recursive_assign2_rv_test.cpp ]
|
[ run sp_recursive_assign2_rv_test.cpp ]
|
||||||
[ run esft_constructor_test.cpp ]
|
[ run esft_constructor_test.cpp ]
|
||||||
|
[ run enable_shared_from_raw_test.cpp ]
|
||||||
[ compile-fail auto_ptr_lv_fail.cpp ]
|
[ compile-fail auto_ptr_lv_fail.cpp ]
|
||||||
[ run atomic_count_test2.cpp ]
|
[ run atomic_count_test2.cpp ]
|
||||||
;
|
;
|
||||||
|
39
test/enable_shared_from_raw_test.cpp
Normal file
39
test/enable_shared_from_raw_test.cpp
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#include <boost/config.hpp>
|
||||||
|
|
||||||
|
//
|
||||||
|
// weak_from_raw_test.cpp
|
||||||
|
//
|
||||||
|
// Copyright (c) 2009 Frank Mori Hess
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See
|
||||||
|
// accompanying file LICENSE_1_0.txt or copy at
|
||||||
|
// http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
|
||||||
|
#include <boost/smart_ptr/enable_shared_from_raw.hpp>
|
||||||
|
|
||||||
|
#include <boost/detail/lightweight_test.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
struct X: public boost::enable_shared_from_raw
|
||||||
|
{};
|
||||||
|
|
||||||
|
void basic_weak_from_raw_test()
|
||||||
|
{
|
||||||
|
X *p(new X);
|
||||||
|
boost::weak_ptr<X> weak = boost::weak_from_raw(p);
|
||||||
|
BOOST_TEST(weak.expired());
|
||||||
|
boost::shared_ptr<X> shared(p);
|
||||||
|
weak = boost::weak_from_raw(p);
|
||||||
|
BOOST_TEST(weak.expired() == false);
|
||||||
|
boost::shared_ptr<X> shared2(weak);
|
||||||
|
BOOST_TEST((shared < shared2 || shared2 < shared) == false);
|
||||||
|
BOOST_TEST(shared.get() == p);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
basic_weak_from_raw_test();
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
Reference in New Issue
Block a user