#include <itkVisitorDispatcher.h>
Collaboration diagram for itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >:
A visitor function is a function, that can perform various operations on objects of various classes. Usually we want this operation applied on any of the polymorphic derived classes. The specific operation is defined in visitor functions.
For example: calculating the area of Shape objects. In this case the function that calculates the area of a specific shape, is called the visitor function. A specific version of this function must be defined for each class, on which you want the perform the operation (Area(Circle*); Area(Square*); ...).
Now suppose that you want different operations performed on the shape objects. Which operation will be performed is specified by the class of the Visitor object. If the Visitor object is of class Area, then the area of objects will be calculated. If the visitor is object of class Circumference, then the circumference of the shapes will be calculated...
In order to be able to do that and provide the framework to easily add new Visitor as well as Visited classes, we create the VisitorDispatcher class. It is implemented as a singelton. It stores pointers to Visitor functions together with the information about which Visitor function must be called in order to perform an operation specified by Visitor class on objects of Visited class.
To make a specific base class and all its derived classes visitable, you must make the following changes to your code:
1. Declare the folowing virtual member function in the base class:
class BaseVisitable { ... virtual ReturnType AcceptVisitor( VisitorBase* ) = 0; ... };
2. Implement this function in ALL derived classes like this:
class MyVisitableClass : public BaseVisitable { ... virtual ReturnType AcceptVisitor( VisitorBase* l ) { return VisitorDispatcher<MyVisitableClass,VisitorBase,VisitFunctionPointerType>::Visit( <parameters> ); } ... };
Since this code is the same for all derived element classes, you should probably put it in the macro.
3. Register each visitor class with the VisitorDispatcher class before it is called. This is done by calling the member function RegisterVisitor of the VisitorDispatcher class and providing the pointer to the Visitor function that performs the required task. The visitor function must be declared according to the VisitFunctionPointerType template parameter.
ReturnType MyVisitor_Function( ... );
Once all this is done, you can perform various operations on objects of all derived classes by simply calling the Visit function on the pointer to base class and providing a pointer to the specific Visitor object:
object->AcceptVisitor(visitor);
The Visitor class is templated over several classes that make its use generic and simple.
Definition at line 134 of file itkVisitorDispatcher.h.
static VisitFunctionPointerType | Visit (VisitorBasePointer l) |
Public Types | |
typedef int | ClassIDType |
typedef TVisitedClass | VisitedClass |
typedef VisitedClass::ConstPointer | VisitedClassConstPointer |
typedef VisitedClass::Pointer | VisitedClassPointer |
typedef TVisitFunctionPointerType | VisitFunctionPointerType |
typedef TVisitorBase | VisitorBase |
typedef VisitorBase::Pointer | VisitorBasePointer |
typedef VisitorsArrayType::value_type | VisitorsArray_value_type |
typedef std::map< ClassIDType, VisitFunctionPointerType > | VisitorsArrayType |
Static Public Member Functions | |
template<class TVisitorClass> | |
static bool | RegisterVisitor (TVisitorClass *, VisitFunctionPointerType visitor_function) |
typedef int itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::ClassIDType |
Type that holds class IDs.
Definition at line 167 of file itkVisitorDispatcher.h.
typedef TVisitedClass itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::VisitedClass |
TVisitedClass is class to which visitor functions will be applied.
Definition at line 142 of file itkVisitorDispatcher.h.
typedef VisitedClass::ConstPointer itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::VisitedClassConstPointer |
Definition at line 154 of file itkVisitorDispatcher.h.
typedef VisitedClass::Pointer itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::VisitedClassPointer |
Definition at line 153 of file itkVisitorDispatcher.h.
typedef TVisitFunctionPointerType itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::VisitFunctionPointerType |
Type that holds pointers to visit functions
Definition at line 162 of file itkVisitorDispatcher.h.
typedef TVisitorBase itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::VisitorBase |
TVisitorBase is base class for visitor objects. Any class derived from TVisitorBase could in general be applied to TVisitedClass.
Definition at line 148 of file itkVisitorDispatcher.h.
typedef VisitorBase::Pointer itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::VisitorBasePointer |
Definition at line 155 of file itkVisitorDispatcher.h.
typedef VisitorsArrayType::value_type itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::VisitorsArray_value_type |
Definition at line 177 of file itkVisitorDispatcher.h.
typedef std::map<ClassIDType, VisitFunctionPointerType> itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::VisitorsArrayType |
Type that holds array of pairs of class ID and pointer to visit functions.
FIXME: Maybe std::map is not the most efficient way of storing these pointers.
Definition at line 176 of file itkVisitorDispatcher.h.
static bool itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::RegisterVisitor | ( | TVisitorClass * | , | |
VisitFunctionPointerType | visitor_function | |||
) | [inline, static] |
Adds function visitor_function to the VisitorDispatcher class and associates this function with the class TVisitorClass. This means that when member function Visit(e,l) is called, the fucntion visitor_function will be called, if object pointed to by l is of class TVisitorClass.
To automatically register visitor on library initialization, you would call this function immediatly after defining an implementation function class.
Visitor class must define a static member function "int CLID()" that returns the class ID and a virtual member int ClassID() that does the same.
bool Dummy = VisitorDispatcher<Bar,Load>::RegisterVisitor((LoadGrav*)0, &LoadGravImpl);
visitor_function | Pointer to a visitor function. |
Definition at line 209 of file itkVisitorDispatcher.h.
References itk::SimpleFastMutexLock::Lock(), itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::m_MutexLock, itk::SimpleFastMutexLock::Unlock(), and itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::visitors.
VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::VisitFunctionPointerType itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::Visit | ( | VisitorBasePointer | l | ) | [static] |
Returns the pointer to the correct implementation of the visit function. based on the class of object passed in l.
Before this function can be called, the visitor functions must be added to the VisitorDispatcher class for visitor class that is derived from TVisitorBase.
l | Pointer to an object. Class of this object determines which pointer to visit function will be returned. |
Definition at line 309 of file itkVisitorDispatcher.h.