Transfer functionality for shared_ptr
If you have a shared_ptr, perhaps with a custom deleter of unknown type,
what extra functionality is required to then transfer that shared_ptr's
raw pointer and deleter to some other smart pointer?
In order to answer that question I created the small test program listed
below, where the class SharedPtr is a simplified variant of shared_ptr
(only features relevant to this problem).
The transferTo() member function seems to require, as shown here, two
member functions values() and clear() which would be very dangerous,
breaking encapsulation, if exposed to client code.
In other words, the transferTo() member function seems to be safe and
useful functionality that can only reasonably be implemented by
shared_ptr itself, and is not currently present.
I see no way to implement transferTo() using only the current public
functionality of shared_ptr.
<code>
// Example of transferTo() (a.k.a. "release") functionality for shared
// pointer.
// I had to fix up some formatting due to line-wrapping in posting.
// ...
#include <iostream>
#include <ostream>
#include <cstddef> // std::size_t
#include <utility> // std::pair
#include <stdexcept>
#include <boost/intrusive_ptr.hpp>
#include <boost/function.hpp>
#include <boost/type_traits/remove_const.hpp>
template< typename T >
void say( T const& v ) { std::cout << v << std::endl; }
namespace ext
{
namespace detail
{
template< typename T >
class AbstractRefCounter
{
private:
std::size_t myRefCount;
public:
typedef boost::function< void (T const*) > AbstractDeleter;
typedef std::pair< T*, AbstractDeleter > InternalValues;
AbstractRefCounter(): myRefCount( 0 ) {}
virtual ~AbstractRefCounter() {}
void addRef() { ++myRefCount; }
void release() { if( --myRefCount == 0 ) { delete this; } }
std::size_t count() const { return myRefCount; }
virtual InternalValues values() const = 0;
virtual void clear() = 0;
};
template< typename T >
void intrusive_ptr_add_ref( AbstractRefCounter<T>* p )
{ p->addRef(); }
template< typename T >
void intrusive_ptr_release( AbstractRefCounter<T>* p )
{ p->release(); }
template< typename T >
void defaultDeleter( T const* p ) { delete p; }
template< typename T, typename D >
class DeleterInvoker
{
public:
typedef typename boost::remove_const<T>::type NcT;
D deleter;
DeleterInvoker( D d ): deleter( d ) {}
void operator()( T const* p ) const
{
deleter( const_cast<NcT*>( p ) );
}
};
template< typename T, typename D >
class RefCounter: public AbstractRefCounter<T>
{
private:
T* myPtr;
DeleterInvoker<T, D> myDeleter;
public:
typedef typename AbstractRefCounter<T>::InternalValues
InternalValues;
RefCounter( T* p, D d ): myPtr( p ), myDeleter( d ) {}
~RefCounter() { if( myPtr != 0 ) { myDeleter( myPtr ); } }
InternalValues values() const
{
return InternalValues( myPtr, myDeleter );
}
void clear() { myPtr = 0; }
};
}
template< typename T >
class SharedPtr
{
private:
typedef boost::intrusive_ptr< detail::AbstractRefCounter<T> >
RefCountedPtr;
RefCountedPtr myPtr;
public:
typedef typename detail::AbstractRefCounter<T>::InternalValues
InternalValues;
SharedPtr(): myPtr() {}
template< typename D >
SharedPtr( T* p, D d )
: myPtr( new detail::RefCounter<T, D>( p, d ) )
{}
bool isUnique() const
{ return (!myPtr? false : myPtr->count() == 1); }
template< typename ValuesReceiver >
ValuesReceiver transferTo()
{
if( !isUnique() )
{
throw std::logic_error(
"SharedPtr::transferTo: is not unique()"
);
}
InternalValues const values = myPtr->values();
myPtr->clear();
return ValuesReceiver( values );
}
};
} // namespace ext
template< typename T >
void destroy( T* p )
{
say( "automatically deleted" );
delete p;
}
enum TransferTestEnum{ testTransferSuccess, testTransferFailure };
void cppMain( TransferTestEnum whichTest )
{
typedef ext::SharedPtr<int> IntPtr;
IntPtr p( new int( 42 ), destroy<int> );
IntPtr q;
if( whichTest == testTransferFailure ) { q = p; }
say( "main" );
IntPtr::InternalValues const values =
p.transferTo<IntPtr::InternalValues>();
say( *values.first ); // 42
delete values.first;
say( "manually deleted" );
}
int main( int nArgs, char*[] )
{
try
{
cppMain(
nArgs == 1? testTransferSuccess : testTransferFailure
);
return EXIT_SUCCESS;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
return EXIT_FAILURE;
}
}
</code>
<output arguments="">
main
42
manually deleted
</output>
<output arguments="whatever">
main
automatically deleted
!SharedPtr::transferTo: is not unique()
</output>
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]