#pragma once //#include "pdh.h" #include "boost/weak_ptr.hpp" #include "boost/shared_ptr.hpp" #include "rbx/Debug.h" #include "rbx/threadsafe.h" namespace RBX { template class ScopedSingleton { private: static int& initCount() { static int initcount = 0; return initcount; } protected: // use this to validate usage pattern. // some usage patterns will want to check that this doesn't // increase more than 1. static int getInitCount() { return initCount(); } SAFE_STATIC(rbx::spin_mutex, sync) SAFE_STATIC(boost::weak_ptr, s_instance) public: static boost::shared_ptr getInstance() { rbx::spin_mutex::scoped_lock lock(sync()); shared_ptr result = s_instance().lock(); if (!result) { initCount()++; result = shared_ptr(new T()); s_instance() = result; } return result; } static boost::shared_ptr getInstanceOptional() { return s_instance().lock(); } }; }