Newer
Older
// ************************************************************************** //

Wuttke, Joachim
committed
//
// BornAgain: simulate and fit scattering at grazing incidence
//
//! @file Tools/inc/ISingleton.h

Wuttke, Joachim
committed
//!
//! @homepage http://apps.jcns.fz-juelich.de/BornAgain

Wuttke, Joachim
committed
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2013
//! @authors Scientific Computing Group at MLZ Garching
//! @authors C. Durniak, G. Pospelov, W. Van Herck, J. Wuttke
//
// ************************************************************************** //
#ifndef ISINGLETON_H
#define ISINGLETON_H
#include <stdexcept>
#include <iostream>
Pospelov, Gennady
committed
#include "Macros.h"
GCC_DIAG_OFF(strict-aliasing);
#include <boost/thread.hpp>
GCC_DIAG_ON(strict-aliasing);
Pospelov, Gennady
committed
//! @class ISingleton
//! @ingroup tools_internal
//! @brief Singleton pattern.
template <class T>
class ISingleton
{
Van Herck, Walter
committed
public:
{
static boost::mutex single_mutex;
boost::unique_lock<boost::mutex> single_lock( single_mutex );
if( !m_instance) {
if( m_destroyed ) {
onDeadReference();
} else {
}
}
return *m_instance;
}
Van Herck, Walter
committed
protected:
virtual ~ISingleton()
{
m_instance = 0;
m_destroyed = true;
}
{
static T theInstance;
}
static void onDeadReference() { throw std::runtime_error("ISingleton::onDeadReference()"); }
typedef T* T_Pointer;
Van Herck, Walter
committed
private:
ISingleton(const ISingleton<T>& ) {}
ISingleton& operator=(const ISingleton<T>& ) { throw std::runtime_error("ISingleton::operator=()"); }
static T_Pointer m_instance;
static bool m_destroyed;
};
template<class T> typename ISingleton<T>::T_Pointer ISingleton<T>::m_instance = 0;
template<class T> bool ISingleton<T>::m_destroyed = false;
#endif // ISINGLETON_H