Krita Source Code Documentation
Loading...
Searching...
No Matches
KisValueCache.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#ifndef KISVALUECACHE_H
8#define KISVALUECACHE_H
9
10#include <optional>
11
17template <typename Initializer>
18struct KisValueCache : Initializer
19{
20 using value_type = decltype(std::declval<Initializer>().initialize());
21
22 template <typename ...Args>
23 KisValueCache(Args... args)
24 : Initializer(args...)
25 {
26 }
27
28 const value_type& value() {
29 if (!m_value) {
30 m_value = Initializer::initialize();
31 }
32
33 return *m_value;
34 }
35
36 operator const value_type&() const {
37 return value();
38 }
39
40 bool isValid() const {
41 return bool(m_value);
42 }
43
44 void clear() {
45 m_value = std::nullopt;
46 }
47
48private:
49 std::optional<value_type> m_value;
50};
51
52#endif // KISVALUECACHE_H
decltype(std::declval< Initializer >().initialize()) value_type
const value_type & value()
bool isValid() const
KisValueCache(Args... args)
std::optional< value_type > m_value