Skip to content
Snippets Groups Projects
ISingleton.h 2.03 KiB
Newer Older
  • Learn to ignore specific revisions
  • // ************************************************************************** //
    
    //  BornAgain: simulate and fit scattering at grazing incidence
    //
    
    //! @file      Tools/inc/ISingleton.h
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
    //! @brief     Defines the standard mix-in ISingleton.
    
    //! @homepage  http://apps.jcns.fz-juelich.de/BornAgain
    
    //! @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>
    
    #include <typeinfo>
    
    GCC_DIAG_OFF(strict-aliasing);
    
    #include <boost/thread.hpp>
    
    GCC_DIAG_ON(strict-aliasing);
    
    //! @class ISingleton
    //! @ingroup tools_internal
    //! @brief Singleton pattern.
    
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        static T& instance()
    
            static boost::mutex single_mutex;
            boost::unique_lock<boost::mutex> single_lock( single_mutex );
    
            if( !m_instance) {
                if( m_destroyed ) {
                    onDeadReference();
                } else {
    
                    CreateSingleton();
    
        ISingleton(){}
    
        virtual ~ISingleton()
        {
            m_instance = 0;
            m_destroyed = true;
        }
    
    
        static void CreateSingleton()
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
            m_instance =& theInstance;
    
        static void onDeadReference() { throw std::runtime_error("ISingleton::onDeadReference()"); }
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
        ISingleton(const ISingleton<T>& ) {}
        ISingleton& operator=(const ISingleton<T>& ) { throw std::runtime_error("ISingleton::operator=()"); }
    
        static T_Pointer m_instance;
        static bool m_destroyed;
    
    Van Herck, Walter's avatar
    Van Herck, Walter committed
    template<class T> typename ISingleton<T>::T_Pointer ISingleton<T>::m_instance = 0;
    template<class T> bool ISingleton<T>::m_destroyed = false;