Pointer utilities added (proposed by Ion Gaztañaga)

[SVN r31932]
This commit is contained in:
Peter Dimov
2005-12-06 13:26:13 +00:00
parent 2d25f8f036
commit 90b5a3736a
7 changed files with 409 additions and 0 deletions

View File

@ -0,0 +1,45 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gazta<74>aga 2005.
// 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)
//
//////////////////////////////////////////////////////////////////////////////
#ifndef BOOST_POINTER_CAST_HPP
#define BOOST_POINTER_CAST_HPP
namespace boost {
//static_pointer_cast overload for raw pointers
template<class T, class U>
inline T* static_pointer_cast(U *ptr)
{
return static_cast<T*>(ptr);
}
//dynamic_pointer_cast overload for raw pointers
template<class T, class U>
inline T* dynamic_pointer_cast(U *ptr)
{
return dynamic_cast<T*>(ptr);
}
//const_pointer_cast overload for raw pointers
template<class T, class U>
inline T* const_pointer_cast(U *ptr)
{
return const_cast<T*>(ptr);
}
//reinterpret_pointer_cast overload for raw pointers
template<class T, class U>
inline T* reinterpret_pointer_cast(U *ptr)
{
return reinterpret_cast<T*>(ptr);
}
} // namespace boost
#endif //BOOST_POINTER_CAST_HPP

View File

@ -0,0 +1,55 @@
#ifndef BOOST_POINTER_TO_OTHER_HPP_INCLUDED
#define BOOST_POINTER_TO_OTHER_HPP_INCLUDED
//
// pointer_to_other.hpp
//
// (C) Copyright Ion Gazta<74>aga 2005.
// Copyright (c) 2005 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)
//
// See http://www.boost.org/libs/smart_ptr/pointer_to_other.html
//
namespace boost
{
// Defines the same pointer type (raw or smart) to another pointee type
template<class T, class U>
struct pointer_to_other;
template<class T, class U,
template<class> class Sp>
struct pointer_to_other< Sp<T>, U >
{
typedef Sp<U> type;
};
template<class T, class T2, class U,
template<class, class> class Sp>
struct pointer_to_other< Sp<T, T2>, U >
{
typedef Sp<U, T2> type;
};
template<class T, class T2, class T3, class U,
template<class, class, class> class Sp>
struct pointer_to_other< Sp<T, T2, T3>, U >
{
typedef Sp<U, T2, T3> type;
};
template<class T, class U>
struct pointer_to_other< T*, U >
{
typedef U* type;
};
} // namespace boost
#endif // #ifndef BOOST_POINTER_TO_OTHER_HPP_INCLUDED

105
pointer_cast.html Normal file
View File

@ -0,0 +1,105 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>pointer_cast.hpp</title>
</head>
<body>
<h1><IMG height="86" alt="C++ Boost" src="../../boost.png" width="277" align="middle" border="0">Pointer
cast functions</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>
<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&lt;...&gt;</code> implements
a static pointer cast this way:</P>
<pre>
template&lt;class T, class U&gt;
shared_ptr&lt;T&gt; static_pointer_cast(shared_ptr&lt;U&gt; const &amp;r);
</pre>
<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>
namespace boost {
template&lt;class T, class U&gt;
inline T* static_pointer_cast(U *ptr)
{ return static_cast&lt;T*&gt;(ptr); }
template&lt;class T, class U&gt;
inline T* dynamic_pointer_cast(U *ptr)
{ return dynamic_cast&lt;T*&gt;(ptr); }
template&lt;class T, class U&gt;
inline T* const_pointer_cast(U *ptr)
{ return const_cast&lt;T*&gt;(ptr); }
template&lt;class T, class U&gt;
inline T* reinterpret_pointer_cast(U *ptr)
{ return reinterpret_cast&lt;T*&gt;(ptr); }
} // 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>
#include &lt;boost/pointer_cast.hpp&gt;
#include &lt;boost/shared_ptr.hpp&gt;
class base
{
public:
virtual ~base()
{
}
};
class derived: public base
{
};
template &lt;class BasePtr&gt;
void check_if_it_is_derived(const BasePtr &amp;ptr)
{
assert(boost::dynamic_pointer_cast&lt;derived&gt;(ptr) != 0);
}
int main()
{
<I>// Create a raw and a shared_ptr</I>
base *ptr = new derived;
boost::shared_ptr&lt;base&gt; sptr(new derived);
<I>// Check that base pointer points actually to derived class</I>
check_if_it_is_derived(ptr);
check_if_it_is_derived(sptr);
// <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>Revised: $Date$</p>
<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 &lt;<A href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>&gt;.)</p>
</body>
</html>

View File

@ -31,6 +31,8 @@ DEPENDS all : smart_ptr ;
[ compile-fail shared_ptr_assign_fail.cpp ]
[ compile-fail shared_ptr_delete_fail.cpp ]
[ run shared_ptr_alloc2_test.cpp ]
[ run pointer_cast_test.cpp ]
[ compile pointer_to_other_test.cpp ]
;
# this one is too slow to run unless explicitly requested, and ALL

View File

@ -25,5 +25,7 @@ import testing ;
[ compile-fail shared_ptr_assign_fail.cpp ]
[ compile-fail shared_ptr_delete_fail.cpp ]
[ run shared_ptr_alloc2_test.cpp ]
[ run pointer_cast_test.cpp ]
[ compile pointer_to_other_test.cpp ]
;
}

126
test/pointer_cast_test.cpp Normal file
View File

@ -0,0 +1,126 @@
//
// pointer_cast_test.cpp - a test for boost/pointer_cast.hpp
//
// Copyright (c) 2005 Ion Gazta<74>aga
// Copyright (c) 2005 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 <boost/shared_ptr.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/get_pointer.hpp>
#include <boost/detail/lightweight_test.hpp>
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
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));
}
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)));
}
}
int main()
{
{
// Try casts with shared_ptr
boost::shared_ptr<base> ptr(new derived);
BOOST_TEST( check_dynamic_pointer_cast( ptr ) );
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);
BOOST_TEST( check_dynamic_pointer_cast( ptr.get() ) );
BOOST_TEST( check_static_pointer_cast( ptr.get() ) );
BOOST_TEST( check_const_pointer_cast( ptr.get() ) );
}
return boost::report_errors();
}

View File

@ -0,0 +1,74 @@
//
// pointer_to_other_test.cpp - a test for boost/pointer_to_other.hpp
//
// Copyright (c) 2005 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_to_other.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/shared_array.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/scoped_array.hpp>
#include <boost/intrusive_ptr.hpp>
#include <memory>
template<class T, class U> void assert_same_type( T** pt = 0, U** pu = 0 )
{
pt = pu;
}
struct X;
struct Y;
int main()
{
// shared_ptr
assert_same_type< boost::pointer_to_other< boost::shared_ptr<X>, Y >::type, boost::shared_ptr<Y> >();
assert_same_type< boost::pointer_to_other< boost::shared_ptr<X>, void >::type, boost::shared_ptr<void> >();
assert_same_type< boost::pointer_to_other< boost::shared_ptr<void>, Y >::type, boost::shared_ptr<Y> >();
// shared_array
assert_same_type< boost::pointer_to_other< boost::shared_array<X>, Y >::type, boost::shared_array<Y> >();
assert_same_type< boost::pointer_to_other< boost::shared_array<X>, void >::type, boost::shared_array<void> >();
assert_same_type< boost::pointer_to_other< boost::shared_array<void>, Y >::type, boost::shared_array<Y> >();
// scoped_ptr
assert_same_type< boost::pointer_to_other< boost::scoped_ptr<X>, Y >::type, boost::scoped_ptr<Y> >();
assert_same_type< boost::pointer_to_other< boost::scoped_ptr<X>, void >::type, boost::scoped_ptr<void> >();
assert_same_type< boost::pointer_to_other< boost::scoped_ptr<void>, Y >::type, boost::scoped_ptr<Y> >();
// scoped_array
assert_same_type< boost::pointer_to_other< boost::scoped_array<X>, Y >::type, boost::scoped_array<Y> >();
assert_same_type< boost::pointer_to_other< boost::scoped_array<X>, void >::type, boost::scoped_array<void> >();
assert_same_type< boost::pointer_to_other< boost::scoped_array<void>, Y >::type, boost::scoped_array<Y> >();
// intrusive_ptr
assert_same_type< boost::pointer_to_other< boost::intrusive_ptr<X>, Y >::type, boost::intrusive_ptr<Y> >();
assert_same_type< boost::pointer_to_other< boost::intrusive_ptr<X>, void >::type, boost::intrusive_ptr<void> >();
assert_same_type< boost::pointer_to_other< boost::intrusive_ptr<void>, Y >::type, boost::intrusive_ptr<Y> >();
// auto_ptr
assert_same_type< boost::pointer_to_other< std::auto_ptr<X>, Y >::type, std::auto_ptr<Y> >();
assert_same_type< boost::pointer_to_other< std::auto_ptr<X>, void >::type, std::auto_ptr<void> >();
assert_same_type< boost::pointer_to_other< std::auto_ptr<void>, Y >::type, std::auto_ptr<Y> >();
// raw pointer
assert_same_type< boost::pointer_to_other< X *, Y >::type, Y * >();
assert_same_type< boost::pointer_to_other< X *, void >::type, void * >();
assert_same_type< boost::pointer_to_other< void *, Y >::type, Y * >();
return 0;
}