Boost logo

Boost :

From: Raz (coolmandan_at_[hidden])
Date: 2005-02-13 20:27:26


Why doesn't Boost have any smart pointers that automatically lock a
class when a member is used? The code isn't that hard.

#include <iostream>

struct Dummy { int a; }

class Lock {
private:
  Dummy &_d;
public:
  Lock( Dummy &d ) : _d(d) { std::cout << "Locked." << std::endl; }
  ~Lock( void ) { std::cout << "Unlocked." << std::endl; }

  Dummy *operator->( void ) { return &_d; }
};

class Ref {
private:
  Dummy &_d;
public:
  Ref( Dummy &d ) : _d(d) { }

   Lock operator->(void) { return Lock(_d); }
};

int main( void )
{
   Dummy d;
   Ref x(d);

   x->a = 1;
   std::cout << x->a << std::endl;
}

I get:

Locked
Unlocked
Locked
1
Unlocked

on my system (g++ 3.3.3 and g++ 3.4.0).

Has there been any reason not to do this?

-- 
-------------------------------------------
 Dan Day
http://razzerblog.blogspot.com

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