|
Boost : |
Subject: Re: [boost] [smart_ptr] shared_ptr template type
From: Sid Sacek (ssacek_at_[hidden])
Date: 2009-07-09 12:50:33
> I wonder if it might be better/possible to have a shared_ptr<T> that
didn't
> assume its corresponding "raw" pointer type was T* but rather T.
If I'm not mistaken, the C++0x committee is setting the 'shared_ptr<>'
class in stone as we speak. I seriously doubt that can be changed this
late in the game, considering the amount of code that is already using
this class as-is.
See if this type of code works for you:
#include <boost/shared_ptr.hpp>
using boost::shared_ptr;
struct mutex
{
void lock() {}
void unlock() {}
};
template< typename T > class monitor;
template< typename T > class locker
{
mutex & mutex_;
monitor< T > * monitor_;
public:
locker( monitor< T > * mt, mutex & mx ) : mutex_( mx ),
monitor_( mt )
{
mutex_.lock();
}
~locker()
{
mutex_.unlock();
}
monitor< T > * operator->()
{
return monitor_;
}
};
template< typename T > class monitor
{
mutex mutex_;
public:
shared_ptr< T > obj_ptr;
monitor( T * t ) : obj_ptr( shared_ptr< T >( t ) )
{ }
locker< T > operator->()
{
return locker< T >( this, mutex_ );
}
};
int main( int argc, char * argv[] )
{
char * buf = new char[ 3 ];
memset( buf, 0, 3 );
monitor< char > m( buf );
*m->obj_ptr = 'A';
return 0;
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk