Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

Singleton< T > Class Template Reference

Class which implements the singleton pattern. More...

#include <singleton.hpp>

Inheritance diagram for Singleton< T >:

Inheritance graph
[legend]
Collaboration diagram for Singleton< T >:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Singleton ()
 Default constructor.
 ~Singleton ()
 Default destructor.

Static Public Member Functions

T & getSingleton ()
 Provides access to instance.
T * getSingletonPtr ()
 Provides access to instance.

Detailed Description

template<typename T>
class Singleton< T >

Class which implements the singleton pattern.

A singleton ensures that only one instance of a class exists at one time. A class is converted to a singleton by deriving it from Singleton and giving the user's class as template parameter.

However unlike most other implementations the instance must be created by the user. It Is not created automatically. It has to be deleted by the user, too. See the example for details.

Access is provided by getSingleton() (returns a reference) or by getSingletonPtr() (returns a pointer).

Now a simple example of the usage:

#include <singleton.hpp>
#include <iostream>

using namespace std;

class Hello : pulbic Singleton<Hello> {
public:
  sayHello() {cout << "Hello\n"};
};

int main() {
  // create singleton
  new Hello();
  // say hello
  Hello::getSingleton().sayHello();
  Hello::getSingletonPtr()->sayHello();
  // clean up
  delete Hello::getSingletonPtr();
  return 0;
}

Definition at line 39 of file singleton.hpp.


Constructor & Destructor Documentation

template<typename T>
Singleton< T >::Singleton  )  [inline]
 

Default constructor.

Definition at line 47 of file singleton.hpp.

template<typename T>
Singleton< T >::~Singleton  )  [inline]
 

Default destructor.

Definition at line 58 of file singleton.hpp.


Member Function Documentation

template<typename T>
T& Singleton< T >::getSingleton  )  [inline, static]
 

Provides access to instance.

Returns:
a reference to the instance
Definition at line 67 of file singleton.hpp.

template<typename T>
T* Singleton< T >::getSingletonPtr  )  [inline, static]
 

Provides access to instance.

Returns:
a pointer to the instance
Definition at line 75 of file singleton.hpp.


The documentation for this class was generated from the following file:
Generated on Fri Dec 17 14:54:32 2004 for Psychological Test Framework by doxygen 1.3.8