itkVisitorDispatcher.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __itkVisitorDispatcher_h
00018 #define __itkVisitorDispatcher_h
00019
00020 #include "itkFEMMacro.h"
00021 #include "itkFEMException.h"
00022 #include "itkFastMutexLock.h"
00023 #include <typeinfo>
00024 #include <map>
00025
00026 namespace itk {
00027 namespace fem {
00028
00029 template< class TVisitedClass,
00030 class TVisitorBase>
00031 class VisitorDispatcherTemplateHelper
00032 {
00033 public:
00034 typedef void (*FunctionPointerType )(typename TVisitedClass::ConstPointer, typename TVisitorBase::Pointer);
00035 };
00036
00128 template< class TVisitedClass,
00129 class TVisitorBase,
00130 class TVisitFunctionPointerType= ITK_TYPENAME VisitorDispatcherTemplateHelper<TVisitedClass, TVisitorBase>::FunctionPointerType >
00131 class VisitorDispatcher
00132 {
00133 public:
00135
00139 typedef TVisitedClass VisitedClass;
00140
00145 typedef TVisitorBase VisitorBase;
00146
00150 typedef typename VisitedClass::Pointer VisitedClassPointer;
00151 typedef typename VisitedClass::ConstPointer VisitedClassConstPointer;
00152 typedef typename VisitorBase::Pointer VisitorBasePointer;
00153
00157 typedef TVisitFunctionPointerType VisitFunctionPointerType;
00158
00162 typedef int ClassIDType;
00163
00171 typedef std::map<ClassIDType, VisitFunctionPointerType> VisitorsArrayType;
00172 typedef typename VisitorsArrayType::value_type VisitorsArray_value_type;
00173
00203 template<class TVisitorClass>
00204 inline static bool RegisterVisitor(TVisitorClass*, VisitFunctionPointerType visitor_function)
00205 {
00206 typedef TVisitorClass VisitorClass;
00207 bool status;
00208 Instance().m_MutexLock.Lock();
00209 status=Instance().visitors.insert(VisitorsArray_value_type(VisitorClass::CLID(),visitor_function)).second;
00210 Instance().m_MutexLock.Unlock();
00211 if ( status )
00212 {
00213
00214 }
00215 else
00216 {
00217
00218
00219 std::cout<<"Warning: Visitor "<<typeid(VisitorClass).name()<<" that operates on objects of "<<typeid(VisitedClass).name()<<" was already registered! Ignoring the re-registration.\n";
00220 }
00221 return status;
00222 }
00224
00238 static VisitFunctionPointerType Visit(VisitorBasePointer l);
00239
00240 private:
00241
00242 static VisitorDispatcher& Instance();
00243
00244 static void CleanUP(void) { delete obj; }
00245
00249 static VisitorDispatcher* obj;
00250
00251 VisitorsArrayType visitors;
00252
00257 mutable SimpleFastMutexLock m_MutexLock;
00258
00259 };
00260
00261 template<class TVisitedClass, class TVisitorBase, class TVisitFunctionPointerType>
00262 VisitorDispatcher<TVisitedClass, TVisitorBase, TVisitFunctionPointerType>*
00263 VisitorDispatcher<TVisitedClass, TVisitorBase, TVisitFunctionPointerType>
00264 ::obj = 0;
00265
00266 extern "C"
00267 {
00268 typedef void(*c_void_cast)();
00269 }
00270
00271 template<class TVisitedClass, class TVisitorBase, class TVisitFunctionPointerType>
00272 VisitorDispatcher<TVisitedClass, TVisitorBase, TVisitFunctionPointerType>&
00273 VisitorDispatcher<TVisitedClass, TVisitorBase, TVisitFunctionPointerType>
00274 ::Instance()
00275 {
00276
00277 if (!obj)
00278 {
00279
00280 obj=new VisitorDispatcher;
00281
00282
00283
00284 atexit(reinterpret_cast<c_void_cast>(&CleanUP));
00285 }
00286
00287
00288 return *obj;
00289 }
00290
00291 template<class TVisitedClass, class TVisitorBase, class TVisitFunctionPointerType>
00292 typename VisitorDispatcher<TVisitedClass, TVisitorBase, TVisitFunctionPointerType>::VisitFunctionPointerType
00293 VisitorDispatcher<TVisitedClass, TVisitorBase, TVisitFunctionPointerType>
00294 ::Visit(VisitorBasePointer l)
00295 {
00296 typename VisitorsArrayType::const_iterator i = Instance().visitors.find(l->ClassID());
00297 if( i==Instance().visitors.end() )
00298 {
00299
00300 std::cout<<"Error: Visitor "<<typeid(*l).name()<<" that operates on objects of "<<typeid(VisitedClass).name()<<" not found!\n";
00301 throw FEMException(__FILE__, __LINE__, "FEM error");
00302 }
00303 return i->second;
00304 }
00305
00306 }}
00307
00308 #endif // __VisitorDispatcher_h
00309