Audacity 3.2.0
Public Types | Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
GraphicsDataCache< CacheElementType > Class Template Reference

#include <GraphicsDataCache.h>

Inheritance diagram for GraphicsDataCache< CacheElementType >:
[legend]
Collaboration diagram for GraphicsDataCache< CacheElementType >:
[legend]

Public Types

using ElementFactory = std::function< std::unique_ptr< CacheElementType >()>
 
using Initializer = std::function< bool(const GraphicsDataCacheKey &Key, CacheElementType &element)>
 

Public Member Functions

 GraphicsDataCache (const GraphicsDataCache &)=delete
 
GraphicsDataCacheoperator= (const GraphicsDataCache &)=delete
 
 GraphicsDataCache (double scaledSampleRate, ElementFactory elementFactory)
 
 ~GraphicsDataCache () override
 
IteratorRange< GraphicsDataCacheIterator< CacheElementType > > PerformLookup (const ZoomInfo &zoomInfo, double t0, double t1)
 
const CacheElementType * PerformLookup (GraphicsDataCacheKey key)
 
void setInitializer (Initializer initializer)
 
- Public Member Functions inherited from GraphicsDataCacheBase
virtual ~GraphicsDataCacheBase ()=default
 
void Invalidate ()
 Invalidate the cache content. More...
 
double GetScaledSampleRate () const noexcept
 Returns the sample rate associated with cache. More...
 
void UpdateViewportWidth (int64_t width) noexcept
 
int64_t GetMaxViewportWidth () const noexcept
 

Protected Member Functions

virtual void CheckCache (const ZoomInfo &, double, double)
 
virtual bool InitializeElement (const GraphicsDataCacheKey &key, CacheElementType &element)
 
- Protected Member Functions inherited from GraphicsDataCacheBase
 GraphicsDataCacheBase (double scaledSampleRate)
 
void SetScaledSampleRate (double scaledSampleRate)
 
virtual GraphicsDataCacheElementBaseCreateElement (const GraphicsDataCacheKey &key)=0
 Create a new Cache element. Implementation is responsible of the lifetime control. More...
 
virtual void DisposeElement (GraphicsDataCacheElementBase *element)=0
 This method is called, when the cache element should be evicted. Implementation may not deallocate the object. More...
 
virtual bool UpdateElement (const GraphicsDataCacheKey &key, GraphicsDataCacheElementBase &element)=0
 This method is called on all elements matching the request that are not complete (i. e. IsComplete if false). More...
 
BaseLookupResult PerformBaseLookup (const ZoomInfo &zoomInfo, double t0, double t1)
 Perform a lookup inside the cache. This method modifies mLookup and invalidates any previous result. More...
 
const GraphicsDataCacheElementBasePerformBaseLookup (GraphicsDataCacheKey key)
 Perform a lookup for the given key. This method modifies mLookup and invalidates any previous result. More...
 

Private Member Functions

GraphicsDataCacheElementBaseCreateElement (const GraphicsDataCacheKey &key) override
 Create a new Cache element. Implementation is responsible of the lifetime control. More...
 
void DisposeElement (GraphicsDataCacheElementBase *element) override
 This method is called, when the cache element should be evicted. Implementation may not deallocate the object. More...
 
bool UpdateElement (const GraphicsDataCacheKey &key, GraphicsDataCacheElementBase &element) override
 This method is called on all elements matching the request that are not complete (i. e. IsComplete if false). More...
 

Private Attributes

Initializer mInitializer
 
ElementFactory mElementFactory
 
std::deque< std::unique_ptr< CacheElementType > > mCache
 
std::vector< CacheElementType * > mFreeList
 

Additional Inherited Members

- Static Public Attributes inherited from GraphicsDataCacheBase
static constexpr uint32_t CacheElementWidth = 256
 
- Protected Types inherited from GraphicsDataCacheBase
using Lookup = std::vector< LookupElement >
 Cache lookup is a vector, with items sorted using Key. More...
 

Detailed Description

template<typename CacheElementType>
class GraphicsDataCache< CacheElementType >

Definition at line 231 of file GraphicsDataCache.h.

Member Typedef Documentation

◆ ElementFactory

template<typename CacheElementType >
using GraphicsDataCache< CacheElementType >::ElementFactory = std::function<std::unique_ptr<CacheElementType>()>

Definition at line 234 of file GraphicsDataCache.h.

◆ Initializer

template<typename CacheElementType >
using GraphicsDataCache< CacheElementType >::Initializer = std::function<bool(const GraphicsDataCacheKey& Key, CacheElementType& element)>

Definition at line 239 of file GraphicsDataCache.h.

Constructor & Destructor Documentation

◆ GraphicsDataCache() [1/2]

template<typename CacheElementType >
GraphicsDataCache< CacheElementType >::GraphicsDataCache ( const GraphicsDataCache< CacheElementType > &  )
delete

◆ GraphicsDataCache() [2/2]

template<typename CacheElementType >
GraphicsDataCache< CacheElementType >::GraphicsDataCache ( double  scaledSampleRate,
ElementFactory  elementFactory 
)
inlineexplicit

Definition at line 241 of file GraphicsDataCache.h.

242 : GraphicsDataCacheBase(scaledSampleRate), mElementFactory(std::move(elementFactory))
243 {
244 }
GraphicsDataCacheBase(double scaledSampleRate)
ElementFactory mElementFactory

◆ ~GraphicsDataCache()

template<typename CacheElementType >
GraphicsDataCache< CacheElementType >::~GraphicsDataCache ( )
inlineoverride

Definition at line 246 of file GraphicsDataCache.h.

247 {
248 Invalidate();
249 }
void Invalidate()
Invalidate the cache content.

References GraphicsDataCacheBase::Invalidate().

Here is the call graph for this function:

Member Function Documentation

◆ CheckCache()

template<typename CacheElementType >
virtual void GraphicsDataCache< CacheElementType >::CheckCache ( const ZoomInfo ,
double  ,
double   
)
inlineprotectedvirtual

Reimplemented in WaveBitmapCache.

Definition at line 273 of file GraphicsDataCache.h.

274 {
275 }

Referenced by GraphicsDataCache< CacheElementType >::PerformLookup().

Here is the caller graph for this function:

◆ CreateElement()

template<typename CacheElementType >
GraphicsDataCacheElementBase * GraphicsDataCache< CacheElementType >::CreateElement ( const GraphicsDataCacheKey key)
inlineoverrideprivatevirtual

Create a new Cache element. Implementation is responsible of the lifetime control.

Implements GraphicsDataCacheBase.

Definition at line 299 of file GraphicsDataCache.h.

300 {
301 CacheElementType* element = nullptr;
302
303 if (!mFreeList.empty())
304 {
305 element = mFreeList.back();
306 mFreeList.pop_back();
307 }
308
309 if (element == nullptr)
310 {
311 mCache.push_back(std::move(mElementFactory()));
312 element = mCache.back().get();
313 }
314
315 if (element == nullptr)
316 return nullptr;
317
318 if (mInitializer)
319 {
320 if (!mInitializer(key, *element))
321 {
322 DisposeElement(element);
323 return nullptr;
324 }
325 }
326 else if (!InitializeElement(key, *element))
327 {
328 DisposeElement(element);
329 return nullptr;
330 }
331
332 return element;
333 }
static const AudacityProject::AttachedObjects::RegisteredFactory key
std::vector< CacheElementType * > mFreeList
void DisposeElement(GraphicsDataCacheElementBase *element) override
This method is called, when the cache element should be evicted. Implementation may not deallocate th...
std::deque< std::unique_ptr< CacheElementType > > mCache
virtual bool InitializeElement(const GraphicsDataCacheKey &key, CacheElementType &element)
Initializer mInitializer

References GraphicsDataCache< CacheElementType >::DisposeElement(), GraphicsDataCache< CacheElementType >::InitializeElement(), key, GraphicsDataCache< CacheElementType >::mCache, GraphicsDataCache< CacheElementType >::mElementFactory, GraphicsDataCache< CacheElementType >::mFreeList, and GraphicsDataCache< CacheElementType >::mInitializer.

Here is the call graph for this function:

◆ DisposeElement()

template<typename CacheElementType >
void GraphicsDataCache< CacheElementType >::DisposeElement ( GraphicsDataCacheElementBase element)
inlineoverrideprivatevirtual

This method is called, when the cache element should be evicted. Implementation may not deallocate the object.

Implements GraphicsDataCacheBase.

Definition at line 335 of file GraphicsDataCache.h.

336 {
337 if (element == nullptr)
338 return;
339
340 element->Dispose();
341
342 mFreeList.push_back(static_cast<CacheElementType*>(element));
343 }
virtual void Dispose()
This method is called when the item is evicted from the cache. Default implementation is empty.

References GraphicsDataCacheElementBase::Dispose(), and GraphicsDataCache< CacheElementType >::mFreeList.

Referenced by GraphicsDataCache< CacheElementType >::CreateElement().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ InitializeElement()

template<typename CacheElementType >
virtual bool GraphicsDataCache< CacheElementType >::InitializeElement ( const GraphicsDataCacheKey key,
CacheElementType &  element 
)
inlineprotectedvirtual

Reimplemented in WaveBitmapCache, and WaveDataCache.

Definition at line 278 of file GraphicsDataCache.h.

279 {
280 if constexpr (std::is_assignable_v<
281 CacheElementType, GraphicsDataCacheKey>)
282 {
283 element = key;
284 return true;
285 }
286 else if constexpr (std::is_constructible_v<
287 CacheElementType, GraphicsDataCacheKey>)
288 {
289 element = CacheElementType(key);
290 return true;
291 }
292 else
293 {
294 return false;
295 }
296 }
A key into the graphics data cache.

References key.

Referenced by GraphicsDataCache< CacheElementType >::CreateElement(), and GraphicsDataCache< CacheElementType >::UpdateElement().

Here is the caller graph for this function:

◆ operator=()

template<typename CacheElementType >
GraphicsDataCache & GraphicsDataCache< CacheElementType >::operator= ( const GraphicsDataCache< CacheElementType > &  )
delete

◆ PerformLookup() [1/2]

template<typename CacheElementType >
IteratorRange< GraphicsDataCacheIterator< CacheElementType > > GraphicsDataCache< CacheElementType >::PerformLookup ( const ZoomInfo zoomInfo,
double  t0,
double  t1 
)
inline

Definition at line 252 of file GraphicsDataCache.h.

253 {
254 CheckCache(zoomInfo, t0, t1);
255
256 const auto base = this->PerformBaseLookup(zoomInfo, t0, t1);
257
258 return { { base, true }, { base, false } };
259 }
BaseLookupResult PerformBaseLookup(const ZoomInfo &zoomInfo, double t0, double t1)
Perform a lookup inside the cache. This method modifies mLookup and invalidates any previous result.
virtual void CheckCache(const ZoomInfo &, double, double)

References GraphicsDataCache< CacheElementType >::CheckCache(), and GraphicsDataCacheBase::PerformBaseLookup().

Referenced by anonymous_namespace{GraphicsDataCacheTests.cpp}::CheckCacheElementLookup().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PerformLookup() [2/2]

template<typename CacheElementType >
const CacheElementType * GraphicsDataCache< CacheElementType >::PerformLookup ( GraphicsDataCacheKey  key)
inline

Definition at line 261 of file GraphicsDataCache.h.

262 {
263 return static_cast<const CacheElementType*>(PerformBaseLookup(key));
264 }

References key, and GraphicsDataCacheBase::PerformBaseLookup().

Here is the call graph for this function:

◆ setInitializer()

template<typename CacheElementType >
void GraphicsDataCache< CacheElementType >::setInitializer ( Initializer  initializer)
inline

◆ UpdateElement()

template<typename CacheElementType >
bool GraphicsDataCache< CacheElementType >::UpdateElement ( const GraphicsDataCacheKey key,
GraphicsDataCacheElementBase element 
)
inlineoverrideprivatevirtual

This method is called on all elements matching the request that are not complete (i. e. IsComplete if false).

Implements GraphicsDataCacheBase.

Definition at line 345 of file GraphicsDataCache.h.

348 {
349 return InitializeElement(key, static_cast<CacheElementType&>(element));
350 }

References GraphicsDataCache< CacheElementType >::InitializeElement(), and key.

Here is the call graph for this function:

Member Data Documentation

◆ mCache

template<typename CacheElementType >
std::deque<std::unique_ptr<CacheElementType> > GraphicsDataCache< CacheElementType >::mCache
private

◆ mElementFactory

template<typename CacheElementType >
ElementFactory GraphicsDataCache< CacheElementType >::mElementFactory
private

◆ mFreeList

template<typename CacheElementType >
std::vector<CacheElementType*> GraphicsDataCache< CacheElementType >::mFreeList
private

◆ mInitializer

template<typename CacheElementType >
Initializer GraphicsDataCache< CacheElementType >::mInitializer
private

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