VTK  9.1.0
Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
vtkDispatcher< BaseLhs, ReturnType, CastingPolicy > Class Template Reference

Dispatch to functor based on a pointer type. More...

#include <vtkDispatcher.h>

Public Member Functions

template<class SomeLhs , class Functor >
void Add (Functor fun)
 Add in a functor that is mapped to the template SomeLhs parameter. More...
 
template<class SomeLhs >
bool Remove ()
 Remove a functor that is bound to the given parameter type. More...
 
ReturnType Go (BaseLhs *lhs)
 Given a pointer to an object that derives from the BaseLhs we find the matching functor that was added, and call it passing along the given parameter. More...
 

Protected Types

typedef vtkDispatcherCommon::TypeInfo TypeInfo
 
typedef vtkDispatcherPrivate::Functor< ReturnType, BaseLhs > MappedType
 
typedef std::map< TypeInfo, MappedTypeMapType
 

Protected Member Functions

void DoAddFunctor (TypeInfo lhs, MappedType fun)
 
bool DoRemove (TypeInfo lhs)
 

Protected Attributes

MapType FunctorMap
 

Detailed Description

template<class BaseLhs, typename ReturnType = void, template< class, class > class CastingPolicy = vtkDispatcherCommon::vtkCaster>
class vtkDispatcher< BaseLhs, ReturnType, CastingPolicy >

Dispatch to functor based on a pointer type.

vtkDispatcher is a class that allows calling a functor based on the derived types of a pointer. This form of dynamic dispatching allows the conversion of runtime polymorphism to a compile time polymorphism. For example it can be used as a replacement for the vtkTemplateMacro, with bonus of being easier to understand

Note: By default the return type is void.

The functors that are passed around can contain state, and are allowed to be const or non const. If you are using a functor that does have state, make sure your copy constructor is correct.

struct functor{
template<typename T>
void operator()(T& t) const
{
}
};
Here is an example of using the dispatcher.
\code
statefull functor;
dispatcher.Add<vtkCharArray>(&functor);
dispatcher.Add<vtkDoubleArray>(&functor);
dispatcher.Add<vtkIdTypeArray>(&functor);
dispatcher.Add<vtkIntArray>(&functor);
dispatcher.Add<vtkFloatArray>(&functor);
dispatcher.Go(ptr1);
dynamic, self-adjusting array of char
Definition: vtkCharArray.h:68
Dispatch to functor based on a pointer type.
Definition: vtkDispatcher.h:89
ReturnType Go(BaseLhs *lhs)
Given a pointer to an object that derives from the BaseLhs we find the matching functor that was adde...
void Add(Functor fun)
Add in a functor that is mapped to the template SomeLhs parameter.
dynamic, self-adjusting array of double
dynamic, self-adjusting array of float
dynamic, self-adjusting array of vtkIdType
dynamic, self-adjusting array of int
Definition: vtkIntArray.h:149
See also
vtkDispatcher

Definition at line 88 of file vtkDispatcher.h.

Member Typedef Documentation

◆ TypeInfo

template<class BaseLhs , typename ReturnType = void, template< class, class > class CastingPolicy = vtkDispatcherCommon::vtkCaster>
typedef vtkDispatcherCommon::TypeInfo vtkDispatcher< BaseLhs, ReturnType, CastingPolicy >::TypeInfo
protected

Definition at line 141 of file vtkDispatcher.h.

◆ MappedType

template<class BaseLhs , typename ReturnType = void, template< class, class > class CastingPolicy = vtkDispatcherCommon::vtkCaster>
typedef vtkDispatcherPrivate::Functor<ReturnType, BaseLhs> vtkDispatcher< BaseLhs, ReturnType, CastingPolicy >::MappedType
protected

Definition at line 142 of file vtkDispatcher.h.

◆ MapType

template<class BaseLhs , typename ReturnType = void, template< class, class > class CastingPolicy = vtkDispatcherCommon::vtkCaster>
typedef std::map<TypeInfo, MappedType> vtkDispatcher< BaseLhs, ReturnType, CastingPolicy >::MapType
protected

Definition at line 146 of file vtkDispatcher.h.

Member Function Documentation

◆ Add()

template<class BaseLhs , typename ReturnType = void, template< class, class > class CastingPolicy = vtkDispatcherCommon::vtkCaster>
template<class SomeLhs , class Functor >
void vtkDispatcher< BaseLhs, ReturnType, CastingPolicy >::Add ( Functor  fun)
inline

Add in a functor that is mapped to the template SomeLhs parameter.

When instances of the parameter is passed in on the Go method we will call the functor and pass along the given parameter. Note: This copies the functor so pass stateful functors by pointer.

dispatcher.Add<vtkImageData>(exampleFunctor());
dispatcher.Add<vtkImageData>(&exampleFunctorWithState);
topologically and geometrically regular array of data
Definition: vtkImageData.h:157

Definition at line 104 of file vtkDispatcher.h.

◆ Remove()

template<class BaseLhs , typename ReturnType = void, template< class, class > class CastingPolicy = vtkDispatcherCommon::vtkCaster>
template<class SomeLhs >
bool vtkDispatcher< BaseLhs, ReturnType, CastingPolicy >::Remove ( )
inline

Remove a functor that is bound to the given parameter type.

Will return true if we did remove a functor.

Definition at line 115 of file vtkDispatcher.h.

◆ Go()

template<class BaseLhs , typename ReturnType , template< class, class > class CastingPolicy>
ReturnType vtkDispatcher< BaseLhs, ReturnType, CastingPolicy >::Go ( BaseLhs *  lhs)

Given a pointer to an object that derives from the BaseLhs we find the matching functor that was added, and call it passing along the given parameter.

It should be noted that the functor will be called with the parameter being the derived type that Functor was registered with.

Note: This will only find exact matches. So if you add functor to find vtkDataArray, it will not be called if passed with a vtkDoubleArray.

dispatcher.Add(vtkFloatArray>(floatFunctor())
dispatcher.Add(vtkDoubleArray>(doubleFunctor())
dispatcher.Go(dataArray1);
dispatcher.Go(dataArray2);

Definition at line 204 of file vtkDispatcher.h.

◆ DoAddFunctor()

template<class BaseLhs , typename ReturnType , template< class, class > class CastingPolicy>
void vtkDispatcher< BaseLhs, ReturnType, CastingPolicy >::DoAddFunctor ( TypeInfo  lhs,
MappedType  fun 
)
protected

Definition at line 188 of file vtkDispatcher.h.

◆ DoRemove()

template<class BaseLhs , typename ReturnType , template< class, class > class CastingPolicy>
bool vtkDispatcher< BaseLhs, ReturnType, CastingPolicy >::DoRemove ( TypeInfo  lhs)
protected

Definition at line 196 of file vtkDispatcher.h.

Member Data Documentation

◆ FunctorMap

template<class BaseLhs , typename ReturnType = void, template< class, class > class CastingPolicy = vtkDispatcherCommon::vtkCaster>
MapType vtkDispatcher< BaseLhs, ReturnType, CastingPolicy >::FunctorMap
protected

Definition at line 147 of file vtkDispatcher.h.


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