ImFusion SDK 4.3
LazyInstantiatedDataComponent< ComponentType, DataType > Class Template Reference

#include <ImFusion/Base/LazyInstantiatedDataComponent.h>

Extended interface for DataComponents that support lazy instantiation / lazy retrieval. More...

+ Inheritance diagram for LazyInstantiatedDataComponent< ComponentType, DataType >:

Detailed Description

template<typename ComponentType, typename DataType = Data>
class ImFusion::LazyInstantiatedDataComponent< ComponentType, DataType >

Extended interface for DataComponents that support lazy instantiation / lazy retrieval.

Usually, a DataComponent has to be explicitly instantiated first and then added to its corresponding Data object. However, for certain use cases of DataComponents (e.g. display options) it may be handy to always create a default instance when you're trying to retrieve it from a Data object, which does not yet have a component of such type. This class allows for such lazy instantiation of through its static get() method.

In order to implement your own data-specific options class, inherit from this class directly using your class type as template parameter (this pattern is commonly known as the CRTP pattern). Furthermore, make sure to implement to needed constructor taking a const Data* as argument.

Example:

// Define your DataComponent to be lazy-instantiable
class YourComponentType : public DataComponent, public LazyInstantiatedDataComponent<YourComponentType, SharedImageSet>
{
public:
// Constructor signature needed by LazyInstantiatedDataComponent<T>
explicit YourComponentType(const SharedImageSet* data)
[...]
}
[...]
// Lazy-instantiate/request your DataComponent for a given Data instance.
// LazyInstantiatedDataComponent::get() will always return a valid reference.
SharedImageSet& sis = [...];
auto& yourComponent = YourComponentType::get(sis);
Main specialization of DataComponentBase for regular types.
Definition DataComponent.h:109
Extended interface for DataComponents that support lazy instantiation / lazy retrieval.
Definition LazyInstantiatedDataComponent.h:58
Set of images independent of their storage location.
Definition SharedImageSet.h:42
Warning
Due to the invariant of get() that a returned reference remains valid for the lifetime of the parent object, LazyInstantiatedDataComponents put particular burden onto the managing DataComponentList as it must never delete them. Therefore, you should not 'by default' inherit from LazyInstantiatedDataComponent but only if you really deem this necessary. DataComponentList::getOrCreate<T>() may provide a nicer and lighter mean for working with default-instantiated DataComponents.
Template Parameters
ComponentTypeSpecific type of the inheriting class (CRTP pattern).
DataTypeType of the constructor that ComponentType requires. Must inherit from Data, allows to restrict a DataComponent to certain data types.

Static Public Member Functions

static ComponentType & get (const DataType &data)
 Returns the specific options of type T that are associated with the given data.
 
static ComponentType * get (const DataType *data)
 Returns the specific options of type T that are associated with the given data.
 

Member Function Documentation

◆ get() [1/2]

template<typename ComponentType, typename DataType = Data>
static ComponentType & get ( const DataType & data)
inlinestatic

Returns the specific options of type T that are associated with the given data.

All data-specific options are lazily instantiated. Thus, this method always returns a valid reference, which will remain valid as long as the corresponding Data object exists.

Note
Given a single Data instance, the returned reference will always be the same. Thus, it can be considered safe to store the returned reference on client side for direct manipulation.
Parameters
dataReference to the data for which to return the data-specific options.

◆ get() [2/2]

template<typename ComponentType, typename DataType = Data>
static ComponentType * get ( const DataType * data)
inlinestatic

Returns the specific options of type T that are associated with the given data.

All data-specific options are lazily instantiated. Thus, this method always returns a valid reference, which will remain valid as long as the corresponding Data object exists.

Note
Given a single Data instance, the returned reference will always be the same. Thus, it can be considered safe to store the returned reference on client side for direct manipulation.
Parameters
dataReference to the data for which to return the data-specific options.

The documentation for this class was generated from the following file:
Search Tab / S to search, Esc to close