Blame view

src/helpers/dsgpatt_Singleton.hh 3.22 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* -*- 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>
ab9d7e65   Benjamin Renard   Fix libraries loc...
38
#include <boost/thread/locks.hpp>
fbe3c2bb   Benjamin Renard   First commit
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

/**

*/

//=============================================================================
// 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_