File indexing completed on 2024-05-19 04:25:10
0001 /* 0002 * SPDX-FileCopyrightText: 2023 Dmitry Kazakov <dimula73@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #ifndef KISVALUECACHE_H 0008 #define KISVALUECACHE_H 0009 0010 #include <optional> 0011 0012 /** 0013 * A simple class for cache-like structures. It should be parametrized 0014 * with a policy-class that has `value_type initialize()` method, which 0015 * would be used for initializing the cache. 0016 */ 0017 template <typename Initializer> 0018 struct KisValueCache : Initializer 0019 { 0020 using value_type = decltype(std::declval<Initializer>().initialize()); 0021 0022 template <typename ...Args> 0023 KisValueCache(Args... args) 0024 : Initializer(args...) 0025 { 0026 } 0027 0028 const value_type& value() { 0029 if (!m_value) { 0030 m_value = Initializer::initialize(); 0031 } 0032 0033 return *m_value; 0034 } 0035 0036 operator const value_type&() const { 0037 return value(); 0038 } 0039 0040 bool isValid() const { 0041 return bool(m_value); 0042 } 0043 0044 void clear() { 0045 m_value = std::nullopt; 0046 } 0047 0048 private: 0049 std::optional<value_type> m_value; 0050 }; 0051 0052 #endif // KISVALUECACHE_H