/*****************************************************************************
 * $LastChangedDate: 2011-04-09 21:58:06 -0400 (Sat, 09 Apr 2011) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   Singleton base class.
 *//*
 * LEGAL:   COPYRIGHT (C) 2010 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#ifndef BASE_SINGLETON_HH
#define BASE_SINGLETON_HH 1

namespace base {

////////////////////////////////////////////////////////////////////////////////
/// @brief Singleton base class.
///
class Singleton
{
PREVENT_COPYING(Singleton)
protected:
    Singleton( void ) { }
    ~Singleton() { }

#if COMPILE_THREADS
#   define AUTO_LOCK_GET_INSTANCE AutoLock<RushLock> autolock( &msGetInstanceLock )
#   define AUTO_LOCK_SINGLETON    AutoLock<FastLock> autolock( &msSingletonLock )
#else
#   define AUTO_LOCK_GET_INSTANCE
#   define AUTO_LOCK_SINGLETON
#endif

#define DEFINE_GetInstance( CLASS )         \
    CLASS_METHOD CLASS& GetInstance( void ) \
    {                                       \
    AUTO_LOCK_GET_INSTANCE;                 \
        if ( UX( msInstance == NULL ) )     \
            msInstance = new CLASS;         \
        return *msInstance;                 \
    }

#define DECLARE_SINGLETON_CLASS_VARS( CLASS ) \
    CLASS_VAR CLASS* msInstance;

#define DEFINE_SINGLETON_CLASS_VARS( CLASS ) \
    CLASS* CLASS::msInstance;

};

} // namespace base

#endif // BASE_SINGLETON_HH
