mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-01-26 12:35:27 +00:00
Common: Add a Lazy type
This commit is contained in:
parent
60ee0b2913
commit
2c7e93f3b8
@ -125,6 +125,7 @@
|
||||
<ClInclude Include="HttpRequest.h" />
|
||||
<ClInclude Include="IniFile.h" />
|
||||
<ClInclude Include="JitRegister.h" />
|
||||
<ClInclude Include="Lazy.h" />
|
||||
<ClInclude Include="LdrWatcher.h" />
|
||||
<ClInclude Include="LinearDiskCache.h" />
|
||||
<ClInclude Include="MathUtil.h" />
|
||||
|
@ -253,6 +253,7 @@
|
||||
<Filter>GL\GLExtensions</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="File.h" />
|
||||
<ClInclude Include="Lazy.h" />
|
||||
<ClInclude Include="LdrWatcher.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
38
Source/Core/Common/Lazy.h
Normal file
38
Source/Core/Common/Lazy.h
Normal file
@ -0,0 +1,38 @@
|
||||
// Copyright 2017 Dolphin Emulator Project
|
||||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
|
||||
namespace Common
|
||||
{
|
||||
// A Lazy object holds a value. If a Lazy object is constructed using
|
||||
// a function as an argument, that function will be called to compute
|
||||
// the value the first time any code tries to access the value.
|
||||
|
||||
template <typename T>
|
||||
class Lazy
|
||||
{
|
||||
public:
|
||||
Lazy() : m_value(T()) {}
|
||||
Lazy(const std::variant<T, std::function<T()>>& value) : m_value(value) {}
|
||||
Lazy(std::variant<T, std::function<T()>>&& value) : m_value(std::move(value)) {}
|
||||
const T& operator*() const { return *ComputeValue(); }
|
||||
const T* operator->() const { return ComputeValue(); }
|
||||
T& operator*() { return *ComputeValue(); }
|
||||
T* operator->() { return ComputeValue(); }
|
||||
private:
|
||||
T* ComputeValue() const
|
||||
{
|
||||
if (!std::holds_alternative<T>(m_value))
|
||||
m_value = std::get<std::function<T()>>(m_value)();
|
||||
return &std::get<T>(m_value);
|
||||
}
|
||||
|
||||
mutable std::variant<T, std::function<T()>> m_value;
|
||||
};
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user