forked from boostorg/smart_ptr
const_pointer_cast added.
[SVN r19147]
This commit is contained in:
@@ -220,6 +220,11 @@ template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U>
|
||||
return static_cast<T *>(p.get());
|
||||
}
|
||||
|
||||
template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
|
||||
{
|
||||
return const_cast<T *>(p.get());
|
||||
}
|
||||
|
||||
template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
|
||||
{
|
||||
return dynamic_cast<T *>(p.get());
|
||||
|
@@ -48,6 +48,7 @@ namespace detail
|
||||
{
|
||||
|
||||
struct static_cast_tag {};
|
||||
struct const_cast_tag {};
|
||||
struct dynamic_cast_tag {};
|
||||
struct polymorphic_cast_tag {};
|
||||
|
||||
@@ -68,6 +69,16 @@ template<> struct shared_ptr_traits<void const>
|
||||
typedef void reference;
|
||||
};
|
||||
|
||||
template<> struct shared_ptr_traits<void volatile>
|
||||
{
|
||||
typedef void reference;
|
||||
};
|
||||
|
||||
template<> struct shared_ptr_traits<void const volatile>
|
||||
{
|
||||
typedef void reference;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
// enable_shared_from_this support
|
||||
@@ -158,6 +169,11 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
template<class Y>
|
||||
shared_ptr(shared_ptr<Y> const & r, detail::const_cast_tag): px(const_cast<element_type *>(r.px)), pn(r.pn)
|
||||
{
|
||||
}
|
||||
|
||||
template<class Y>
|
||||
shared_ptr(shared_ptr<Y> const & r, detail::dynamic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
|
||||
{
|
||||
@@ -351,6 +367,11 @@ template<class T, class U> shared_ptr<T> static_pointer_cast(shared_ptr<U> const
|
||||
return shared_ptr<T>(r, detail::static_cast_tag());
|
||||
}
|
||||
|
||||
template<class T, class U> shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r)
|
||||
{
|
||||
return shared_ptr<T>(r, detail::const_cast_tag());
|
||||
}
|
||||
|
||||
template<class T, class U> shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r)
|
||||
{
|
||||
return shared_ptr<T>(r, detail::dynamic_cast_tag());
|
||||
|
@@ -100,6 +100,9 @@
|
||||
template<class T, class U>
|
||||
intrusive_ptr<T> <A href="#static_pointer_cast" >static_pointer_cast</A>(intrusive_ptr<U> const & r); // never throws
|
||||
|
||||
template<class T, class U>
|
||||
intrusive_ptr<T> <A href="#const_pointer_cast" >const_pointer_cast</A>(intrusive_ptr<U> const & r); // never throws
|
||||
|
||||
template<class T, class U>
|
||||
intrusive_ptr<T> <A href="#dynamic_pointer_cast" >dynamic_pointer_cast</A>(intrusive_ptr<U> const & r); // never throws
|
||||
|
||||
@@ -250,6 +253,13 @@ intrusive_ptr & operator=(T * r);</pre>
|
||||
<P><B>Returns:</B> <code>intrusive_ptr<T>(static_cast<T*>(r.get()))</code>.</P>
|
||||
<P><B>Throws:</B> nothing.</P>
|
||||
</BLOCKQUOTE>
|
||||
<h3><a name="const_pointer_cast">const_pointer_cast</a></h3>
|
||||
<pre>template<class T, class U>
|
||||
intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & r); // never throws</pre>
|
||||
<BLOCKQUOTE>
|
||||
<P><B>Returns:</B> <code>intrusive_ptr<T>(const_cast<T*>(r.get()))</code>.</P>
|
||||
<P><B>Throws:</B> nothing.</P>
|
||||
</BLOCKQUOTE>
|
||||
<h3><a name="dynamic_pointer_cast">dynamic_pointer_cast</a></h3>
|
||||
<pre>template<class T, class U>
|
||||
intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & r);</pre>
|
||||
|
@@ -142,6 +142,9 @@ void bad()
|
||||
template<class T, class U>
|
||||
shared_ptr<T> <A href="#static_pointer_cast" >static_pointer_cast</A>(shared_ptr<U> const & r); // never throws
|
||||
|
||||
template<class T, class U>
|
||||
shared_ptr<T> <A href="#const_pointer_cast" >const_pointer_cast</A>(shared_ptr<U> const & r); // never throws
|
||||
|
||||
template<class T, class U>
|
||||
shared_ptr<T> <A href="#dynamic_pointer_cast" >dynamic_pointer_cast</A>(shared_ptr<U> const & r); // never throws
|
||||
|
||||
@@ -473,6 +476,21 @@ q = p;
|
||||
<p>will eventually result in undefined behavior, attempting to delete the same
|
||||
object twice.</p>
|
||||
</BLOCKQUOTE>
|
||||
<h3><a name="const_pointer_cast">const_pointer_cast</a></h3>
|
||||
<pre>template<class T, class U>
|
||||
shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r); // never throws</pre>
|
||||
<BLOCKQUOTE>
|
||||
<P><STRONG>Requires:</STRONG> The expression <code>const_cast<T*>(r.get())</code>
|
||||
must be well-formed.</P>
|
||||
<P><B>Returns:</B> If <b>r</b> is <i>empty</i>, an <i>empty</i> <b>shared_ptr<T></b>;
|
||||
otherwise, a <STRONG>shared_ptr<T></STRONG> object that stores a copy of <code>
|
||||
const_cast<T*>(r.get())</code> and <i>shares ownership</i> with <b>r</b>.</P>
|
||||
<P><B>Throws:</B> nothing.</P>
|
||||
<P><B>Notes:</B> the seemingly equivalent expression</P>
|
||||
<p><code>shared_ptr<T>(const_cast<T*>(r.get()))</code></p>
|
||||
<p>will eventually result in undefined behavior, attempting to delete the same
|
||||
object twice.</p>
|
||||
</BLOCKQUOTE>
|
||||
<h3><a name="dynamic_pointer_cast">dynamic_pointer_cast</a></h3>
|
||||
<pre>template<class T, class U>
|
||||
shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r);</pre>
|
||||
|
@@ -2375,6 +2375,57 @@ void test()
|
||||
|
||||
} // namespace n_static_cast
|
||||
|
||||
namespace n_const_cast
|
||||
{
|
||||
|
||||
struct X;
|
||||
|
||||
void test()
|
||||
{
|
||||
{
|
||||
boost::shared_ptr<void const volatile> px;
|
||||
|
||||
boost::shared_ptr<void> px2 = boost::const_pointer_cast<void>(px);
|
||||
BOOST_TEST(px2.get() == 0);
|
||||
}
|
||||
|
||||
{
|
||||
boost::shared_ptr<int const volatile> px;
|
||||
|
||||
boost::shared_ptr<int> px2 = boost::const_pointer_cast<int>(px);
|
||||
BOOST_TEST(px2.get() == 0);
|
||||
}
|
||||
|
||||
{
|
||||
boost::shared_ptr<X const volatile> px;
|
||||
|
||||
boost::shared_ptr<X> px2 = boost::const_pointer_cast<X>(px);
|
||||
BOOST_TEST(px2.get() == 0);
|
||||
}
|
||||
|
||||
{
|
||||
boost::shared_ptr<void const volatile> px(new int);
|
||||
|
||||
boost::shared_ptr<void> px2 = boost::const_pointer_cast<void>(px);
|
||||
BOOST_TEST(px.get() == px2.get());
|
||||
BOOST_TEST(!(px < px2 || px2 < px));
|
||||
BOOST_TEST(px.use_count() == 2);
|
||||
BOOST_TEST(px2.use_count() == 2);
|
||||
}
|
||||
|
||||
{
|
||||
boost::shared_ptr<int const volatile> px(new int);
|
||||
|
||||
boost::shared_ptr<int> px2 = boost::const_pointer_cast<int>(px);
|
||||
BOOST_TEST(px.get() == px2.get());
|
||||
BOOST_TEST(!(px < px2 || px2 < px));
|
||||
BOOST_TEST(px.use_count() == 2);
|
||||
BOOST_TEST(px2.use_count() == 2);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace n_const_cast
|
||||
|
||||
namespace n_dynamic_cast
|
||||
{
|
||||
|
||||
@@ -3141,6 +3192,7 @@ int main()
|
||||
n_swap::test();
|
||||
n_comparison::test();
|
||||
n_static_cast::test();
|
||||
n_const_cast::test();
|
||||
n_dynamic_cast::test();
|
||||
|
||||
n_map::test();
|
||||
|
Reference in New Issue
Block a user