Boost logo

Boost :

Subject: Re: [boost] [smart_ptr] shared_ptr template type
From: Sid Sacek (ssacek_at_[hidden])
Date: 2009-07-09 14:20:43


This was fun to experiment with. Here's another whack at your puzzle.
-Sid Sacek

#include <iostream>
#include <boost/shared_ptr.hpp>

using namespace std;
using boost::shared_ptr;

template< typename T > class monitor;

template< typename T > class locker
{
        shared_ptr< T > shared_obj_;
public:
        locker( shared_ptr< T > &shared_obj ) : shared_obj_( shared_obj
)
        {
                shared_obj_->lock();
        }
        ~locker()
        {
                shared_obj_->unlock();
        }
        shared_ptr< T > operator->()
        {
                return shared_obj_;
        }
};

template< typename T > class monitor
{
        shared_ptr< T > shared_obj_;
public:
        monitor( T * t ) : shared_obj_( shared_ptr< T >( t ) )
        { }

        locker< T > operator->()
        {
                return locker< T >( shared_obj_ );
        }
};

struct mutex
{
        void lock() { cout << "object locked \n"; }
        void unlock() { cout << "object unlocked \n"; }
};

struct Q : mutex
{
        void ModifyObject() { cout << "Object modified \n"; }
};

int main( int argc, char * argv[] )
{
        monitor< Q > m( new Q );
        m->ModifyObject();
        return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk