#include <itkVisitorDispatcher.h>
Collaboration diagram for itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >:
Public Types | |
typedef TVisitedClass | VisitedClass |
typedef TVisitorBase | VisitorBase |
typedef VisitedClass::Pointer | VisitedClassPointer |
typedef VisitedClass::ConstPointer | VisitedClassConstPointer |
typedef VisitorBase::Pointer | VisitorBasePointer |
typedef TVisitFunctionPointerType | VisitFunctionPointerType |
typedef int | ClassIDType |
typedef std::map< ClassIDType, VisitFunctionPointerType > | VisitorsArrayType |
Static Public Methods | |
VisitFunctionPointerType | Visit (VisitorBasePointer l) |
template<class TVisitorClass> bool | RegisterVisitor (TVisitorClass *, VisitFunctionPointerType visitor_function) |
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.
|
Type that holds class IDs. Definition at line 167 of file itkVisitorDispatcher.h. |
|
TVisitedClass is class to which visitor functions will be applied. Definition at line 142 of file itkVisitorDispatcher.h. |
|
Definition at line 154 of file itkVisitorDispatcher.h. |
|
Definition at line 153 of file itkVisitorDispatcher.h. |
|
Type that holds pointers to visit functions Definition at line 162 of file itkVisitorDispatcher.h. |
|
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. |
|
Definition at line 155 of file itkVisitorDispatcher.h. |
|
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. |
|
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);
Definition at line 208 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. |
|
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.
Definition at line 304 of file itkVisitorDispatcher.h. |