template <class T>class CORESHARED_EXPORT CSingleton{public: static T& instance(); void free();protected: virtual ~CSingleton(); CSingleton();private: static T* _instance; static int _refCount;};template <class T> T * CSingleton<T>::_instance = NULL;template <class T> int CSingleton<T>::_refCount = 0;template <class T>CSingleton<T>::CSingleton(){}template <class T>CSingleton<T>::~CSingleton(){ _instance = NULL;}template <class T>T &CSingleton<T>::instance(){ if(!_instance) _instance = new T; _refCount++; return _instance;}template <class T>void CSingleton<T>::free(){ if(--_refCount==0) delete this;}
class CORESHARED_EXPORT CObjectStorageManager : //public CSingleton<CObjectStorageManager>, public CManagerInterface, public CXMLDocumentItemInterface, public CMMNCadImportInterface{Q_OBJECTpublic: explicit CObjectStorageManager(QObject *parent = 0); ~CRailObjectStorageManager();};....
//public CSingleton<CObjectStorageManager>,
class CORESHARED_EXPORT CObjectStorageManager : public CManagerInterface, public CXMLDocumentItemInterface, public CMMNCadImportInterface{Q_OBJECTCObjectFactoryLoader * fmodelLoader; //указатель на другой менеджер, который будет использоваться этимprotected: void enumerateEntities(); void detachEntities();public: static const QString SHARED_SPACE_NAME;};
const QString CObjectStorageManager::SHARED_SPACE_NAME = "storage_manager"; //это ключ, по которому будем искать указатель на этот менеджерCObjectStorageManager::CObjectStorageManager(QObject *parent) : CManagerInterface(parent), CXMLDocumentItemInterface(), CMMNCadImportInterface(), fmodelLoader(NULL){}void CObjectStorageManager::enumerateEntities() //находим нужные нам менеджеры{ fmodelLoader = dynamic_cast<CObjectFactoryLoader*>(getSharedInstance(CObjectFactoryLoader::SHARED_SPACE_NAME));}void CObjectStorageManager::detachEntities(){ fmodelLoader = NULL;}
//конструктор главного окнаfsharedSpace->registerObject(CObjectStorageManager::SHARED_SPACE_NAME,fstorageManager);fsharedSpace->registerObject(CObjectFactoryLoader::SHARED_SPACE_NAME,fmodelLoader);
C++ (Qt)struct CBaseManager;typedef CBaseManager * (*TCreateFunc)( void ); struct CBaseManager { static bool AddManager( const QString & name, TCreateFunc func ); static CBaseManager * GetManagerByName( const QString & name ); virtual QString GetName( void ) const = 0; void Release( void ); private: ~CBaseManager( void ); QAtomicInt mNumRef; typedef QPair <CBaseManager *, TCreateFunc> TPair; static QMap <QString, TPair> mMap;};
C++ (Qt)#include <QtWidgets> QHash <int, QPointer<QObject> > theHash; template <class T>QSharedPointer<T> AcquireResource( void ){ T * ptr = 0; int id = qMetaTypeId<T> (); QObject * obj = theHash.value(id); if (obj) ptr = dynamic_cast<T *> (obj); else { ptr = static_cast <T *> (QMetaType::create(id)); theHash[id] = ptr; } return QSharedPointer<T> (ptr);} //-------------------- test ------------------------ struct CTest : public QObject { CTest( void ) : mA(5) { qDebug() << "construct"; } CTest( const CTest & src ) { mA = src.mA; qDebug() << "construct copy"; } ~CTest( void ) { qDebug() << "destroy"; } int mA;}; Q_DECLARE_METATYPE(CTest) int main(int, char **){ for (int i = 0; i < 2; ++i) { QSharedPointer<CTest> p = AcquireResource<CTest>(); qDebug() << p->mA; } return 0;}
C++ (Qt)Object *o1 = manager.instance<Object>(); // Вернет указатель на первый попавшийся объект класса Object или его наследников.Object *o2 = manager.instance<Object>( "Obj2" ); // Тоже, только с именем Obj2Storage *store = manager.instance<Storage>();
C++ (Qt)#include <QtWidgets> QHash <const char *, QWeakPointer <int> > theHash; template <class T>QSharedPointer<T> AcquireResource( void ){ const char * key = typeid(T).name(); typedef QWeakPointer <T> TWeakPtr; typedef QSharedPointer <T> TSharedPtr; TWeakPtr & weakPtr = (TWeakPtr &) theHash[key]; TSharedPtr sharedPtr = weakPtr.toStrongRef(); if (sharedPtr.isNull()) { sharedPtr = TSharedPtr(new T); weakPtr = sharedPtr; } return sharedPtr;} //-------------------- test ------------------------ struct CTest { CTest( void ) : mA(5) { qDebug() << "construct CTest"; } ~CTest( void ) { qDebug() << "destroy CTest"; } int mA;}; struct CTest2 : public QObject { CTest2( void ) : mB(6) { qDebug() << "construct CTest2"; } ~CTest2( void ) { qDebug() << "destroy CTest2"; } int mB;}; int main(int, char **){ if (1) { QSharedPointer<CTest2> test = AcquireResource<CTest2>(); test->mB = 7; qDebug() << "test" << test.data() << test->mB; for (int i = 0; i < 2; ++i) { QSharedPointer<CTest> p = AcquireResource<CTest>(); qDebug() << "p" << p.data() << p->mA; QSharedPointer<CTest2> p2 = AcquireResource<CTest2>(); qDebug() << "p2" << p2.data() << p2->mB; } } return 0;}