Jamba C++ API 7.5.0
Loading...
Searching...
No Matches
ExpiringDataCache.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 pongasoft
3 *
4 * Licensed under the Apache License, Version 2.0 or the MIT license,
5 * at your option. You may not use this file except in compliance with
6 * one of these licenses. You may obtain copies of the licenses at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 * https://opensource.org/licenses/MIT
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 *
17 * @author Yan Pujante
18 */
19
20#ifndef JAMBA_EXPIRINGDATACACHE_H
21#define JAMBA_EXPIRINGDATACACHE_H
22
23#include <pongasoft/VST/Timer.h>
24#include <functional>
25
26#include <pongasoft/logging/logging.h>
27
29
30using namespace Steinberg;
31
71template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
72class ExpiringDataCache : ITimerCallback
73{
74public:
75 using value_type = T;
76 using pointer = Ptr;
77 using loader = Loader;
78
79public:
81 ExpiringDataCache() = default;
82
84 ExpiringDataCache(Loader iDataLoader, uint32 iTimeToLiveMilliseconds) :
85 fDataLoader{std::move(iDataLoader)}, fTimeToLiveMilliseconds{iTimeToLiveMilliseconds} {}
86
90
93 fDataLoader{std::move(iOther.fDataLoader)}, fTimeToLiveMilliseconds{iOther.fTimeToLiveMilliseconds} {}
94
97 {
98 fDataLoader = iOther.fDataLoader;
99 fTimeToLiveMilliseconds = iOther.fTimeToLiveMilliseconds;
100 fCachedData = nullptr;
101 fTimer = nullptr;
102
103 return *this;
104 }
105
108 {
109 fDataLoader = std::move(iOther.fDataLoader);
110 fTimeToLiveMilliseconds = iOther.fTimeToLiveMilliseconds;
111 fCachedData = nullptr;
112 fTimer = nullptr;
113
114 return *this;
115 }
116
118 explicit operator bool() const noexcept { return fDataLoader ? true : false; }
119
128 {
129 if(fCachedData)
130 {
131 // resets the timer
133 return fCachedData;
134 }
135
136 fCachedData = fDataLoader ? fDataLoader() : nullptr;
137
138 if(fCachedData)
140
141 return fCachedData;
142 }
143
144private:
145 // Callback when the timer expires => remove cached data
146 void onTimer(Timer *timer) override
147 {
148 fCachedData = nullptr;
149 fTimer = nullptr;
150 }
151
152private:
153 Loader fDataLoader{};
155
157 std::unique_ptr<AutoReleaseTimer> fTimer{};
158};
159
160}
161
162#endif //JAMBA_EXPIRINGDATACACHE_H
static std::unique_ptr< AutoReleaseTimer > create(Steinberg::ITimerCallback *iCallback, Steinberg::uint32 iIntervalMilliseconds)
Creates and return an auto release timer.
Definition Timer.h:55
Loader loader
Definition ExpiringDataCache.h:77
ExpiringDataCache & operator=(ExpiringDataCache const &iOther) noexcept
Copy assignment operator.
Definition ExpiringDataCache.h:96
Ptr getData()
Main api to retrieve the data.
Definition ExpiringDataCache.h:127
ExpiringDataCache & operator=(ExpiringDataCache &&iOther) noexcept
Move assignment operator.
Definition ExpiringDataCache.h:107
void onTimer(Timer *timer) override
Definition ExpiringDataCache.h:146
Loader fDataLoader
Definition ExpiringDataCache.h:153
ExpiringDataCache(Loader iDataLoader, uint32 iTimeToLiveMilliseconds)
Main constructor with loader and TTL.
Definition ExpiringDataCache.h:84
ExpiringDataCache()=default
Default empty constructor => getData() always return nullptr.
std::unique_ptr< AutoReleaseTimer > fTimer
Definition ExpiringDataCache.h:157
uint32 fTimeToLiveMilliseconds
Definition ExpiringDataCache.h:154
ExpiringDataCache(ExpiringDataCache const &iOther)
Copy constructor.
Definition ExpiringDataCache.h:88
Ptr fCachedData
Definition ExpiringDataCache.h:156
ExpiringDataCache(ExpiringDataCache &&iOther) noexcept
Move constructor.
Definition ExpiringDataCache.h:92
Ptr pointer
Definition ExpiringDataCache.h:76
T value_type
Definition ExpiringDataCache.h:75
Definition ExpiringDataCache.h:28