DGtal 1.4.0
Loading...
Searching...
No Matches
DGtal::functors::FunctorHolder< FunctorStorage, NeedDereference > Class Template Reference

Aim: hold any callable object (function, functor, lambda, ...) as a C(Unary)Functor model. More...

#include <DGtal/base/FunctorHolder.h>

Public Member Functions

template<typename Function , typename std::enable_if<!std::is_base_of< FunctorHolder, typename std::decay< Function >::type >::value, int >::type = 0>
 FunctorHolder (Function &&fn)
 Constructor.
 
template<typename... T>
auto operator() (T &&... args) const -> decltype(Invoker< NeedDereference >::apply(this->myFunctor, std::forward< T >(args)...))
 Invokes the stored callable object in a constant context.
 
template<typename... T>
auto operator() (T &&... args) -> decltype(Invoker< NeedDereference >::apply(this->myFunctor, std::forward< T >(args)...))
 Invokes the stored callable object in a mutable context.
 
void selfDisplay (std::ostream &out) const
 Writes/Displays the object on an output stream.
 
constexpr bool isValid () const
 Checks the validity/consistency of the object.
 

Private Attributes

FunctorStorage myFunctor
 The callable object.
 

Detailed Description

template<typename FunctorStorage, bool NeedDereference>
class DGtal::functors::FunctorHolder< FunctorStorage, NeedDereference >

Aim: hold any callable object (function, functor, lambda, ...) as a C(Unary)Functor model.

Template Parameters
FunctorStorageType used to store the given callable object.
NeedDereferencetrue if the object must be dereferenced before calls.
Warning
This class is not meant to be directly constructed by the user. As explained below, you should use instead the holdFunctor helper that will choose the more appropriate storage type depending on the given callable object.

Given any type of callable object (function, functor and lambda) passed by value, reference or pointer, stores it in the most appropriate way so that the returned object is compatible with DGtal functor concept (concepts::CUnaryFunctor so far), and especially the boost::Assignable concept.

More precisely, the storage mode depends on the lifetime of the given object, deduced from its passing mode :

  • if the given object is passed by left-value reference (long lifetime), it will be stored as a reference (using std::reference_wrapper).
  • otherwise (short lifetime, like right-value references), it will be moved to a dynamically allocated memory place (std::shared_ptr) so that the object lifetime matches the FunctorHolder lifetime.

In both case, the returned object is copy/move constructible and copy/move assignable so that it can be used as a functor in all DGtal library.

Remarks
It is important to not explicitly specify the given object type so that it's real lifetime can be deduced using universal reference. However, you can transfer ownership of the object to FunctorHolder by moving it using std::move. As a consequence, the type of the returned FunctorHolder cannot be guessed easily and the use of the auto keyword is thus mandatory.

More informations can be found in the dedicate module page about Using functions, functors and lambdas in DGtal .

See also
holdFunctor

Definition at line 152 of file FunctorHolder.h.

Constructor & Destructor Documentation

◆ FunctorHolder()

template<typename FunctorStorage , bool NeedDereference>
template<typename Function , typename std::enable_if<!std::is_base_of< FunctorHolder, typename std::decay< Function >::type >::value, int >::type = 0>
DGtal::functors::FunctorHolder< FunctorStorage, NeedDereference >::FunctorHolder ( Function && fn)
inlineexplicit

Constructor.

Template Parameters
FunctionThe type of the callable object (auto-deduced).
Parameters
fnThe callable object.

Definition at line 172 of file FunctorHolder.h.

173 : myFunctor(std::forward<Function>(fn))
174 {
175 }
FunctorStorage myFunctor
The callable object.

Member Function Documentation

◆ isValid()

template<typename FunctorStorage , bool NeedDereference>
bool DGtal::functors::FunctorHolder< FunctorStorage, NeedDereference >::isValid ( ) const
inlineconstexpr

Checks the validity/consistency of the object.

Returns
'true' if the object is valid, 'false' otherwise.

Definition at line 225 of file FunctorHolder.h.

226 {
227 return true;
228 }

◆ operator()() [1/2]

template<typename FunctorStorage , bool NeedDereference>
template<typename... T>
auto DGtal::functors::FunctorHolder< FunctorStorage, NeedDereference >::operator() ( T &&... args) -> decltype(Invoker<NeedDereference>::apply(this->myFunctor, std::forward<T>(args)...))
inline

Invokes the stored callable object in a mutable context.

Template Parameters
TArguments's types.
Parameters
argsThe arguments.

Definition at line 198 of file FunctorHolder.h.

200 {
201 return Invoker<NeedDereference>::apply(myFunctor, std::forward<T>(args)...);
202 }

References DGtal::functors::FunctorHolder< FunctorStorage, NeedDereference >::myFunctor.

◆ operator()() [2/2]

template<typename FunctorStorage , bool NeedDereference>
template<typename... T>
auto DGtal::functors::FunctorHolder< FunctorStorage, NeedDereference >::operator() ( T &&... args) const -> decltype(Invoker<NeedDereference>::apply(this->myFunctor, std::forward<T>(args)...))
inline

Invokes the stored callable object in a constant context.

Template Parameters
TArguments's types.
Parameters
argsThe arguments.

Definition at line 186 of file FunctorHolder.h.

188 {
189 return Invoker<NeedDereference>::apply(myFunctor, std::forward<T>(args)...);
190 }

References DGtal::functors::FunctorHolder< FunctorStorage, NeedDereference >::myFunctor.

◆ selfDisplay()

template<typename FunctorStorage , bool NeedDereference>
void DGtal::functors::FunctorHolder< FunctorStorage, NeedDereference >::selfDisplay ( std::ostream & out) const
inline

Writes/Displays the object on an output stream.

Parameters
outthe output stream where the object is written.

Definition at line 209 of file FunctorHolder.h.

210 {
211 out << "[FunctorHolder]";
212 if ( SharedPtrTrait::apply(myFunctor) )
213 out << " using std::shared_ptr storage (rvalue)";
214 else if ( ReferenceWrapperTrait::apply(myFunctor) )
215 out << " using std::reference_wrapper storage (lvalue)";
216 else
217 out << " using custom storage";
218 }

References DGtal::functors::FunctorHolder< FunctorStorage, NeedDereference >::myFunctor.

Field Documentation

◆ myFunctor

template<typename FunctorStorage , bool NeedDereference>
FunctorStorage DGtal::functors::FunctorHolder< FunctorStorage, NeedDereference >::myFunctor
private

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