mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-07-31 21:24:40 +02:00
Merge branch 'feature/unique_ptr_casts' into develop
This commit is contained in:
@@ -10,6 +10,8 @@
|
||||
#ifndef BOOST_POINTER_CAST_HPP
|
||||
#define BOOST_POINTER_CAST_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
namespace boost {
|
||||
|
||||
//static_pointer_cast overload for raw pointers
|
||||
@@ -42,4 +44,78 @@ inline T* reinterpret_pointer_cast(U *ptr)
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#if !defined( BOOST_NO_CXX11_SMART_PTR )
|
||||
|
||||
#include <boost/type_traits/has_virtual_destructor.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace boost {
|
||||
|
||||
//static_pointer_cast overload for std::shared_ptr
|
||||
using std::static_pointer_cast;
|
||||
|
||||
//dynamic_pointer_cast overload for std::shared_ptr
|
||||
using std::dynamic_pointer_cast;
|
||||
|
||||
//const_pointer_cast overload for std::shared_ptr
|
||||
using std::const_pointer_cast;
|
||||
|
||||
//reinterpret_pointer_cast overload for std::shared_ptr
|
||||
template<class T, class U> std::shared_ptr<T> reinterpret_pointer_cast(const std::shared_ptr<U> & r ) BOOST_NOEXCEPT
|
||||
{
|
||||
(void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
|
||||
|
||||
typedef typename std::shared_ptr<T>::element_type E;
|
||||
|
||||
E * p = reinterpret_cast< E* >( r.get() );
|
||||
return std::shared_ptr<T>( r, p );
|
||||
}
|
||||
|
||||
//static_pointer_cast overload for std::unique_ptr
|
||||
template<class T, class U> std::unique_ptr<T> static_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
|
||||
{
|
||||
(void) static_cast< T* >( static_cast< U* >( 0 ) );
|
||||
|
||||
typedef typename std::unique_ptr<T>::element_type E;
|
||||
|
||||
return std::unique_ptr<T>( static_cast<E*>( r.release() ) );
|
||||
}
|
||||
|
||||
//dynamic_pointer_cast overload for std::unique_ptr
|
||||
template<class T, class U> std::unique_ptr<T> dynamic_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
|
||||
{
|
||||
(void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
|
||||
|
||||
BOOST_STATIC_ASSERT_MSG( boost::has_virtual_destructor<T>::value, "The target of dynamic_pointer_cast must have a virtual destructor." );
|
||||
|
||||
T * p = dynamic_cast<T*>( r.get() );
|
||||
if( p ) r.release();
|
||||
return std::unique_ptr<T>( p );
|
||||
}
|
||||
|
||||
//const_pointer_cast overload for std::unique_ptr
|
||||
template<class T, class U> std::unique_ptr<T> const_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
|
||||
{
|
||||
(void) const_cast< T* >( static_cast< U* >( 0 ) );
|
||||
|
||||
typedef typename std::unique_ptr<T>::element_type E;
|
||||
|
||||
return std::unique_ptr<T>( const_cast<E*>( r.release() ) );
|
||||
}
|
||||
|
||||
//reinterpret_pointer_cast overload for std::unique_ptr
|
||||
template<class T, class U> std::unique_ptr<T> reinterpret_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
|
||||
{
|
||||
(void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
|
||||
|
||||
typedef typename std::unique_ptr<T>::element_type E;
|
||||
|
||||
return std::unique_ptr<T>( reinterpret_cast<E*>( r.release() ) );
|
||||
}
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // #if !defined( BOOST_NO_CXX11_SMART_PTR )
|
||||
|
||||
#endif //BOOST_POINTER_CAST_HPP
|
||||
|
@@ -2,16 +2,16 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>pointer_cast</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#ffffff" link="#0000ff" vlink="#0000ff">
|
||||
<h1><img height="86" alt="boost.png (6897 bytes)" src="../../boost.png"
|
||||
width="277" align="middle" border="0">pointer_cast</h1>
|
||||
width="277" align="middle" border="0" />pointer_cast</h1>
|
||||
<p>The pointer cast functions (<code>boost::static_pointer_cast</code> <code>boost::dynamic_pointer_cast</code>
|
||||
<code>boost::reinterpret_pointer_cast</code> <code>boost::const_pointer_cast</code>)
|
||||
provide a way to write generic pointer castings for raw pointers. The functions
|
||||
are defined in <CITE><A href="../../boost/pointer_cast.hpp">boost/pointer_cast.hpp</A>.</CITE></p>
|
||||
<P>There is test/example code in <CITE><A href="test/pointer_cast_test.cpp">pointer_cast_test.cpp</A></CITE>.</p>
|
||||
provide a way to write generic pointer castings for raw pointers, <code>std::shared_ptr</code> and <code>std::unique_ptr</code>. The functions
|
||||
are defined in <cite><a href="../../boost/pointer_cast.hpp">boost/pointer_cast.hpp</a>.</cite></p>
|
||||
<p>There is test/example code in <cite><a href="test/pointer_cast_test.cpp">pointer_cast_test.cpp</a></cite>.</p>
|
||||
<h2><a name="rationale">Rationale</a></h2>
|
||||
<P>Boost smart pointers usually overload those functions to provide a mechanism to
|
||||
emulate pointers casts. For example, <code>boost::shared_ptr<...></code> implements
|
||||
@@ -20,15 +20,15 @@
|
||||
template<class T, class U>
|
||||
shared_ptr<T> static_pointer_cast(shared_ptr<U> const &r);
|
||||
</pre>
|
||||
<P>Pointer cast functions from <CITE><A href="../../boost/pointer_cast.hpp">boost/pointer_cast.hpp</A></CITE>
|
||||
<p>Pointer cast functions from <cite><A href="../../boost/pointer_cast.hpp">boost/pointer_cast.hpp</A></CITE>
|
||||
are overloads of <code>boost::static_pointer_cast</code>, <code>boost::dynamic_pointer_cast</code>,
|
||||
<code>boost::reinterpret_pointer_cast</code> and <code>boost::const_pointer_cast</code>
|
||||
for raw pointers. This way when developing pointer type independent classes,
|
||||
for example, memory managers or shared memory compatible classes, the same code
|
||||
can be used for raw and smart pointers.</p>
|
||||
<H2><A name="synopsis">Synopsis</A></H2>
|
||||
<BLOCKQUOTE>
|
||||
<PRE>
|
||||
for raw pointers, <code>std::shared_ptr</code> and <code>std::unique_ptr</code>. This way when developing
|
||||
pointer type independent classes, for example, memory managers or shared memory compatible classes, the same
|
||||
code can be used for raw and smart pointers.</p>
|
||||
<h2><a name="synopsis">Synopsis</a></h2>
|
||||
<blockquote>
|
||||
<pre>
|
||||
namespace boost {
|
||||
|
||||
template<class T, class U>
|
||||
@@ -46,15 +46,93 @@ inline T* const_pointer_cast(U *ptr)
|
||||
template<class T, class U>
|
||||
inline T* reinterpret_pointer_cast(U *ptr)
|
||||
{ return reinterpret_cast<T*>(ptr); }
|
||||
|
||||
template<class T, class U>
|
||||
inline std::shared_ptr<T> static_pointer_cast(std::shared_ptr<U> const& r);
|
||||
|
||||
template<class T, class U>
|
||||
inline std::shared_ptr<T> dynamic_pointer_cast(std::shared_ptr<U> const& r);
|
||||
|
||||
template<class T, class U>
|
||||
inline std::shared_ptr<T> const_pointer_cast(std::shared_ptr<U> const& r);
|
||||
|
||||
template<class T, class U>
|
||||
inline std::shared_ptr<T> reinterpret_pointer_cast(std::shared_ptr<U> const& r);
|
||||
|
||||
template<class T, class U>
|
||||
inline std::unique_ptr<T> static_pointer_cast(std::unique_ptr<U>&& r);
|
||||
|
||||
template<class T, class U>
|
||||
inline std::unique_ptr<T> dynamic_pointer_cast(std::unique_ptr<U>&& r);
|
||||
|
||||
template<class T, class U>
|
||||
inline std::unique_ptr<T> const_pointer_cast(std::unique_ptr<U>&& r);
|
||||
|
||||
template<class T, class U>
|
||||
inline std::unique_ptr<T> reinterpret_pointer_cast(std::unique_ptr<U>&& r);
|
||||
|
||||
} // namespace boost
|
||||
</PRE>
|
||||
</BLOCKQUOTE>
|
||||
<P>As you can see from the above synopsis, the pointer cast functions are just
|
||||
wrappers around standard C++ cast operators.</P>
|
||||
<H2><A name="example">Example</A></H2>
|
||||
<BLOCKQUOTE>
|
||||
<PRE>
|
||||
</pre>
|
||||
</blockquote>
|
||||
<p>As you can see from the above synopsis, the pointer cast functions for raw pointers are just
|
||||
wrappers around standard C++ cast operators.</p>
|
||||
|
||||
<p>The pointer casts for <code>std::shared_ptr</code> are aliases of the corresponding standard
|
||||
functions with the same names and equivalent to <a href="shared_ptr.htm#static_pointer_cast">the
|
||||
functions taking <code>boost::shared_ptr</code></a>.</p>
|
||||
|
||||
<p>The pointer casts for <code>std::unique_ptr</code> are documented below.</p>
|
||||
|
||||
<h3 id="static_pointer_cast">static_pointer_cast</h3>
|
||||
<pre>template<class T, class U>
|
||||
unique_ptr<T> static_pointer_cast(unique_ptr<U>&& r); // never throws</pre>
|
||||
<blockquote>
|
||||
<p><b>Requires:</b> The expression <code>static_cast<T*>( (U*)0 )</code>
|
||||
must be well-formed.</p>
|
||||
<p><b>Returns:</b> <code>unique_ptr<T>( static_cast<typename unique_ptr<T>::element_type*>(r.release()) )</code>.</p>
|
||||
<p><b>Throws:</b> nothing.</p>
|
||||
<p><b>Notes:</b> the seemingly equivalent expression
|
||||
<code>unique_ptr<T>(static_cast<T*>(r.get()))</code>
|
||||
will eventually result in undefined behavior, attempting to delete the same
|
||||
object twice.</p>
|
||||
</blockquote>
|
||||
<h3 id="const_pointer_cast">const_pointer_cast</h3>
|
||||
<pre>template<class T, class U>
|
||||
unique_ptr<T> const_pointer_cast(unique_ptr<U>&& r); // never throws</pre>
|
||||
<blockquote>
|
||||
<p><b>Requires:</b> The expression <code>const_cast<T*>( (U*)0 )</code>
|
||||
must be well-formed.</p>
|
||||
<p><b>Returns:</b> <code>unique_ptr<T>( const_cast<typename unique_ptr<T>::element_type*>(r.release()) )</code>.</p>
|
||||
<p><b>Throws:</b> nothing.</p>
|
||||
</blockquote>
|
||||
<h3 id="dynamic_pointer_cast">dynamic_pointer_cast</h3>
|
||||
<pre>template<class T, class U>
|
||||
unique_ptr<T> dynamic_pointer_cast(unique_ptr<U>&& r);</pre>
|
||||
<blockquote>
|
||||
<p><b>Requires:</b> The expression <code>dynamic_cast<T*>( (U*)0 )</code>
|
||||
must be well-formed. <code>T</code> must have a virtual destructor.</p>
|
||||
<p><b>Returns:</b></p>
|
||||
<ul>
|
||||
<li>
|
||||
When <code>dynamic_cast<typename unique_ptr<T>::element_type*>(r.get())</code> returns a nonzero value,
|
||||
<code>unique_ptr<T>(dynamic_cast<typename unique_ptr<T>::element_type*>(r.release()))</code>;</li>
|
||||
<li>
|
||||
Otherwise, <code>unique_ptr<T>()</code>.</li></ul>
|
||||
<p><b>Throws:</b> nothing.</p>
|
||||
</blockquote>
|
||||
<h3 id="reinterpret_pointer_cast">reinterpret_pointer_cast</h3>
|
||||
<pre>template<class T, class U>
|
||||
unique_ptr<T> reinterpret_pointer_cast(unique_ptr<U>&& r); // never throws</pre>
|
||||
<blockquote>
|
||||
<p><b>Requires:</b> The expression <code>reinterpret_cast<T*>( (U*)0 )</code>
|
||||
must be well-formed.</p>
|
||||
<p><b>Returns:</b> <code>unique_ptr<T>( reinterpret_cast<typename unique_ptr<T>::element_type*>(r.release()) )</code>.</p>
|
||||
<p><b>Throws:</b> nothing.</p>
|
||||
</blockquote>
|
||||
|
||||
<h2><a name="example">Example</a></h2>
|
||||
<blockquote>
|
||||
<pre>
|
||||
#include <boost/pointer_cast.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
@@ -79,28 +157,27 @@ void check_if_it_is_derived(const BasePtr &ptr)
|
||||
|
||||
int main()
|
||||
{
|
||||
<I>// Create a raw and a shared_ptr</I>
|
||||
<em>// Create a raw and a shared_ptr</em>
|
||||
|
||||
base *ptr = new derived;
|
||||
boost::shared_ptr<base> sptr(new derived);
|
||||
|
||||
<I>// Check that base pointer points actually to derived class</I>
|
||||
<em>// Check that base pointer points actually to derived class</em>
|
||||
|
||||
check_if_it_is_derived(ptr);
|
||||
check_if_it_is_derived(sptr);
|
||||
|
||||
// <EM>Ok!</EM>
|
||||
<em>// Ok!</em>
|
||||
|
||||
delete ptr;
|
||||
return 0;
|
||||
}</PRE>
|
||||
</BLOCKQUOTE>
|
||||
<P>The example demonstrates how the generic pointer casts help us create pointer
|
||||
independent code.</P>
|
||||
<hr>
|
||||
<p>$Date$</p>
|
||||
}</pre>
|
||||
</blockquote>
|
||||
<p>The example demonstrates how the generic pointer casts help us create pointer
|
||||
independent code.</p>
|
||||
<hr />
|
||||
<p>Copyright 2005 Ion Gazta<74>aga. Use, modification, and distribution are subject to
|
||||
the Boost Software License, Version 1.0. (See accompanying file <A href="../../LICENSE_1_0.txt">
|
||||
LICENSE_1_0.txt</A> or a copy at <<A href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>>.)</p>
|
||||
the Boost Software License, Version 1.0. (See accompanying file <a href="../../LICENSE_1_0.txt">
|
||||
LICENSE_1_0.txt</a> or a copy at <<a href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>>.)</p>
|
||||
</body>
|
||||
</html>
|
||||
|
@@ -222,7 +222,7 @@ void bad()
|
||||
shared_ptr<T> <a href="#dynamic_pointer_cast" >dynamic_pointer_cast</a>(shared_ptr<U> const & r); // never throws
|
||||
|
||||
template<class T, class U>
|
||||
shared_ptr<T> <a href="#reinterpret_pointer_cast" >reinterpet_pointer_cast</a>(shared_ptr<U> const & r); // never throws
|
||||
shared_ptr<T> <a href="#reinterpret_pointer_cast" >reinterpret_pointer_cast</a>(shared_ptr<U> const & r); // never throws
|
||||
|
||||
template<class E, class T, class Y>
|
||||
std::basic_ostream<E, T> & <a href="#insertion-operator" >operator<<</a> (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p);
|
||||
|
@@ -29,6 +29,7 @@ import testing ;
|
||||
[ compile-fail shared_ptr_compare_fail.cpp ]
|
||||
[ run shared_ptr_alloc2_test.cpp ]
|
||||
[ run pointer_cast_test.cpp ]
|
||||
[ run cpp11_pointer_cast_test.cpp ]
|
||||
[ compile pointer_to_other_test.cpp ]
|
||||
[ run auto_ptr_rv_test.cpp ]
|
||||
[ run shared_ptr_alias_test.cpp ]
|
||||
@@ -186,5 +187,19 @@ import testing ;
|
||||
|
||||
[ run sp_hash_test2.cpp ]
|
||||
[ run sp_hash_test3.cpp ]
|
||||
|
||||
[ run pointer_cast_test2.cpp ]
|
||||
|
||||
[ compile-fail pointer_cast_st_fail.cpp ]
|
||||
[ compile-fail pointer_cast_st_fail2.cpp ]
|
||||
[ compile-fail pointer_cast_st_fail3.cpp ]
|
||||
|
||||
[ compile-fail pointer_cast_co_fail.cpp ]
|
||||
[ compile-fail pointer_cast_co_fail2.cpp ]
|
||||
[ compile-fail pointer_cast_co_fail3.cpp ]
|
||||
|
||||
[ compile-fail pointer_cast_dy_fail.cpp ]
|
||||
[ compile-fail pointer_cast_dy_fail2.cpp ]
|
||||
[ compile-fail pointer_cast_dy_fail3.cpp ]
|
||||
;
|
||||
}
|
||||
|
224
test/cpp11_pointer_cast_test.cpp
Normal file
224
test/cpp11_pointer_cast_test.cpp
Normal file
@@ -0,0 +1,224 @@
|
||||
//
|
||||
// cpp11_pointer_cast_test.cpp - a test for boost/pointer_cast.hpp with std::shared_ptr and std::unique_ptr
|
||||
//
|
||||
// Copyright (c) 2016 Karolin Varner
|
||||
//
|
||||
// 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/pointer_cast.hpp>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
#include <boost/get_pointer.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <functional>
|
||||
|
||||
#if defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) \
|
||||
|| defined( BOOST_NO_CXX11_HDR_FUNCTIONAL ) \
|
||||
|| defined( BOOST_NO_CXX11_HDR_UTILITY ) \
|
||||
|| defined( BOOST_NO_CXX11_LAMBDAS ) \
|
||||
|| defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
||||
|
||||
// We expect all the features or none of the features to be
|
||||
// available, since we should be on C++11
|
||||
int main() { return 0; }
|
||||
|
||||
#else
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
// Let's create these inheritance relationship:
|
||||
//
|
||||
// base base2
|
||||
// | |
|
||||
// derived
|
||||
// |
|
||||
// derived_derived
|
||||
//
|
||||
|
||||
class base
|
||||
{
|
||||
public:
|
||||
virtual ~base(){}
|
||||
int filler [5];
|
||||
};
|
||||
|
||||
class base2
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~base2(){}
|
||||
int filler [5];
|
||||
};
|
||||
|
||||
class derived
|
||||
: public base, public base2
|
||||
{
|
||||
int filler [5];
|
||||
};
|
||||
|
||||
class derived_derived
|
||||
: public derived
|
||||
{
|
||||
int filler [5];
|
||||
};
|
||||
|
||||
// And now some simple check functions
|
||||
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
|
||||
template <class BasePtr>
|
||||
bool check_dynamic_pointer_cast(const BasePtr &ptr)
|
||||
{
|
||||
//Check that dynamic_pointer_cast versus dynamic_cast
|
||||
return
|
||||
//Correct cast with dynamic_pointer_cast
|
||||
boost::get_pointer(boost::dynamic_pointer_cast<derived>(ptr)) ==
|
||||
//Correct cast with dynamic_cast
|
||||
dynamic_cast<derived*>(boost::get_pointer(ptr))
|
||||
&&
|
||||
//Incorrect cast with dynamic_pointer_cast
|
||||
boost::get_pointer(boost::dynamic_pointer_cast<derived_derived>(ptr)) ==
|
||||
//Incorrect cast with dynamic_cast
|
||||
dynamic_cast<derived_derived*>(boost::get_pointer(ptr));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
template <class BasePtr>
|
||||
bool check_static_pointer_cast(const BasePtr &ptr)
|
||||
{
|
||||
return
|
||||
//Cast base -> derived -> base2 using static_pointer_cast
|
||||
boost::get_pointer(
|
||||
boost::static_pointer_cast<base2>(
|
||||
boost::static_pointer_cast<derived>(ptr))) ==
|
||||
//Now the same with static_cast
|
||||
static_cast<base2*>(static_cast<derived*>(boost::get_pointer(ptr)));
|
||||
}
|
||||
|
||||
template <class BasePtr>
|
||||
bool check_const_pointer_cast(const BasePtr &ptr)
|
||||
{
|
||||
return
|
||||
//Unconst and const again using const_pointer_cast
|
||||
boost::get_pointer(
|
||||
boost::const_pointer_cast<const base>
|
||||
(boost::const_pointer_cast<base>(ptr))) ==
|
||||
//Now the same with const_cast
|
||||
const_cast<const base*>(const_cast<base*>(boost::get_pointer(ptr)));
|
||||
}
|
||||
|
||||
template <class BasePtr>
|
||||
void check_all_copy_casts(const BasePtr &ptr)
|
||||
{
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
BOOST_TEST( check_dynamic_pointer_cast( ptr ) );
|
||||
#endif
|
||||
BOOST_TEST( check_static_pointer_cast( ptr ) );
|
||||
BOOST_TEST( check_const_pointer_cast( ptr ) );
|
||||
}
|
||||
|
||||
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
|
||||
template <class BasePtr>
|
||||
bool check_dynamic_moving_pointer_cast(std::function<BasePtr()> f)
|
||||
{
|
||||
BasePtr smart1 = f(), smart2 = f();
|
||||
derived* expect1 = dynamic_cast<derived*>(boost::get_pointer(smart1));
|
||||
derived_derived* expect2 = dynamic_cast<derived_derived*>(boost::get_pointer(smart2));
|
||||
//Check that dynamic_pointer_cast versus dynamic_cast
|
||||
return
|
||||
//Correct cast with dynamic_pointer_cast
|
||||
boost::get_pointer(boost::dynamic_pointer_cast<derived>( std::move(smart1) )) == expect1
|
||||
&&
|
||||
//Incorrect cast with dynamic_pointer_cast
|
||||
boost::get_pointer(boost::dynamic_pointer_cast<derived_derived>( std::move(smart2) )) == expect2;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
template <class BasePtr>
|
||||
bool check_static_moving_pointer_cast(std::function<BasePtr()> f)
|
||||
{
|
||||
BasePtr smart = f();
|
||||
base2 *expect = static_cast<base2*>(static_cast<derived*>(boost::get_pointer(smart)));
|
||||
|
||||
return
|
||||
//Cast base -> derived -> base2 using static_pointer_cast
|
||||
boost::get_pointer(
|
||||
boost::static_pointer_cast<base2>(
|
||||
boost::static_pointer_cast<derived>( std::move(smart) ))) ==
|
||||
//Now the same with static_cast
|
||||
expect;
|
||||
}
|
||||
|
||||
template <class BasePtr>
|
||||
bool check_const_moving_pointer_cast(std::function<BasePtr()> f)
|
||||
{
|
||||
BasePtr smart = f();
|
||||
const base *expect = const_cast<const base*>(const_cast<base*>(boost::get_pointer(smart)));
|
||||
return
|
||||
//Unconst and const again using const_pointer_cast
|
||||
boost::get_pointer(
|
||||
boost::const_pointer_cast<const base>
|
||||
(boost::const_pointer_cast<base>( std::move(smart) ))) ==
|
||||
//Now the same with const_cast
|
||||
expect;
|
||||
}
|
||||
|
||||
template <class BasePtr>
|
||||
void check_all_moving_casts(std::function<BasePtr()> f) {
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
BOOST_TEST( check_dynamic_moving_pointer_cast( f ) );
|
||||
#endif
|
||||
BOOST_TEST( check_static_moving_pointer_cast( f ) );
|
||||
BOOST_TEST( check_const_moving_pointer_cast( f ) );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
std::shared_ptr<base> std_shared(new derived);
|
||||
boost::shared_ptr<base> boost_shared(new derived);
|
||||
base *plain = boost_shared.get();
|
||||
|
||||
// plain & boost::shared_ptr moving pointer_cast checks; there
|
||||
// is no specific handleing for those types at the moment; this
|
||||
// test just makes sure they won't break when std::move() is used
|
||||
// in generic code
|
||||
|
||||
check_all_moving_casts<boost::shared_ptr<base>>([&boost_shared]() {
|
||||
return boost_shared;
|
||||
});
|
||||
|
||||
check_all_moving_casts<base*>([plain]() {
|
||||
return plain;
|
||||
});
|
||||
|
||||
// std::shared_ptr casts
|
||||
|
||||
check_all_copy_casts(std_shared);
|
||||
check_all_moving_casts<std::shared_ptr<base>>([&std_shared]() {
|
||||
return std_shared;
|
||||
});
|
||||
|
||||
// std::unique_ptr casts
|
||||
|
||||
check_all_moving_casts<std::unique_ptr<base>>([]() {
|
||||
return std::unique_ptr<base>(new derived);
|
||||
});
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
#endif
|
18
test/pointer_cast_co_fail.cpp
Normal file
18
test/pointer_cast_co_fail.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// A negative test for unique_ptr const_cast
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// 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/pointer_cast.hpp>
|
||||
#include <memory>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<int> p1( new int );
|
||||
std::unique_ptr<int[]> p2 = boost::const_pointer_cast<int[]>( std::move( p1 ) );
|
||||
}
|
18
test/pointer_cast_co_fail2.cpp
Normal file
18
test/pointer_cast_co_fail2.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// A negative test for unique_ptr const_cast
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// 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/pointer_cast.hpp>
|
||||
#include <memory>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<int[]> p1( new int[ 1 ] );
|
||||
std::unique_ptr<int> p2 = boost::const_pointer_cast<int>( std::move( p1 ) );
|
||||
}
|
29
test/pointer_cast_co_fail3.cpp
Normal file
29
test/pointer_cast_co_fail3.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// A negative test for unique_ptr const_cast
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// 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/pointer_cast.hpp>
|
||||
#include <memory>
|
||||
|
||||
struct B
|
||||
{
|
||||
virtual ~B()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
struct D: B
|
||||
{
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<D[]> p1( new D[ 1 ] );
|
||||
std::unique_ptr<B[]> p2 = boost::const_pointer_cast<B[]>( std::move( p1 ) );
|
||||
}
|
25
test/pointer_cast_dy_fail.cpp
Normal file
25
test/pointer_cast_dy_fail.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// A negative test for unique_ptr dynamic_cast
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// 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/pointer_cast.hpp>
|
||||
#include <memory>
|
||||
|
||||
struct B
|
||||
{
|
||||
virtual ~B()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<B> p1( new B );
|
||||
std::unique_ptr<B[]> p2 = boost::dynamic_pointer_cast<B[]>( std::move( p1 ) );
|
||||
}
|
25
test/pointer_cast_dy_fail2.cpp
Normal file
25
test/pointer_cast_dy_fail2.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// A negative test for unique_ptr dynamic_cast
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// 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/pointer_cast.hpp>
|
||||
#include <memory>
|
||||
|
||||
struct B
|
||||
{
|
||||
virtual ~B()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<B[]> p1( new B[ 1 ] );
|
||||
std::unique_ptr<B> p2 = boost::dynamic_pointer_cast<B>( std::move( p1 ) );
|
||||
}
|
29
test/pointer_cast_dy_fail3.cpp
Normal file
29
test/pointer_cast_dy_fail3.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// A negative test for unique_ptr dynamic_cast
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// 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/pointer_cast.hpp>
|
||||
#include <memory>
|
||||
|
||||
struct B
|
||||
{
|
||||
virtual ~B()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
struct D: B
|
||||
{
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<D[]> p1( new D[ 1 ] );
|
||||
std::unique_ptr<B[]> p2 = boost::dynamic_pointer_cast<B[]>( std::move( p1 ) );
|
||||
}
|
18
test/pointer_cast_st_fail.cpp
Normal file
18
test/pointer_cast_st_fail.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// A negative test for unique_ptr static_cast
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// 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/pointer_cast.hpp>
|
||||
#include <memory>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<int> p1( new int );
|
||||
std::unique_ptr<int[]> p2 = boost::static_pointer_cast<int[]>( std::move( p1 ) );
|
||||
}
|
18
test/pointer_cast_st_fail2.cpp
Normal file
18
test/pointer_cast_st_fail2.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// A negative test for unique_ptr static_cast
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// 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/pointer_cast.hpp>
|
||||
#include <memory>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<int[]> p1( new int[ 1 ] );
|
||||
std::unique_ptr<int> p2 = boost::static_pointer_cast<int>( std::move( p1 ) );
|
||||
}
|
29
test/pointer_cast_st_fail3.cpp
Normal file
29
test/pointer_cast_st_fail3.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// A negative test for unique_ptr static_cast
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// 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/pointer_cast.hpp>
|
||||
#include <memory>
|
||||
|
||||
struct B
|
||||
{
|
||||
virtual ~B()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
struct D: B
|
||||
{
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::unique_ptr<D[]> p1( new D[ 1 ] );
|
||||
std::unique_ptr<B[]> p2 = boost::static_pointer_cast<B[]>( std::move( p1 ) );
|
||||
}
|
@@ -104,33 +104,25 @@ bool check_const_pointer_cast(const BasePtr &ptr)
|
||||
const_cast<const base*>(const_cast<base*>(boost::get_pointer(ptr)));
|
||||
}
|
||||
|
||||
template <class BasePtr>
|
||||
void check_all_casts(const BasePtr &ptr)
|
||||
{
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
BOOST_TEST( check_dynamic_pointer_cast( ptr ) );
|
||||
#endif
|
||||
BOOST_TEST( check_static_pointer_cast( ptr ) );
|
||||
BOOST_TEST( check_const_pointer_cast( ptr ) );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
// Try casts with shared_ptr
|
||||
boost::shared_ptr<base> boost_shared(new derived);
|
||||
base *plain = boost_shared.get();
|
||||
|
||||
boost::shared_ptr<base> ptr(new derived);
|
||||
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
BOOST_TEST( check_dynamic_pointer_cast( ptr ) );
|
||||
#endif
|
||||
BOOST_TEST( check_static_pointer_cast( ptr ) );
|
||||
BOOST_TEST( check_const_pointer_cast( ptr ) );
|
||||
}
|
||||
|
||||
{
|
||||
// Try casts with raw pointer
|
||||
|
||||
boost::scoped_ptr<base> ptr(new derived);
|
||||
|
||||
#if !defined( BOOST_NO_RTTI )
|
||||
BOOST_TEST( check_dynamic_pointer_cast( ptr.get() ) );
|
||||
#endif
|
||||
BOOST_TEST( check_static_pointer_cast( ptr.get() ) );
|
||||
BOOST_TEST( check_const_pointer_cast( ptr.get() ) );
|
||||
}
|
||||
check_all_casts(boost_shared);
|
||||
check_all_casts(plain);
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
247
test/pointer_cast_test2.cpp
Normal file
247
test/pointer_cast_test2.cpp
Normal file
@@ -0,0 +1,247 @@
|
||||
//
|
||||
// pointer_cast_test2.cpp - a test for unique_ptr casts
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// 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/config.hpp>
|
||||
|
||||
#if defined( BOOST_NO_CXX11_SMART_PTR )
|
||||
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <boost/pointer_cast.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <memory>
|
||||
|
||||
struct B1
|
||||
{
|
||||
};
|
||||
|
||||
struct D1: B1
|
||||
{
|
||||
~D1()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
static void test_static_cast()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<int> p1( new int );
|
||||
int * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<int> p2 = boost::static_pointer_cast<int>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<int> p1( new int );
|
||||
int * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<int const> p2 = boost::static_pointer_cast<int const>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<int[]> p1( new int[ 1 ] );
|
||||
int * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<int[]> p2 = boost::static_pointer_cast<int[]>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<int[]> p1( new int[ 1 ] );
|
||||
int * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<int const[]> p2 = boost::static_pointer_cast<int const[]>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<D1> p1( new D1 );
|
||||
D1 * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<B1> p2 = boost::static_pointer_cast<B1>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
|
||||
std::unique_ptr<D1> p3 = boost::static_pointer_cast<D1>( std::move( p2 ) );
|
||||
|
||||
BOOST_TEST( p2.get() == 0 );
|
||||
BOOST_TEST_EQ( p3.get(), q1 );
|
||||
}
|
||||
}
|
||||
|
||||
static void test_const_cast()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<int> p1( new int );
|
||||
int * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<int const> p2 = boost::const_pointer_cast<int const>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
|
||||
std::unique_ptr<int> p3 = boost::const_pointer_cast<int>( std::move( p2 ) );
|
||||
|
||||
BOOST_TEST( p2.get() == 0 );
|
||||
BOOST_TEST_EQ( p3.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<int[]> p1( new int[ 1 ] );
|
||||
int * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<int const[]> p2 = boost::const_pointer_cast<int const[]>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
|
||||
std::unique_ptr<int[]> p3 = boost::const_pointer_cast<int[]>( std::move( p2 ) );
|
||||
|
||||
BOOST_TEST( p2.get() == 0 );
|
||||
BOOST_TEST_EQ( p3.get(), q1 );
|
||||
}
|
||||
}
|
||||
|
||||
struct B2
|
||||
{
|
||||
virtual ~B2()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
struct C2
|
||||
{
|
||||
virtual ~C2()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
struct D2: B2, C2
|
||||
{
|
||||
};
|
||||
|
||||
static void test_dynamic_cast()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<D2> p1( new D2 );
|
||||
D2 * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<B2> p2 = boost::dynamic_pointer_cast<B2>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<B2> p1( new D2 );
|
||||
B2 * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<D2> p2 = boost::dynamic_pointer_cast<D2>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<B2> p1( new B2 );
|
||||
B2 * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<D2> p2 = boost::dynamic_pointer_cast<D2>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p2.get() == 0 );
|
||||
BOOST_TEST_EQ( p1.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
D2 * q1 = new D2;
|
||||
std::unique_ptr<B2> p1( q1 );
|
||||
|
||||
std::unique_ptr<C2> p2 = boost::dynamic_pointer_cast<C2>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
}
|
||||
|
||||
static void test_reinterpret_cast()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<int> p1( new int );
|
||||
void * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<char> p2 = boost::reinterpret_pointer_cast<char>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
|
||||
p1 = boost::reinterpret_pointer_cast<int>( std::move( p2 ) );
|
||||
|
||||
BOOST_TEST( p2.get() == 0 );
|
||||
BOOST_TEST_EQ( p1.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<int> p1( new int );
|
||||
void * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<char[]> p2 = boost::reinterpret_pointer_cast<char[]>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
|
||||
p1 = boost::reinterpret_pointer_cast<int>( std::move( p2 ) );
|
||||
|
||||
BOOST_TEST( p2.get() == 0 );
|
||||
BOOST_TEST_EQ( p1.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<int[]> p1( new int[ 1 ] );
|
||||
void * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<char[]> p2 = boost::reinterpret_pointer_cast<char[]>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
|
||||
p1 = boost::reinterpret_pointer_cast<int[]>( std::move( p2 ) );
|
||||
|
||||
BOOST_TEST( p2.get() == 0 );
|
||||
BOOST_TEST_EQ( p1.get(), q1 );
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_static_cast();
|
||||
test_const_cast();
|
||||
test_dynamic_cast();
|
||||
test_reinterpret_cast();
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user