dsgpatt_Singleton.hh 3.22 KB
/* -*- Base: 10 ; Mode: C++ -*- */
/*------------------------------------------------------------------------
				     **	
  				FOST project
				     **
--------------------------------------------------------------------------
--------------------------------------------------------------------------
   				FILE LOG
		$Revision: 1.2 $    $Date: 2011-06-23 15:46:09 $
--------------------------------------------------------------------------
CREATION
	F.CASIMIR

SUMMARY

DESCRIPTION

	The main function performs the following actions :
	<ul>
	<li>
	</ul>

------------------------------------------------------------------------*/

//=============================================================================
// 
// History of code
//
// creation 
//
// modification 
//=============================================================================

#ifndef _PATTERN_SINGLETON_HH_
#define _PATTERN_SINGLETON_HH_ 1

#include <boost/thread/mutex.hpp>
#include <boost/thread/locks.hpp>

/**

*/

//=============================================================================
// Class Singleton
//=============================================================================

/*------------------------------------------------------------------------
CLASS

KEYWORDS


DESCRIPTION
	

------------------------------------------------------------------------*/

template< class T >
class Singleton
{

public:

  /**
     @return the only instance of C object
  */
  static T* getInstance(void);

  /**
   */
  static void releaseInstance();

protected:

  /**
     Constructor (default one)
  */
  Singleton() {};
  
  /**
     Copy Constructor (normally should not be used)
  */
  Singleton(const Singleton& copied) {};
  
  /**
     Destructor
  */
  virtual ~Singleton() {};
  
  // Get Methods
  
  // Set Methods
  
  // Other Methods
  
  //FIELDS

private:
  // Static current instance
  static T* _singleton;
  static boost::mutex _pointerAccessMutex;
};

//=============================================================================
// Inline Methods for Class Singleton
//=============================================================================

// Initialisation
template <typename T> T* Singleton<T>::_singleton = 0;
template <typename T> boost::mutex Singleton<T>::_pointerAccessMutex;

//-----------------------------------------------------------------------------
// Get Methods

//-----------------------------------------------------------------------------
// Set Methods

//-----------------------------------------------------------------------------
// Other Methods
template< class T >
T* Singleton<T>::getInstance(void)
{
  if( _singleton==0 )
    {
	  boost::lock_guard<boost::mutex> lock(_pointerAccessMutex);
      if( _singleton==0 )
	{
      _singleton = new T;     
    }
    }
  return _singleton;
}

template< class T >
void Singleton<T>::releaseInstance()
{
  if ( _singleton!=0 )
    {
	  boost::lock_guard<boost::mutex> lock(_pointerAccessMutex);
      if ( _singleton!=0 )
	{
      delete _singleton ;
      _singleton=0;
	}
    }
}

//=============================================================================
// End of Class Singleton
//=============================================================================

#endif // _PATTERN_SINGLETON_HH_