mirror of https://gitee.com/bigwinds/arangodb
107 lines
4.0 KiB
HTML
107 lines
4.0 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||
<html>
|
||
<head>
|
||
<title>pointer_cast</title>
|
||
<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>
|
||
<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<...></code> implements
|
||
a static pointer cast this way:</P>
|
||
<pre>
|
||
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>
|
||
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<class T, class U>
|
||
inline T* static_pointer_cast(U *ptr)
|
||
{ return static_cast<T*>(ptr); }
|
||
|
||
template<class T, class U>
|
||
inline T* dynamic_pointer_cast(U *ptr)
|
||
{ return dynamic_cast<T*>(ptr); }
|
||
|
||
template<class T, class U>
|
||
inline T* const_pointer_cast(U *ptr)
|
||
{ return const_cast<T*>(ptr); }
|
||
|
||
template<class T, class U>
|
||
inline T* reinterpret_pointer_cast(U *ptr)
|
||
{ return reinterpret_cast<T*>(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 <boost/pointer_cast.hpp>
|
||
#include <boost/shared_ptr.hpp>
|
||
|
||
class base
|
||
{
|
||
public:
|
||
|
||
virtual ~base()
|
||
{
|
||
}
|
||
};
|
||
|
||
class derived: public base
|
||
{
|
||
};
|
||
|
||
template <class BasePtr>
|
||
void check_if_it_is_derived(const BasePtr &ptr)
|
||
{
|
||
assert(boost::dynamic_pointer_cast<derived>(ptr) != 0);
|
||
}
|
||
|
||
int main()
|
||
{
|
||
<I>// Create a raw and a shared_ptr</I>
|
||
|
||
base *ptr = new derived;
|
||
boost::shared_ptr<base> 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>$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 <<A href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>>.)</p>
|
||
</body>
|
||
</html>
|