1
0
mirror of https://github.com/doitsujin/dxvk.git synced 2024-12-14 09:23:53 +01:00
dxvk/src/util/util_lazy.h
2021-06-29 00:51:20 +02:00

40 lines
601 B
C++

#pragma once
#include <mutex>
namespace dxvk {
/**
* \brief Lazy-initialized object
*
* Creates an object on demand with
* the given constructor arguments.
*/
template<typename T>
class Lazy {
public:
template<typename... Args>
T& get(Args... args) {
if (m_object)
return *m_object;
std::lock_guard lock(m_mutex);
if (!m_object) {
m_object = std::make_unique<T>(
std::forward<Args>(args)...);
}
return *m_object;
}
private:
dxvk::mutex m_mutex;
std::unique_ptr<T> m_object;
};
}