// Copyright 2017 Dolphin Emulator Project // Licensed under GPLv2+ // Refer to the license.txt file included. #pragma once #include #include #include 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 class Lazy { public: Lazy() : m_value(T()) {} Lazy(const std::variant>& value) : m_value(value) {} Lazy(std::variant>&& value) : m_value(std::move(value)) {} const Lazy& operator=(const std::variant>& value) { m_value = value; return *this; } const Lazy& operator=(std::variant>&& value) { m_value = std::move(value); return *this; } 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(m_value)) m_value = std::get>(m_value)(); return &std::get(m_value); } mutable std::variant> m_value; }; }