/** * @file XTPSingleton.h * * @copyright * (c) 1998-2025 Codejock Software, All Rights Reserved. * * This source file is the property of Codejock Software and must not be * redistributed by any means without the explicit written permission of * Codejock Software. * * The use of this source code is governed by the terms and conditions specified * in the Toolkit Pro license agreement. Codejock Software grants you, as a * single software developer, the limited right to use this software on one * computer only. * * Contact Information: * support@codejock.com * http://www.codejock.com * */ /** @cond */ #if !defined(__XTPSINGLETON_H__) # define __XTPSINGLETON_H__ /** @endcond */ # if _MSC_VER > 1000 # pragma once # endif // _MSC_VER > 1000 # include "Common/Base/Diagnostic/XTPDisableNoisyWarnings.h" # ifndef __XTPSYSTEMHELPERS_H__ class CXTPDummySingletonDependency; template< class Singleton1 = CXTPDummySingletonDependency, class Singleton2 = CXTPDummySingletonDependency, class Singleton3 = CXTPDummySingletonDependency, class Singleton4 = CXTPDummySingletonDependency, class Singleton5 = CXTPDummySingletonDependency, class Singleton6 = CXTPDummySingletonDependency, class Singleton7 = CXTPDummySingletonDependency, class Singleton8 = CXTPDummySingletonDependency, class Singleton9 = CXTPDummySingletonDependency, class Singleton10 = CXTPDummySingletonDependency> class CXTPSingletonDependencies; template > class CXTPSingleton; # endif /*__XTPSYSTEMHELPERS_H__*/ /** * @brief * Implements Singleton design pattern for a pointer of unspecified * type that is to be allocated, created and destroyed by a client. * Respects thread safety for instance allocation. */ class _XTP_EXT_CLASS CXTPSingletonPointer : IXTPApplicationEvents { public: /** * Creator callback type. */ typedef BOOL(AFX_CDECL* Creator)(void*); /** * Destroyer callback type. */ typedef void(AFX_CDECL* Destroyer)(void*); /** * Initializer callback type. */ typedef void(AFX_CDECL* Initializer)(); /** * @brief * Handles object construction. * @param cbObjectSize Singleton object size in bytes. * @param pfnCreator Creater callback function pointer. * @param pfnDestroyer Destroyer callback function pointer. * @param pfnInitializer Initializer callback function pointer. */ CXTPSingletonPointer(SIZE_T cbObjectSize, Creator pfnCreator, Destroyer pfnDestroyer, Initializer pfnInitializer = NULL); /** * @brief * Handles object destruction. */ virtual ~CXTPSingletonPointer(); /** * @brief * Obtains encapsulated singleton instance pointer. The instance is * allocated and created during the first call. The function guaranties * returning a valid pointer value. If it cannot return it an exception * will be issued. * @return * A pointer to singleton instance. */ void* GetInstance(); /** * @brief * Destroys a previously created and allocated instance and * invalidate the instance pointer. */ void Destroy(); /** * @brief * Obtains reference to singleton access critical secton. * @return * A reference to singleton access critical secton. */ ::CRITICAL_SECTION& GetAccess() const { return m_access; } private: virtual void OnApplicationShutdown(CXTPApplication* pApplication); private: void* m_pInstance; mutable ::CRITICAL_SECTION m_access; const SIZE_T m_cbObjectSize; const Creator m_pfnCreator; const Destroyer m_pfnDestroyer; BOOL m_bDestroyed; }; /** @cond */ class CXTPDummySingletonDependency { }; template AFX_INLINE void XTPInitSingletonInstance(Singleton*) { Singleton::Instance(); } AFX_INLINE void XTPInitSingletonInstance(CXTPDummySingletonDependency*) { // Do nothing. } /** @endcond */ /** * @brief * Describes the list of up to 10 optional singleton dependencies. * If specified, the dependencies gets initialized prior to the * singleton instance initialization in the order of listing. * @param Singleton1 Optional 1st dependency. * @param Singleton2 Optional 2nd dependency. * @param Singleton3 Optional 3rd dependency. * @param Singleton4 Optional 4th dependency. * @param Singleton5 Optional 5th dependency. * @param Singleton6 Optional 6th dependency. * @param Singleton7 Optional 7th dependency. * @param Singleton8 Optional 8th dependency. * @param Singleton9 Optional 9th dependency. * @param Singleton10 Optional 10th dependency. * @see * CXTPSingleton */ template class CXTPSingletonDependencies { public: /** @cond */ static void AFX_CDECL InitDependencies() { // Template overloading approach doesn't work in VC6, // that's why function overloading with dummy pointers is used. XTPInitSingletonInstance(reinterpret_cast(NULL)); XTPInitSingletonInstance(reinterpret_cast(NULL)); XTPInitSingletonInstance(reinterpret_cast(NULL)); XTPInitSingletonInstance(reinterpret_cast(NULL)); XTPInitSingletonInstance(reinterpret_cast(NULL)); XTPInitSingletonInstance(reinterpret_cast(NULL)); XTPInitSingletonInstance(reinterpret_cast(NULL)); XTPInitSingletonInstance(reinterpret_cast(NULL)); XTPInitSingletonInstance(reinterpret_cast(NULL)); XTPInitSingletonInstance(reinterpret_cast(NULL)); } /** @endcond */ }; /** * @brief * Implements Singleton design pattern. Can be used both as an * adaptor or a base class for a class the is not derived from * any other class. Respects thread safety for instance allocation. * @param T Type name of singleton instance. * @param Dependencies Optional list of singleton types listed as * CXTPSingletonDependencies template arguments * on which the current signleton type depends. * If not initialized any of the listed dependencies * will be initialized in the order of appearance in * the list before this singleton instance is created. */ template*/> class CXTPSingleton { public: /** * @brief * Obtains encapsulated singleton instance reference. The instance is * allocated and created during the first call. * @return * A reference to singleton instance. */ static T& AFX_CDECL Instance() { void* pInstance = InstancePointer().GetInstance(); return *reinterpret_cast(pInstance); } private: static BOOL AFX_CDECL Create(void* ptr) { new (ptr) T(); return TRUE; } static void AFX_CDECL Destroy(void* ptr) { reinterpret_cast(ptr)->T::~T(); } protected: /** @cond */ CXTPSingleton() { } static ::CRITICAL_SECTION& AFX_CDECL GetInstanceAccess() { return InstancePointer().GetAccess(); } static CXTPSingletonPointer& AFX_CDECL InstancePointer() { static CXTPSingletonPointer instanceHolder(sizeof(T), Create, Destroy, Dependencies::InitDependencies); return instanceHolder; } /** @endcond */ }; /** @cond */ template class CXTPDerive : public Parent { }; template<> class CXTPDerive { }; /** @endcond */ /** * @brief * Implements Singleton design pattern for a derived class with * possibility of providing a parent class. * @param Derived Type name of singleton instance. * @param Parent Type of a parent class, can be 'void' which means * no parent class is used. */ template class CXTPSingletonBase : public CXTPDerive , public CXTPSingleton { protected: /** * @brief * Handles object construction. */ CXTPSingletonBase() { } }; template struct XTPSingletonValueAdaptor { ValueType Value; }; # include "Common/Base/Diagnostic/XTPEnableNoisyWarnings.h" /** @cond */ #endif /*__XTPSINGLETON_H__*/ /** @endcond */