Loading...
Searching...
No Matches
Singleton.h
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// Singleton.h
//------------------------------------------------------------------------------
// Copyright (C) 1997-2002,2005 Vladislav Grinchenko
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
// Created: 02/22/99
//------------------------------------------------------------------------------
#ifndef _Singleton_h
#define _Singleton_h
#include "Destroyer.h"
namespace ASSA {
template <class T>
class Singleton
{
public:
static T* get_instance () {
if (m_instance == 0) {
m_instance = new T;
m_destroyer.setGuard (m_instance);
}
return m_instance;
}
protected:
Singleton() {}
friend class Destroyer<T>;
virtual ~Singleton () {}
private:
static T* m_instance;
static Destroyer<T> m_destroyer;
};
} // end namespace ASSA
#define ASSA_DECL_SINGLETON(K) \
template <> K* ASSA::Singleton<K>::m_instance = NULL; \
template <class T> ASSA::Destroyer<T> ASSA::Singleton<T>::m_destroyer; \
template ASSA::Destroyer<K> ASSA::Singleton<K>::m_destroyer;
#endif
Destroyer.h
Destroyer is the helper class for class Singleton.
ASSA::AutoPtrRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
ASSA::Destroyer
Definition Destroyer.h:25
ASSA::Singleton
Definition Singleton.h:43
ASSA::Singleton::get_instance
static T * get_instance()
Return an instance of templated class T.
Definition Singleton.h:47
ASSA::Singleton::m_destroyer
static Destroyer< T > m_destroyer
Destroyer that owns object T.
Definition Singleton.h:69
ASSA::Singleton::~Singleton
virtual ~Singleton()
Virtual Destructor.
Definition Singleton.h:62
ASSA::Singleton::Singleton
Singleton()
Protected Constructor.
Definition Singleton.h:57
ASSA::Singleton::m_instance
static T * m_instance
Pointer to the object T instance.
Definition Singleton.h:66
ASSA
Definition Acceptor.h:40