Krita Source Code Documentation
Loading...
Searching...
No Matches
KisLazySharedCacheStorage.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2021 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#ifndef KISLAZYSHAREDCACHESTORAGE_H
8#define KISLAZYSHAREDCACHESTORAGE_H
9
10#include <type_traits>
11#include <functional>
12#include <utility>
13
14#include <QMutex>
15#include <QAtomicPointer>
16#include <QSharedPointer>
17#include <QSharedDataPointer>
18#include <QExplicitlySharedDataPointer>
19
21{
22
27template <typename StorageType, typename T, typename... Args>
29{
30 using ConstType = std::add_const_t<T>;
31 using FactoryType = T*(Args...);
32
35 DataStorage(const DataStorage &rhs) = default;
36 DataStorage& operator=(const DataStorage &rhs) = default;
37
38 ConstType* lazyInitialize(const std::function<FactoryType> &factory, Args... args) {
39 if (!m_value) {
40 m_value.reset(factory(std::forward<Args...>(args...)));
41 }
42 return m_value.data();
43 }
44
45 bool hasValue() const {
46 return !m_value.isNull();
47 }
48
49 void reset() {
50 m_value.reset();
51 }
52
53private:
54 StorageType m_value;
55};
56
57template <typename T, typename... Args>
59
60
66template <typename T, typename... Args>
68{
69 using ConstType = std::add_const_t<T>;
70 using FactoryType = T*(Args...);
71
72private:
74 {
75 using ConstType = std::add_const_t<T>;
76
79 SharedStorage(const SharedStorage &rhs) = delete;
80
83 };
84public:
85
86
89 DataWrapperShared(const DataWrapperShared &rhs) = default;
91
92 ConstType* lazyInitialize(const std::function<FactoryType> &factory, Args... args) {
93 QMutexLocker l(&m_sharedStorage->sharedMutex);
94 return m_sharedStorage->m_value.lazyInitialize(factory, std::forward<Args...>(args...));
95 }
96
97 bool hasValue() const {
98 QMutexLocker l(&m_sharedStorage->sharedMutex);
99 return m_sharedStorage->m_value.hasValue();
100 }
101
102 void reset() {
103 m_sharedStorage.reset(new SharedStorage());
104 }
105
106private:
108};
109
110}
111
112template <typename DataWrapper, typename T, typename... Args>
114{
115public:
116 using ConstType = std::add_const_t<T>;
117 using FactoryType = T*(Args...);
118
119public:
123
124 KisLazySharedCacheStorageBase(std::function<FactoryType> factory)
125 : m_factory(factory)
126 {
127 }
128
135
136 void setFactory(std::function<FactoryType> factory) {
137 m_factory = factory;
138 }
139
140 ConstType* value(Args... args) {
141 ConstType *result = 0;
142
143 if (m_cachedValue) {
144 result = m_cachedValue;
145 } else {
146 QMutexLocker l1(&m_mutex);
147 m_cachedValue = m_dataWrapper.lazyInitialize(m_factory, args...);
148 result = m_cachedValue;
149 }
150 return result;
151 }
152
153 bool isNull() const {
154 return !bool(m_cachedValue) && !m_dataWrapper.hasValue();
155 }
156
157 void initialize(Args... args) {
158 (void) value(args...);
159 }
160
161 void reset() {
162 QMutexLocker l(&m_mutex);
163 m_cachedValue.storeRelaxed(nullptr);
164 m_dataWrapper.reset();
165 }
166
167private:
168 std::function<FactoryType> m_factory;
169 DataWrapper m_dataWrapper;
170 QAtomicPointer<ConstType> m_cachedValue;
171 mutable QMutex m_mutex;
172};
173
188template <typename T, typename... Args>
192
204template <typename T, typename... Args>
208
209
210#endif // KISLAZYSHAREDCACHESTORAGE_H
float value(const T *src, size_t ch)
KisLazySharedCacheStorageBase(std::function< FactoryType > factory)
void setFactory(std::function< FactoryType > factory)
QAtomicPointer< ConstType > m_cachedValue
KisLazySharedCacheStorageBase(const KisLazySharedCacheStorageBase &rhs)
std::function< FactoryType > m_factory
typedef void(QOPENGLF_APIENTRYP PFNGLINVALIDATEBUFFERDATAPROC)(GLuint buffer)
DataStorage(const DataStorage &rhs)=default
DataStorage & operator=(const DataStorage &rhs)=default
ConstType * lazyInitialize(const std::function< FactoryType > &factory, Args... args)
DataStorage< QScopedPointer< ConstType >, T, Args... > m_value
DataWrapperShared & operator=(const DataWrapperShared &rhs)=default
ConstType * lazyInitialize(const std::function< FactoryType > &factory, Args... args)
DataWrapperShared(const DataWrapperShared &rhs)=default