1
0
mirror of https://github.com/doitsujin/dxvk.git synced 2024-12-12 13:08:50 +01:00

[util] Add Singleton helper

This commit is contained in:
Philip Rebohle 2023-01-23 17:17:43 +01:00
parent 255ab1a63c
commit d4143429c4

43
src/util/util_singleton.h Normal file
View File

@ -0,0 +1,43 @@
#pragma once
#include "rc/util_rc_ptr.h"
#include "thread.h"
namespace dxvk {
/**
* \brief Singleton helper
*
* Class that manages a dynamically created
*/
template<typename T>
class Singleton {
public:
Rc<T> acquire() {
std::lock_guard lock(m_mutex);
if (!(m_useCount++))
m_object = new T();
return m_object;
}
void release() {
std::lock_guard lock(m_mutex);
if (!(--m_useCount))
m_object = nullptr;
}
private:
dxvk::mutex m_mutex;
size_t m_useCount = 0;
Rc<T> m_object = nullptr;;
};
}