#pragma once // qt #include #include #define SINGLETON(type) Singleton::get().data() template class Singleton { public: static QSharedPointer get() { static QMutex mtx; QMutexLocker l(&mtx); if (!m_instance) { m_instance.reset( static_cast(T::staticMetaObject.newInstance()) // FIXME: deleteLater doesn't work. Crashes without it , &T::deleteLater ); } return m_instance; } static void cleanup() { static QMutex mtx; QMutexLocker l(&mtx); m_instance.clear(); } typedef QSharedPointer FactorySharedPointer; private: static FactorySharedPointer m_instance; }; template typename Singleton::FactorySharedPointer Singleton::m_instance;