Jamba C++ API 7.5.0
Loading...
Searching...
No Matches
ExpiringDataCache< T, Ptr, Loader > Class Template Reference

The purpose of this class is to implement a short live cache (time to live (or TTL) being a constructor parameter) for data. More...

#include <ExpiringDataCache.h>

Inherits ITimerCallback.

Public Types

using loader = Loader
using pointer = Ptr
using value_type = T

Public Member Functions

 ExpiringDataCache ()=default
 Default empty constructor => getData() always return nullptr.
 ExpiringDataCache (ExpiringDataCache &&iOther) noexcept
 Move constructor.
 ExpiringDataCache (ExpiringDataCache const &iOther)
 Copy constructor.
 ExpiringDataCache (Loader iDataLoader, uint32 iTimeToLiveMilliseconds)
 Main constructor with loader and TTL.
Ptr getData ()
 Main api to retrieve the data.
 operator bool () const noexcept
 Allow to write if(cache) to check if the cache is properly initialized.
ExpiringDataCacheoperator= (ExpiringDataCache &&iOther) noexcept
 Move assignment operator.
ExpiringDataCacheoperator= (ExpiringDataCache const &iOther) noexcept
 Copy assignment operator.

Private Member Functions

void onTimer (Timer *timer) override

Private Attributes

Ptr fCachedData {}
Loader fDataLoader {}
std::unique_ptr< AutoReleaseTimerfTimer {}
uint32 fTimeToLiveMilliseconds {}

Detailed Description

template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
class pongasoft::VST::VstUtils::ExpiringDataCache< T, Ptr, Loader >

The purpose of this class is to implement a short live cache (time to live (or TTL) being a constructor parameter) for data.

This class uses the Timer class from the VST3 SDK to implement the timeout so that it can safely be used by all classes living in the plugin. This class is not thread safe but is intended to be used in the VST3 plugin environment where the timer (ITimerCallback::onTimer) and caller (ExpiringDataCache::getData) are part of the event loop and thus never called by 2 threads at the same time.

Note
This class offers copy/move constructors and copy/move assignment operators but care must be taken that the Loader can safely be moved/copied if you use them. A bad example would be a lambda capturing this
struct MyClass
{
std::shared_ptr<MyData> getMyData() const { return fMyDataCache.getData(); }
// this is good because it is capturing a shared pointer independent of "this"
MyClass(std::shared_ptr<MyStorage> iStorage) : fStorage{iStorage},
fMyDataCache{[iStorage]() -> auto { return iStorage->loadMyData(); }, myTTL} {}
// This is terrible:
// MyClass(std::shared_ptr<MyStorage> iStorage) : fStorage{iStorage},
// fMyDataCache{[this]() -> auto { return fStorage->loadMyData(); }, myTTL} {}
//
// And will fail with code like this:
// MyClass a1{myStorage};
// MyClass a2{a1}; // this will lead to failure if a1 goes away because Loader captures the address of a1!!!!
std::shared_ptr<MyStorage> fStorage{};
mutable ExpiringDataCache<MyData> fMyDataCache{}; // mutable allows to declare `getMyData()` const!
}
ExpiringDataCache()=default
Default empty constructor => getData() always return nullptr.
Template Parameters
Tthe type of the data that is being cached
Ptrthe (optional) type for the pointer. By default it is std::shared_ptr<T> because this makes the most sense for data that needs to be shared (clearly this class keeps it and then the caller via ExpiringDataCache::getData()). You can use T* if you prefer/need to, but is not recommended.
Loaderthe (optional) type for the loader "function" which must be a "callable" object (iDataLoader() returns a Ptr) and is by default std::function<Ptr()>.

Member Typedef Documentation

◆ loader

template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
using loader = Loader

◆ pointer

template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
using pointer = Ptr

◆ value_type

template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
using value_type = T

Constructor & Destructor Documentation

◆ ExpiringDataCache() [1/4]

template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
ExpiringDataCache ( )
default

Default empty constructor => getData() always return nullptr.

◆ ExpiringDataCache() [2/4]

template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
ExpiringDataCache ( Loader iDataLoader,
uint32 iTimeToLiveMilliseconds )
inline

Main constructor with loader and TTL.

◆ ExpiringDataCache() [3/4]

template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
ExpiringDataCache ( ExpiringDataCache< T, Ptr, Loader > const & iOther)
inline

Copy constructor.

◆ ExpiringDataCache() [4/4]

template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
ExpiringDataCache ( ExpiringDataCache< T, Ptr, Loader > && iOther)
inlinenoexcept

Move constructor.

Member Function Documentation

◆ getData()

template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
Ptr getData ( )
inline

Main api to retrieve the data.

If the data is in the cache, it simply returns it (and extends the duration to remain in the cache by TTL). If it is not in the cache, it delegates to the loader to get it. Note that it is ok for the loader to return nullptr, but this result will not be cached (meaning, the loader will be called every time).

Returns
the data (which can be nullptr)

◆ onTimer()

template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
void onTimer ( Timer * timer)
inlineoverrideprivate

◆ operator bool()

template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
operator bool ( ) const
inlineexplicitnoexcept

Allow to write if(cache) to check if the cache is properly initialized.

◆ operator=() [1/2]

template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
ExpiringDataCache & operator= ( ExpiringDataCache< T, Ptr, Loader > && iOther)
inlinenoexcept

Move assignment operator.

◆ operator=() [2/2]

template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
ExpiringDataCache & operator= ( ExpiringDataCache< T, Ptr, Loader > const & iOther)
inlinenoexcept

Copy assignment operator.

Member Data Documentation

◆ fCachedData

template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
Ptr fCachedData {}
private

◆ fDataLoader

template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
Loader fDataLoader {}
private

◆ fTimer

template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
std::unique_ptr<AutoReleaseTimer> fTimer {}
private

◆ fTimeToLiveMilliseconds

template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
uint32 fTimeToLiveMilliseconds {}
private

The documentation for this class was generated from the following file: