Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType > Class Template Reference

#include <itkVisitorDispatcher.h>

Collaboration diagram for itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >:

Collaboration graph
[legend]

List of all members.


Detailed Description

template<class TVisitedClass, class TVisitorBase, class TVisitFunctionPointerType = typename VisitorDispatcherTemplateHelper<TVisitedClass, TVisitorBase>::FunctionPointerType>
class itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >

This class provides the functionality needed to apply the correct visitor function to object of some class. The specific visitor function is choosen, based on a given pointer to some object.

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.

Note:
Template parameter TVisitFunctionPointerType in general doesn't have to be a pointer to function. In fact, it can be any type Object of this type will be returned, when calling the VisitorDispatcher::Visit function.

Definition at line 131 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)

Member Typedef Documentation

template<class TVisitedClass, class TVisitorBase, class TVisitFunctionPointerType = typename VisitorDispatcherTemplateHelper<TVisitedClass, TVisitorBase>::FunctionPointerType>
typedef int itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::ClassIDType

Type that holds class IDs.

Definition at line 162 of file itkVisitorDispatcher.h.

template<class TVisitedClass, class TVisitorBase, class TVisitFunctionPointerType = typename VisitorDispatcherTemplateHelper<TVisitedClass, TVisitorBase>::FunctionPointerType>
typedef TVisitedClass itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::VisitedClass

TVisitedClass is class to which visitor functions will be applied.

Definition at line 139 of file itkVisitorDispatcher.h.

template<class TVisitedClass, class TVisitorBase, class TVisitFunctionPointerType = typename VisitorDispatcherTemplateHelper<TVisitedClass, TVisitorBase>::FunctionPointerType>
typedef VisitedClass::ConstPointer itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::VisitedClassConstPointer

Definition at line 151 of file itkVisitorDispatcher.h.

template<class TVisitedClass, class TVisitorBase, class TVisitFunctionPointerType = typename VisitorDispatcherTemplateHelper<TVisitedClass, TVisitorBase>::FunctionPointerType>
typedef VisitedClass::Pointer itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::VisitedClassPointer

Definition at line 150 of file itkVisitorDispatcher.h.

template<class TVisitedClass, class TVisitorBase, class TVisitFunctionPointerType = typename VisitorDispatcherTemplateHelper<TVisitedClass, TVisitorBase>::FunctionPointerType>
typedef TVisitFunctionPointerType itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::VisitFunctionPointerType

Type that holds pointers to visit functions

Definition at line 157 of file itkVisitorDispatcher.h.

template<class TVisitedClass, class TVisitorBase, class TVisitFunctionPointerType = typename VisitorDispatcherTemplateHelper<TVisitedClass, TVisitorBase>::FunctionPointerType>
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 145 of file itkVisitorDispatcher.h.

template<class TVisitedClass, class TVisitorBase, class TVisitFunctionPointerType = typename VisitorDispatcherTemplateHelper<TVisitedClass, TVisitorBase>::FunctionPointerType>
typedef VisitorBase::Pointer itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::VisitorBasePointer

Definition at line 152 of file itkVisitorDispatcher.h.

template<class TVisitedClass, class TVisitorBase, class TVisitFunctionPointerType = typename VisitorDispatcherTemplateHelper<TVisitedClass, TVisitorBase>::FunctionPointerType>
typedef VisitorsArrayType::value_type itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::VisitorsArray_value_type

Definition at line 172 of file itkVisitorDispatcher.h.

template<class TVisitedClass, class TVisitorBase, class TVisitFunctionPointerType = typename VisitorDispatcherTemplateHelper<TVisitedClass, TVisitorBase>::FunctionPointerType>
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 171 of file itkVisitorDispatcher.h.


Member Function Documentation

template<class TVisitedClass, class TVisitorBase, class TVisitFunctionPointerType = typename VisitorDispatcherTemplateHelper<TVisitedClass, TVisitorBase>::FunctionPointerType>
template<class TVisitorClass>
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);

Parameters:
visitor_function Pointer to a visitor function.
Note:
Dummy class pointer must be passed as a first parameter to automatically deduct the correct template parameter TVisitorClass. Technically we would like to call the this function as VisitorDispatcher<...>RegisterVisitor<MyVisitorClass>(...), but MS C compiler crashes if we do this. This is a work around. You should pass null pointer casted to the TVisitorClass when calling this function.
See also:
Visit

Definition at line 204 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.

template<class TVisitedClass, class TVisitorBase, class TVisitFunctionPointerType>
VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::VisitFunctionPointerType itk::fem::VisitorDispatcher< TVisitedClass, TVisitorBase, TVisitFunctionPointerType >::Visit ( VisitorBasePointer  l  )  [inline, 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.

Parameters:
l Pointer to an object. Class of this object determines which pointer to visit function will be returned.
See also:
RegisterVisitor

Definition at line 294 of file itkVisitorDispatcher.h.


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

Generated at Sat Feb 28 19:15:23 2009 for ITK by doxygen 1.5.6 written by Dimitri van Heesch, © 1997-2000