From d4143429c43d3492c896bb9a5a1b7e4a87804b7e Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Mon, 23 Jan 2023 17:17:43 +0100 Subject: [PATCH] [util] Add Singleton helper --- src/util/util_singleton.h | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/util/util_singleton.h diff --git a/src/util/util_singleton.h b/src/util/util_singleton.h new file mode 100644 index 00000000..08cd1562 --- /dev/null +++ b/src/util/util_singleton.h @@ -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 +class Singleton { + +public: + + Rc 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 m_object = nullptr;; + +}; + +}