2017-06-24 10:41:13 -06:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-06-24 10:41:13 -06:00
|
|
|
|
|
|
|
#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)) {}
|
2017-08-02 10:16:56 -06:00
|
|
|
const Lazy<T>& operator=(const std::variant<T, std::function<T()>>& value)
|
|
|
|
{
|
|
|
|
m_value = value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const Lazy<T>& operator=(std::variant<T, std::function<T()>>&& value)
|
|
|
|
{
|
|
|
|
m_value = std::move(value);
|
|
|
|
return *this;
|
|
|
|
}
|
2017-06-24 10:41:13 -06:00
|
|
|
const T& operator*() const { return *ComputeValue(); }
|
|
|
|
const T* operator->() const { return ComputeValue(); }
|
|
|
|
T& operator*() { return *ComputeValue(); }
|
|
|
|
T* operator->() { return ComputeValue(); }
|
2018-04-12 06:18:04 -06:00
|
|
|
|
2017-06-24 10:41:13 -06:00
|
|
|
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;
|
|
|
|
};
|
2019-05-05 17:48:12 -06:00
|
|
|
} // namespace Common
|