Gaia-ECS v1.0.0
A simple and powerful entity component system
Loading...
Searching...
No Matches
gaia::mt::JobCallable< TArg > Class Template Reference

Move-only type-erased callable owned by a job. Callables of up to InlineSize bytes are stored inline. Larger ones are allocated with mem::AllocHelper. The storage never comes from the process-wide arena allocators (mem::SmallBlockAllocator and friends), so a job callable can be destroyed on whichever thread finishes with the job. More...

#include <jobcommon.h>

Public Member Functions

 JobCallable (const JobCallable &)=delete
 
JobCallable & operator= (const JobCallable &)=delete
 
 JobCallable (JobCallable &&other) noexcept
 Takes over the callable of other, leaving it empty.
 
JobCallable & operator= (JobCallable &&other) noexcept
 Destroys the stored callable and takes over the callable of other, leaving it empty.
 
template<typename F , typename = std::enable_if_t<!std::is_same_v<std::decay_t<F>, JobCallable>>>
 JobCallable (F &&f)
 Wraps the callable f.
 
template<typename F , typename = std::enable_if_t<!std::is_same_v<std::decay_t<F>, JobCallable>>>
JobCallable & operator= (F &&f)
 Destroys the stored callable and wraps the callable f.
 
template<typename T = TArg, typename = std::enable_if_t<std::is_void_v<T>>>
void exec () const
 Invokes the stored callable.
 
template<typename T = TArg, typename = std::enable_if_t<!std::is_void_v<T>>>
void exec (const T &arg) const
 Invokes the stored callable with arg.
 
template<typename T = TArg, typename = std::enable_if_t<std::is_void_v<T>>>
void operator() () const
 Invokes the stored callable.
 
template<typename T = TArg, typename = std::enable_if_t<!std::is_void_v<T>>>
void operator() (const T &arg) const
 Invokes the stored callable with arg.
 
void reset ()
 Destroys the stored callable and everything it captured, and releases its storage.
 
void finish ()
 Ends the lifetime of the stored callable and everything it captured, but keeps any heap storage until reset() or the destructor releases it. A trivially destructible callable has nothing to end, so it is left untouched and this does not write to the wrapper at all.
 
 operator bool () const
 Checks whether the wrapper holds a callable, or the heap storage of one ended by finish().
 

Static Public Member Functions

template<typename F >
static GAIA_NODISCARD constexpr bool stores_inline ()
 Checks whether a callable of type F is stored inline, without a heap allocation.
 
template<typename F >
static JobCallable create (F &&f)
 Creates a wrapper for the callable f.
 

Static Public Attributes

static constexpr uint32_t InlineSize = 24
 Size in bytes of the inline callable storage.
 

Detailed Description

template<typename TArg>
class gaia::mt::JobCallable< TArg >

Move-only type-erased callable owned by a job. Callables of up to InlineSize bytes are stored inline. Larger ones are allocated with mem::AllocHelper. The storage never comes from the process-wide arena allocators (mem::SmallBlockAllocator and friends), so a job callable can be destroyed on whichever thread finishes with the job.

Template Parameters
TArgType passed by const reference to the callable, or void for a callable taking no arguments.

Constructor & Destructor Documentation

◆ JobCallable() [1/2]

template<typename TArg >
gaia::mt::JobCallable< TArg >::JobCallable ( JobCallable< TArg > &&  other)
inlinenoexcept

Takes over the callable of other, leaving it empty.

Parameters
otherWrapper to move from.

◆ JobCallable() [2/2]

template<typename TArg >
template<typename F , typename = std::enable_if_t<!std::is_same_v<std::decay_t<F>, JobCallable>>>
gaia::mt::JobCallable< TArg >::JobCallable ( F &&  f)
inline

Wraps the callable f.

Template Parameters
FCallable type.
Parameters
fCallable object.

Member Function Documentation

◆ create()

template<typename TArg >
template<typename F >
static JobCallable gaia::mt::JobCallable< TArg >::create ( F &&  f)
inlinestatic

Creates a wrapper for the callable f.

Template Parameters
FCallable type.
Parameters
fCallable object.
Returns
New wrapper.

◆ exec()

template<typename TArg >
template<typename T = TArg, typename = std::enable_if_t<!std::is_void_v<T>>>
void gaia::mt::JobCallable< TArg >::exec ( const T &  arg) const
inline

Invokes the stored callable with arg.

Parameters
argArgument forwarded to the callable.

◆ finish()

template<typename TArg >
void gaia::mt::JobCallable< TArg >::finish ( )
inline

Ends the lifetime of the stored callable and everything it captured, but keeps any heap storage until reset() or the destructor releases it. A trivially destructible callable has nothing to end, so it is left untouched and this does not write to the wrapper at all.

Warning
The callable must not be invoked afterwards.

◆ operator bool()

template<typename TArg >
gaia::mt::JobCallable< TArg >::operator bool ( ) const
inlineexplicit

Checks whether the wrapper holds a callable, or the heap storage of one ended by finish().

Returns
True when a callable or its storage is held.

◆ operator()()

template<typename TArg >
template<typename T = TArg, typename = std::enable_if_t<!std::is_void_v<T>>>
void gaia::mt::JobCallable< TArg >::operator() ( const T &  arg) const
inline

Invokes the stored callable with arg.

Parameters
argArgument forwarded to the callable.

◆ operator=() [1/2]

template<typename TArg >
template<typename F , typename = std::enable_if_t<!std::is_same_v<std::decay_t<F>, JobCallable>>>
JobCallable & gaia::mt::JobCallable< TArg >::operator= ( F &&  f)
inline

Destroys the stored callable and wraps the callable f.

Template Parameters
FCallable type.
Parameters
fCallable object.
Returns
This wrapper.

◆ operator=() [2/2]

template<typename TArg >
JobCallable & gaia::mt::JobCallable< TArg >::operator= ( JobCallable< TArg > &&  other)
inlinenoexcept

Destroys the stored callable and takes over the callable of other, leaving it empty.

Parameters
otherWrapper to move from.
Returns
This wrapper.

◆ stores_inline()

template<typename TArg >
template<typename F >
static GAIA_NODISCARD constexpr bool gaia::mt::JobCallable< TArg >::stores_inline ( )
inlinestaticconstexpr

Checks whether a callable of type F is stored inline, without a heap allocation.

Template Parameters
FCallable type.
Returns
True when F fits the inline storage.

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