Jamba C++ API  5.1.1
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 More...
 
 ExpiringDataCache (Loader iDataLoader, uint32 iTimeToLiveMilliseconds)
 Main constructor with loader and TTL. More...
 
 ExpiringDataCache (ExpiringDataCache const &iOther)
 Copy constructor. More...
 
 ExpiringDataCache (ExpiringDataCache &&iOther) noexcept
 Move constructor. More...
 
Ptr getData ()
 Main api to retrieve the data. More...
 
 operator bool () const noexcept
 Allow to write if(cache) to check if the cache is properly initialized. More...
 
ExpiringDataCacheoperator= (ExpiringDataCache const &iOther) noexcept
 Copy assignment operator. More...
 
ExpiringDataCacheoperator= (ExpiringDataCache &&iOther) noexcept
 Move assignment operator. More...
 

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!
}
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

using loader = Loader

◆ pointer

using pointer = Ptr

◆ value_type

using value_type = T

Constructor & Destructor Documentation

◆ ExpiringDataCache() [1/4]

ExpiringDataCache ( )
default

Default empty constructor => getData() always return nullptr

◆ ExpiringDataCache() [2/4]

ExpiringDataCache ( Loader  iDataLoader,
uint32  iTimeToLiveMilliseconds 
)
inline

Main constructor with loader and TTL.

◆ ExpiringDataCache() [3/4]

ExpiringDataCache ( ExpiringDataCache< T, Ptr, Loader > const &  iOther)
inline

Copy constructor.

◆ ExpiringDataCache() [4/4]

ExpiringDataCache ( ExpiringDataCache< T, Ptr, Loader > &&  iOther)
inlinenoexcept

Move constructor.

Member Function Documentation

◆ getData()

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()

void onTimer ( Timer *  timer)
inlineoverrideprivate

◆ operator bool()

operator bool ( ) const
inlineexplicitnoexcept

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

◆ operator=() [1/2]

ExpiringDataCache& operator= ( ExpiringDataCache< T, Ptr, Loader > const &  iOther)
inlinenoexcept

Copy assignment operator.

◆ operator=() [2/2]

ExpiringDataCache& operator= ( ExpiringDataCache< T, Ptr, Loader > &&  iOther)
inlinenoexcept

Move assignment operator.

Member Data Documentation

◆ fCachedData

Ptr fCachedData {}
private

◆ fDataLoader

Loader fDataLoader {}
private

◆ fTimer

std::unique_ptr<AutoReleaseTimer> fTimer {}
private

◆ fTimeToLiveMilliseconds

uint32 fTimeToLiveMilliseconds {}
private

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