VTK
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);
See also
vtkDispatcher
Tests:
vtkDispatcher (Tests)

Definition at line 91 of file vtkDispatcher.h.

Member Typedef Documentation

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 137 of file vtkDispatcher.h.

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 138 of file vtkDispatcher.h.

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 142 of file vtkDispatcher.h.

Member Function Documentation

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);

Definition at line 107 of file vtkDispatcher.h.

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 114 of file vtkDispatcher.h.

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 209 of file vtkDispatcher.h.

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

Definition at line 191 of file vtkDispatcher.h.

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

Definition at line 200 of file vtkDispatcher.h.

Member Data Documentation

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

Definition at line 143 of file vtkDispatcher.h.


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