9#include "utils/spk_debug_macro.hpp"
24 template <
typename TType>
27 static_assert(!std::is_void_v<TType>,
"TType must not be void");
40 mutable std::optional<TType> _data;
41 mutable std::mutex _mutex;
43 void _destroyDataLocked()
const
45 if (_data.has_value() ==
false)
50 if (_destructor !=
nullptr)
57 void _generateData()
const
59 if (_data.has_value() ==
true)
64 std::scoped_lock lock(_mutex);
65 if (_data.has_value() ==
false)
67 if (_generator ==
nullptr)
69 throw std::logic_error(
"CachedData: generator not set");
75 void _destroyData()
const
77 std::scoped_lock lock(_mutex);
78 if (_data.has_value() ==
false)
93 _generator(
std::move(p_generator)),
94 _destructor(
std::move(p_destructor))
119 return _data.value();
129 return _data.value();
156 return &(_data.value());
166 return &(_data.value());
182 operator const TType &()
const
202 std::scoped_lock lock(_mutex);
203 _destroyDataLocked();
204 _generator = std::move(p_generator);
205 _destructor = std::move(p_destructor);
212 void set(
const TType &p_value)
214 std::scoped_lock lock(_mutex);
215 _destroyDataLocked();
225 std::scoped_lock lock(_mutex);
226 _destroyDataLocked();
227 _data = std::move(p_value);
236 std::scoped_lock lock(_mutex);
237 std::optional<TType> extracted;
238 if (_data.has_value())
240 extracted.emplace(std::move(*_data));
252 return _data.has_value();
Lazily generates and caches a value with optional custom destructor.
Definition spk_cached_data.hpp:26
std::function< TType()> Generator
Callable used to lazily produce the cached value.
Definition spk_cached_data.hpp:33
~CachedData()
Destroys any cached value using the destructor callback if set.
Definition spk_cached_data.hpp:101
const TType & get() const
Retrieves the cached value, generating it on first access.
Definition spk_cached_data.hpp:126
const TType * operator->() const
Member access to the cached value, generating if needed.
Definition spk_cached_data.hpp:163
void release() const
Releases the cached value by invoking destructor if provided.
Definition spk_cached_data.hpp:190
void set(TType &&p_value)
Stores a value directly, bypassing the generator (move overload).
Definition spk_cached_data.hpp:223
std::function< void(TType &)> Destructor
Optional cleanup function invoked when the cache is cleared.
Definition spk_cached_data.hpp:35
TType & operator*()
Dereferences to the cached value, generating if needed.
Definition spk_cached_data.hpp:136
void set(const TType &p_value)
Stores a value directly, bypassing the generator.
Definition spk_cached_data.hpp:212
bool isCached() const
Checks if a value is currently cached.
Definition spk_cached_data.hpp:250
TType & get()
Retrieves the cached value, generating it on first access.
Definition spk_cached_data.hpp:116
TType & refresh()
Drops the cached value and regenerates it.
Definition spk_cached_data.hpp:259
const TType & operator*() const
Dereferences to the cached value, generating if needed.
Definition spk_cached_data.hpp:144
TType * operator->()
Member access to the cached value, generating if needed.
Definition spk_cached_data.hpp:153
void configure(Generator p_generator, Destructor p_destructor=nullptr)
Replaces the generator/destructor pair and clears any cached value.
Definition spk_cached_data.hpp:200
TType value_type
Stored value type.
Definition spk_cached_data.hpp:31
std::optional< TType > take()
Moves the cached value out of the cache without invoking the destructor.
Definition spk_cached_data.hpp:234
CachedData(Generator p_generator, Destructor p_destructor=nullptr)
Builds a cache with a generator and optional destructor.
Definition spk_cached_data.hpp:92