Krita Source Code Documentation
Loading...
Searching...
No Matches
KisLazyStorage.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2018 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#ifndef KISLAZYSTORAGE_H
8#define KISLAZYSTORAGE_H
9
10#include <functional>
11#include <memory>
12#include <atomic>
13#include <mutex>
14
15#include "KisMpl.h"
16
23template <typename T, typename... Args>
25{
26public:
27 struct init_value_tag { explicit init_value_tag() = default; };
28
34 explicit KisLazyStorage(Args... args)
35 : m_constructionArgs(std::forward<Args>(args)...),
36 m_data(0)
37 {
38 }
39
49 : m_data(new T(std::forward<T>(value)))
50 {
51 }
52
53 KisLazyStorage(const KisLazyStorage &rhs) = delete;
55
57 *this = std::move(rhs);
58 }
59
61 std::scoped_lock lock(m_mutex, rhs.m_mutex);
62
63 m_constructionArgs = std::move(rhs.m_constructionArgs);
64 delete m_data.load();
65 m_data.store(rhs.m_data.load());
66 rhs.m_data.store(0);
67
68 return *this;
69 }
70
72 delete m_data.load();
73 }
74
76 return getPointer();
77 }
78
79 T& operator*() {
80 return *getPointer();
81 }
82
83private:
85 if(!m_data) {
86 std::unique_lock l(m_mutex);
87 if(!m_data) {
89 }
90 }
91 return m_data;
92 }
93
94 static inline T* constructObject(Args... args) {
95 return new T(std::forward<Args>(args)...);
96 }
97
98private:
99 std::tuple<Args...> m_constructionArgs;
100 std::atomic<T*> m_data;
101 std::mutex m_mutex;
102};
103
104#endif // KISLAZYSTORAGE_H
float value(const T *src, size_t ch)
KisLazyStorage & operator=(const KisLazyStorage &rhs)=delete
std::atomic< T * > m_data
static T * constructObject(Args... args)
std::tuple< Args... > m_constructionArgs
KisLazyStorage(KisLazyStorage &&rhs)
KisLazyStorage(Args... args)
KisLazyStorage(init_value_tag, T &&value)
KisLazyStorage(const KisLazyStorage &rhs)=delete
KisLazyStorage & operator=(KisLazyStorage &&rhs)
std::mutex m_mutex