Skip to content

Instantly share code, notes, and snippets.

@f0ster
Created Jul 8, 2014
Embed
What would you like to do?
#ifndef _Cing_Singleton_H_
#define _Cing_Singleton_H_
/**
* @internal
* @file Singleton related utilities
*/
#ifndef NULL
#define NULL 0
#endif
/**
* @internal
* @brief Base class to create static singletons. This means that its creation/destruction cycle
* is not controlled. If you need to control the creation/destruction of the singleton use
* the Singleton class.
*/
template < class T >
class SingletonStatic
{
public:
virtual ~SingletonStatic() { m_bIsValid = false; }
/**
* @brief Returns a reference to the singleton instance
* @return a reference to the singleton instance
*/
inline static T &getSingleton() { return m_singleton; }
/**
* @brief Returns a pointer to the singleton instance
* @return a reference to the singleton instance
*/
inline static T *getSingletonPtr() { return m_bIsValid? &m_singleton: NULL; }
private:
static T m_singleton; ///< Singleton
static bool m_bIsValid;
};
// Singleton definition
template < class T >
T SingletonStatic<T>::m_singleton;
template < class T >
bool SingletonStatic<T>::m_bIsValid = true;
/**
* @internal
* @brief Base class to create controlled singletons. In this class the creation and destruction of
* the singleton are controlled. The creation occurs with the first getSingleton call, and the
* destruction when the method destroySingleton is called
*/template < class T >
class Singleton
{
public:
virtual ~Singleton() { }
/**
* @brief Returns a reference to the singleton instance
* The first call will create the singleton
* @return a reference to the singleton instance
*/
inline static T &getSingleton() {
if ( !m_singleton ){
m_singleton = new T();
}
return *m_singleton;
}
/**
* @brief Returns a pointer to the singleton instance
* The first call will create the singleton
* @return a pointer to the singleton instance
*/
inline static T *getSingletonPtr() {
if ( !m_singleton ){
m_singleton = new T();
}
return m_singleton;
}
/// To destroy the singleton
void static destroySingleton() { if(m_singleton) {delete m_singleton; m_singleton = 0; } }
private:
static T *m_singleton; ///< Singleton
};
//Singleton
template < class T >
T *Singleton<T>::m_singleton = 0;
#endif // _Singleton_H_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment