VTK
|
Dispatch to functor based on a pointer type. More...
#include <vtkDispatcher.h>
Public Member Functions | |
ReturnType | Go (BaseLhs *lhs) |
template<class SomeLhs , class Functor > | |
void | Add (Functor fun) |
template<class SomeLhs > | |
bool | Remove () |
Protected Types | |
typedef vtkDispatcherCommon::TypeInfo | TypeInfo |
typedef vtkDispatcherPrivate::Functor < ReturnType, BaseLhs > | MappedType |
typedef std::map< TypeInfo, MappedType > | MapType |
Protected Member Functions | |
void | DoAddFunctor (TypeInfo lhs, MappedType fun) |
bool | DoRemove (TypeInfo lhs) |
Protected Attributes | |
MapType | FunctorMap |
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; vtkDispatcher<vtkDataArray> dispatcher; dispatcher.Add<vtkCharArray>(&functor); dispatcher.Add<vtkDoubleArray>(&functor); dispatcher.Add<vtkIdTypeArray>(&functor); dispatcher.Add<vtkIntArray>(&functor); dispatcher.Add<vtkFloatArray>(&functor); dispatcher.Go(ptr1);
Definition at line 90 of file vtkDispatcher.h.
typedef vtkDispatcherCommon::TypeInfo vtkDispatcher< BaseLhs, ReturnType, CastingPolicy >::TypeInfo [protected] |
Definition at line 125 of file vtkDispatcher.h.
typedef vtkDispatcherPrivate::Functor<ReturnType,BaseLhs> vtkDispatcher< BaseLhs, ReturnType, CastingPolicy >::MappedType [protected] |
Definition at line 126 of file vtkDispatcher.h.
typedef std::map<TypeInfo, MappedType > vtkDispatcher< BaseLhs, ReturnType, CastingPolicy >::MapType [protected] |
Definition at line 130 of file vtkDispatcher.h.
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.
vtkDispatcher<vtkDataModel> dispatcher; dispatcher.Add<vtkImageData>(exampleFunctor()); dispatcher.Add<vtkImageData>(&exampleFunctorWithState);
Definition at line 102 of file vtkDispatcher.h.
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 109 of file vtkDispatcher.h.
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.
vtkDispatcher<vtkDataArray> dispatcher; dispatcher.Add(vtkFloatArray>(floatFunctor()) dispatcher.Add(vtkDoubleArray>(doubleFunctor()) dispatcher.Go(dataArray1); dispatcher.Go(dataArray2);
Definition at line 197 of file vtkDispatcher.h.
void vtkDispatcher< BaseLhs, ReturnType, CastingPolicy >::DoAddFunctor | ( | TypeInfo | lhs, |
MappedType | fun | ||
) | [protected] |
Definition at line 179 of file vtkDispatcher.h.
bool vtkDispatcher< BaseLhs, ReturnType, CastingPolicy >::DoRemove | ( | TypeInfo | lhs | ) | [protected] |
Definition at line 188 of file vtkDispatcher.h.
MapType vtkDispatcher< BaseLhs, ReturnType, CastingPolicy >::FunctorMap [protected] |
Definition at line 131 of file vtkDispatcher.h.