mirror of
https://github.com/EduApps-CDG/OpenDX
synced 2024-12-30 09:45:37 +01:00
[util] Add helper for lazy initialization
a
This commit is contained in:
parent
23379b6b9c
commit
9a2da555c0
40
src/util/util_lazy.h
Normal file
40
src/util/util_lazy.h
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#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:
|
||||||
|
|
||||||
|
std::mutex m_mutex;
|
||||||
|
std::unique_ptr<T> m_object;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user