#ifndef OPENMW_COMPONENTS_MISC_GUARDED_H #define OPENMW_COMPONENTS_MISC_GUARDED_H #include #include namespace Misc { template class Locked { public: Locked(std::mutex& mutex, T& value) : mLock(mutex), mValue(value) {} T& get() const { return mValue.get(); } T* operator ->() const { return std::addressof(get()); } T& operator *() const { return get(); } private: std::unique_lock mLock; std::reference_wrapper mValue; }; template class SharedGuarded { public: SharedGuarded() : mMutex(std::make_shared()) {} SharedGuarded(std::shared_ptr value) : mMutex(std::make_shared()), mValue(std::move(value)) {} Locked lock() const { return Locked(*mMutex, *mValue); } Locked lockConst() const { return Locked(*mMutex, *mValue); } private: std::shared_ptr mMutex; std::shared_ptr mValue; }; } #endif