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;
- See also
- vtkDispatcher
- Tests:
- vtkDispatcher (Tests)
Definition at line 90 of file vtkDispatcher.h.
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.
Definition at line 102 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 109 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.
Go(dataArray1); dispatcher.
Go(dataArray2);
Definition at line 197 of file vtkDispatcher.h.