Boost logo

Boost :

From: Lee Brown (lee_at_[hidden])
Date: 2002-01-22 03:50:20


Just throwing this out there. Perhaps others will
get ideas.

/// aquisition policy
/// a nonshared object gets aquisition at construction
/// releases at destruction
/// nobody else may aquire inbetween
template
<
   class ResourcePolicy, /// the resource to be aquired
>
class nonshared : noncopyassignable {
public:
   typedef ResourcePolicy = resource_type; /// copyable value type

   nonshared(resource_type& r_) : r(r_) { r.aquire(); }
   ~nonshared() { r.release(); }

   resource_type& get_resource() { return r; }

private:
   resource_type r ;
};

/// resource pollcy
template
<
   class T /// lock handle
>
class lockable {
public:
   typedef T lock_type;

   lockable(lock_type& lock_) : lock(lock_){ }

   void aquire() { lock.lock(); }
   void release() { lock.unlock(); }

   lock_type& get_lock() { return lock; }

private:
   lock_type& lock;
};

typedef non_shared<lockable<mutex> > lock;

/// instance resource policy
template
<
   typename T,
   class CreationPolicy = new_create<T>
>
class heap_instance {
public:
   typedef T instance_type;
   typedef T* pointer_type;
   typedef CreationPolicy creation_policy;

   static void aquire() { instance = cp.create(); }

   template
   <
      typename U
>
   static void aquire(U u) { instance = cp.create(u); }

   static void release() { cp.destroy(instance); }

   pointer_type get_instance() { return instance_ptr; }

private:
   pointer_type instance_ptr;
   creation_policy cp;
};


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