CoreLinux++  0.4.32
Singleton.hpp
1 #if !defined(__SINGLETON_HPP)
2 #define __SINGLETON_HPP
3 
4 /*
5  CoreLinux++
6  Copyright (C) 1999 CoreLinux Consortium
7 
8  The CoreLinux++ Library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Library General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12 
13  The CoreLinux++ Library Library is distributed in the hope that it will
14  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Library General Public License for more details.
17 
18  You should have received a copy of the GNU Library General Public
19  License along with the GNU C Library; see the file COPYING.LIB. If not,
20  write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  Boston, MA 02111-1307, USA.
22 */
23 
24 #if !defined(__COMMON_HPP)
25 #include <Common.hpp>
26 #endif
27 
28 namespace corelinux
29 {
37  template< class TypeImpl >
38  class Singleton : public CoreLinuxObject
39  {
40 
41  public:
42 
43  //
44  // Constructors and destructor
45  //
46 
54  Singleton( void ) throw( Assertion )
55  :
57  {
58  REQUIRE( theSingleton == NULLPTR );
59  REQUIRE( theType == NULLPTR );
60  theSingleton = this;
61  theType = new TypeImpl;
62  }
63 
67  Singleton( TypeImpl *aTypePtr ) throw( Assertion )
68  :
70  {
71  ENSURE( aTypePtr != NULLPTR );
72  REQUIRE( theSingleton == NULLPTR );
73  REQUIRE( theType == NULLPTR );
74  theSingleton = this;
75  theType = aTypePtr;
76  }
77 
79 
80  virtual ~Singleton( void )
81  {
82  if( theSingleton == this )
83  {
84  theSingleton = NULLPTR;
85  if( theType != NULLPTR )
86  {
87  delete theType;
88  theType = NULLPTR;
89  }
90  else
91  {
92  ; // do nothing
93  }
94  }
95  else
96  {
97  ; // do nothing
98  }
99  }
100 
101  //
102  // Operator overload
103  //
104 
112  bool operator==( const Singleton & aSingleton ) const
113  {
114  return ( &aSingleton == theSingleton );
115  }
116 
117  //
118  // Accessor
119  //
120 
126  static TypeImpl * instance( void )
127  {
128  return theType;
129  }
130 
131  private:
132 
133  //
134  // Constructor
135  //
141  Singleton( const Singleton & ) throw( Assertion )
142  :
144  {
145  NEVER_GET_HERE;
146  }
147 
148  //
149  // Operator overload
150  //
151 
157  Singleton & operator=( const Singleton & )
158  throw( Assertion )
159  {
160  NEVER_GET_HERE;
161  return (*this);
162  }
163 
164  private:
165 
167 
168  static Singleton<TypeImpl> *theSingleton;
169 
171 
172  static TypeImpl *theType;
173  };
174 
176 
177 template< class TypeImpl >
179 
181 
182 template< class TypeImpl >
183  TypeImpl *Singleton<TypeImpl>::theType( NULLPTR );
184 }
185 
186 #endif // if !defined(__SINGLETON_HPP)
187 
188 /*
189  Common rcs information do not modify
190  $Author: prudhomm $
191  $Revision: 1.1 $
192  $Date: 2000/04/23 20:43:13 $
193  $Locker: $
194 */
195 
static TypeImpl * instance(void)
Returns the instance of the TypeImpl.
Definition: Singleton.hpp:126
Ensure a class only has one instance, and provide a global point of access to it. ...
Definition: Singleton.hpp:38
CoreLinuxObject(void)
Default Constructor.
Definition: CoreLinuxObject.cpp:31
Singleton(TypeImpl *aTypePtr)
Initializing constructor.
Definition: Singleton.hpp:67
Forward reference the various common classes.
Definition: AbstractAllocator.hpp:32
bool operator==(const Singleton &aSingleton) const
Equality operator tests that theSingleton instances are equal.
Definition: Singleton.hpp:112
Singleton(void)
Default constructor sets theSingleton and theType after insuring that they are not already instantiat...
Definition: Singleton.hpp:54
Assertion is-a Exception created when an assertion fails.
Definition: Assertion.hpp:423
virtual ~Singleton(void)
Virtual destructor.
Definition: Singleton.hpp:80
An CoreLinuxObject is a base class for the library.
Definition: CoreLinuxObject.hpp:39

This is the CoreLinux++ reference manual
Provided by The CoreLinux Consortium