/* $Id: MutexPtrIntermedRep.h,v 1.2 1999/09/09 15:50:42 v2kea318 Exp $

$Log: MutexPtrIntermedRep.h,v $
 * Revision 1.2  1999/09/09  15:50:42  v2kea318
 * Use the thread-safe version of the Pointer class.
 *
 * Revision 1.1  1999/09/08  20:47:26  v2kea318
 * Initial revision
 *
***************************************************************************

Define an intermediate representation of a Mutex which is implemented
as a smart pointer to some native type.  There are no public methods as
this is simply specializing the Mutex class a little for the true
implementations by classes derived from this one.

***************************************************************************/

#ifndef MLIBSPP_MUTEXPTRINTERMEDREP_H
#define MLIBSPP_MUTEXPTRINTERMEDREP_H

#include "Mutex.h"
#include "pointer.h"

template <class NativeRep>
class MutexPtrIntermedRep : public Mutex {
  // define a type for a smart pointer to the native rep.
  typedef Pointer<NativeRep> MutexPtr;

protected:
  // constructors.
  MutexPtrIntermedRep() : Mutex(), mutex(new NativeRep, true) {}
  MutexPtrIntermedRep(const MutexPtrIntermedRep& i) 
    : Mutex(i), mutex(i.mutex) {}

  // assignment operator.
  MutexPtrIntermedRep& operator = (const MutexPtrIntermedRep& i)
    {
      if (this != &i) {
	Mutex::operator = (i);
	mutex = i.mutex;
      }
      return *this;
    }

  // we destroy the mutex when there is only 1 reference.
  bool os_do_destroy() { return mutex.count() == 1; }

private:
  MutexPtr mutex;

protected:
  NativeRep* ptr_get() { return mutex.get(); }
  void ptr_release() { mutex.release(); }
};

#endif
