CuteHMI - CuteHMI (CuteHMI.2)
cutehmi::Initializer< DERIVED > Class Template Reference

Initializer template. More...

#include <cutehmi/Initializer.hpp>

Inheritance diagram for cutehmi::Initializer< DERIVED >:
cutehmi::NonCopyable

Public Member Functions

 Initializer (std::function< void()> init, std::function< void()> deinit=nullptr)
 Constructor. More...
 

Protected Member Functions

 ~Initializer ()
 
- Protected Member Functions inherited from cutehmi::NonCopyable
 NonCopyable ()=default
 
 NonCopyable (NonCopyable &&other)=default
 
 ~NonCopyable ()=default
 
NonCopyableoperator= (NonCopyable &&other)=default
 

Detailed Description

template<class DERIVED>
class cutehmi::Initializer< DERIVED >

Initializer template.

Initializer can be used to initialize an extension. This is achieved by subclassing the template and creating a global instance of a derived class, which is a subject of static initialization. This relies on following C++ principle.

"If a variable with static storage duration has initialization or a destructor with side effects, it shall not be eliminated even if it appears to be unused, except that a class object or its copy/move may be eliminated as specified in 11.9.5."

– C++ Standard (6.6.5.1/2 N4810) [basic.stc.static]


Initializer counts its own references and runs initialization and deinitialization code only once - for the first constructed and last destroyed instance.

Note
Special care must be taken for static builds, because a global variable may be skipped by a linker, if it is not used by resulting binary.
Warning
Beware of static initialization order fiasco (SIOF)! The order in which global instances are initialized is undefined. This means that initialization code should be self-contained and it can not rely on other statically initialized objects, including initialization based on this template performed by other extensions.

Constructor & Destructor Documentation

◆ Initializer()

template<class DERIVED >
cutehmi::Initializer< DERIVED >::Initializer ( std::function< void()>  init,
std::function< void()>  deinit = nullptr 
)

Constructor.

Parameters
initinitialization code. Typically a lambda expression can be passed for initialization code.
deinitdeinitialization code or nullptr if there is no deinitialization.

◆ ~Initializer()

template<class DERIVED >
cutehmi::Initializer< DERIVED >::~Initializer
protected