<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><meta name="robots" content="noindex"> <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>ITK: itkVisitorDispatcher.h Source File</title> <link href="DoxygenStyle.css" rel="stylesheet" type="text/css"> </head><body bgcolor="#ffffff"> <!-- Section customized for INSIGHT : Tue Jul 17 01:02:45 2001 --> <center> <a href="index.html" class="qindex">Main Page</a> <a href="modules.html" class="qindex">Groups</a> <a href="namespaces.html" class="qindex">Namespace List</a> <a href="hierarchy.html" class="qindex">Class Hierarchy</a> <a href="classes.html" class="qindex">Alphabetical List</a> <a href="annotated.html" class="qindex">Compound List</a> <a href="files.html" class="qindex">File List</a> <a href="namespacemembers.html" class="qindex">Namespace Members</a> <a href="functions.html" class="qindex">Compound Members</a> <a href="globals.html" class="qindex">File Members</a> <a href="pages.html" class="qindex">Concepts</a></center> <!-- Generated by Doxygen 1.5.9 --> <div class="navpath"><a class="el" href="dir_02e23d841c198393a6e961e9a0678fff.html">Code</a> » <a class="el" href="dir_2c33022d19e8077162952ea9ed57656d.html">Numerics</a> » <a class="el" href="dir_316e5ab5c8a9cb56a9dce1c9fc454a23.html">FEM</a> </div> <div class="contents"> <h1>itkVisitorDispatcher.h</h1><a href="itkVisitorDispatcher_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*=========================================================================</span> <a name="l00002"></a>00002 <span class="comment"></span> <a name="l00003"></a>00003 <span class="comment"> Program: Insight Segmentation & Registration Toolkit</span> <a name="l00004"></a>00004 <span class="comment"> Module: $RCSfile: itkVisitorDispatcher.h,v $</span> <a name="l00005"></a>00005 <span class="comment"> Language: C++</span> <a name="l00006"></a>00006 <span class="comment"> Date: $Date: 2009-01-30 21:53:03 $</span> <a name="l00007"></a>00007 <span class="comment"> Version: $Revision: 1.24 $</span> <a name="l00008"></a>00008 <span class="comment"></span> <a name="l00009"></a>00009 <span class="comment"> Copyright (c) Insight Software Consortium. All rights reserved.</span> <a name="l00010"></a>00010 <span class="comment"> See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.</span> <a name="l00011"></a>00011 <span class="comment"></span> <a name="l00012"></a>00012 <span class="comment"> This software is distributed WITHOUT ANY WARRANTY; without even </span> <a name="l00013"></a>00013 <span class="comment"> the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR </span> <a name="l00014"></a>00014 <span class="comment"> PURPOSE. See the above copyright notices for more information.</span> <a name="l00015"></a>00015 <span class="comment"></span> <a name="l00016"></a>00016 <span class="comment">=========================================================================*/</span> <a name="l00017"></a>00017 <span class="preprocessor">#ifndef __itkVisitorDispatcher_h</span> <a name="l00018"></a>00018 <span class="preprocessor"></span><span class="preprocessor">#define __itkVisitorDispatcher_h</span> <a name="l00019"></a>00019 <span class="preprocessor"></span> <a name="l00020"></a>00020 <span class="preprocessor">#include "<a class="code" href="itkFEMMacro_8h.html" title="Definitions of macros used in FEM code.">itkFEMMacro.h</a>"</span> <a name="l00021"></a>00021 <span class="preprocessor">#include "<a class="code" href="itkFEMException_8h.html" title="Declaration of several exception classes that are used within the FEM code.">itkFEMException.h</a>"</span> <a name="l00022"></a>00022 <span class="preprocessor">#include "<a class="code" href="itkFastMutexLock_8h.html">itkFastMutexLock.h</a>"</span> <a name="l00023"></a>00023 <span class="preprocessor">#include <typeinfo></span> <a name="l00024"></a>00024 <span class="preprocessor">#include <map></span> <a name="l00025"></a>00025 <a name="l00026"></a>00026 <span class="keyword">namespace </span>itk { <a name="l00027"></a>00027 <span class="keyword">namespace </span>fem { <a name="l00028"></a>00028 <a name="l00029"></a>00029 <span class="keyword">template</span>< <span class="keyword">class </span>TVisitedClass, <a name="l00030"></a>00030 <span class="keyword">class </span>TVisitorBase> <a name="l00031"></a><a class="code" href="classitk_1_1fem_1_1VisitorDispatcherTemplateHelper.html">00031</a> <span class="keyword">class </span><a class="code" href="classitk_1_1fem_1_1VisitorDispatcherTemplateHelper.html">VisitorDispatcherTemplateHelper</a> <a name="l00032"></a>00032 { <a name="l00033"></a>00033 <span class="keyword">public</span>: <a name="l00034"></a><a class="code" href="classitk_1_1fem_1_1VisitorDispatcherTemplateHelper.html#4f9ea0267147b49b33e4398214b9f20c">00034</a> <span class="keyword">typedef</span> void (*<a class="code" href="classitk_1_1fem_1_1VisitorDispatcherTemplateHelper.html#4f9ea0267147b49b33e4398214b9f20c">FunctionPointerType</a> )(<span class="keyword">typename</span> <a class="code" href="itkFEMMacro_8h.html#68fafcc6909430ccd70cbc4aeee0107c">TVisitedClass::ConstPointer</a>, <span class="keyword">typename</span> <a class="code" href="itkFEMMacro_8h.html#539cce1a3282ba59952dedcbf9cdb23f">TVisitorBase::Pointer</a>); <a name="l00035"></a>00035 }; <a name="l00036"></a>00036 <a name="l00128"></a>00128 <span class="keyword">template</span>< <span class="keyword">class </span>TVisitedClass, <a name="l00129"></a>00129 <span class="keyword">class </span>TVisitorBase, <a name="l00130"></a>00130 <span class="keyword">class </span>TVisitFunctionPointerType= <a class="code" href="itkWin32Header_8h.html#140a5da12f0043ba181c7ae0109c31bf">ITK_TYPENAME</a> <a class="code" href="classitk_1_1fem_1_1VisitorDispatcherTemplateHelper.html">VisitorDispatcherTemplateHelper<TVisitedClass, TVisitorBase>::FunctionPointerType</a> > <a name="l00131"></a><a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html">00131</a> <span class="keyword">class </span><a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html" title="This class provides the functionality needed to apply the correct visitor function...">VisitorDispatcher</a> <a name="l00132"></a>00132 { <a name="l00133"></a>00133 <span class="keyword">public</span>: <a name="l00135"></a>00135 <a name="l00139"></a><a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#64d1b5951871170dc17fe8b9482b9f27">00139</a> <span class="keyword">typedef</span> TVisitedClass <a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#64d1b5951871170dc17fe8b9482b9f27">VisitedClass</a>; <a name="l00140"></a>00140 <a name="l00145"></a><a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#1a6a273d8dd7d6fb986008983a37346c">00145</a> <span class="keyword">typedef</span> TVisitorBase <a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#1a6a273d8dd7d6fb986008983a37346c">VisitorBase</a>; <a name="l00146"></a>00146 <a name="l00150"></a><a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#3cf1dfe39946c298e07324191a4b4c88">00150</a> <span class="keyword">typedef</span> <span class="keyword">typename</span> <a class="code" href="itkFEMMacro_8h.html#539cce1a3282ba59952dedcbf9cdb23f">VisitedClass::Pointer</a> <a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#3cf1dfe39946c298e07324191a4b4c88">VisitedClassPointer</a>; <a name="l00151"></a><a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#f22adae01df8c9471f975f5ba89810d7">00151</a> <span class="keyword">typedef</span> <span class="keyword">typename</span> <a class="code" href="itkFEMMacro_8h.html#68fafcc6909430ccd70cbc4aeee0107c">VisitedClass::ConstPointer</a> <a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#f22adae01df8c9471f975f5ba89810d7">VisitedClassConstPointer</a>; <a name="l00152"></a><a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#0220e3b0e160b3214ff23e1fbdb3c5e1">00152</a> <span class="keyword">typedef</span> <span class="keyword">typename</span> <a class="code" href="itkFEMMacro_8h.html#539cce1a3282ba59952dedcbf9cdb23f">VisitorBase::Pointer</a> <a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#0220e3b0e160b3214ff23e1fbdb3c5e1">VisitorBasePointer</a>; <a name="l00153"></a>00153 <a name="l00157"></a><a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#3bf241cc37137a7746999dd02b00aec6">00157</a> <span class="keyword">typedef</span> TVisitFunctionPointerType <a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#3bf241cc37137a7746999dd02b00aec6">VisitFunctionPointerType</a>; <a name="l00158"></a>00158 <a name="l00162"></a><a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#3eeb9e5d40db2e96a28f7e87a1cd7525">00162</a> <span class="keyword">typedef</span> <span class="keywordtype">int</span> <a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#3eeb9e5d40db2e96a28f7e87a1cd7525">ClassIDType</a>; <a name="l00163"></a>00163 <a name="l00171"></a><a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#fdfe4fcafeb424c112e4ed9159bad497">00171</a> <span class="keyword">typedef</span> std::map<ClassIDType, VisitFunctionPointerType> <a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#fdfe4fcafeb424c112e4ed9159bad497">VisitorsArrayType</a>; <a name="l00172"></a><a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#4b177839573127a782a225f8cbed6027">00172</a> <span class="keyword">typedef</span> <span class="keyword">typename</span> <a class="code" href="namespaceitk.html#87ac35b82c3f0b7231a21eda576557d1">VisitorsArrayType::value_type</a> <a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#4b177839573127a782a225f8cbed6027">VisitorsArray_value_type</a>; <a name="l00173"></a>00173 <a name="l00203"></a>00203 <span class="keyword">template</span><<span class="keyword">class</span> TVisitorClass> <a name="l00204"></a><a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#ca466353dc410bde826aea7397292450">00204</a> <span class="keyword">inline</span> <span class="keyword">static</span> <span class="keywordtype">bool</span> <a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#ca466353dc410bde826aea7397292450">RegisterVisitor</a>(TVisitorClass*, <a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#3bf241cc37137a7746999dd02b00aec6">VisitFunctionPointerType</a> visitor_function) <a name="l00205"></a>00205 { <a name="l00206"></a>00206 <span class="keyword">typedef</span> TVisitorClass VisitorClass; <a name="l00207"></a>00207 <span class="keywordtype">bool</span> status; <a name="l00208"></a>00208 Instance().m_MutexLock.<a class="code" href="classitk_1_1SimpleFastMutexLock.html#523045202556fb2b3f29e28728d312bf">Lock</a>(); <a name="l00209"></a>00209 status=Instance().visitors.insert(<a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#4b177839573127a782a225f8cbed6027">VisitorsArray_value_type</a>(VisitorClass::CLID(),visitor_function)).second; <a name="l00210"></a>00210 Instance().m_MutexLock.<a class="code" href="classitk_1_1SimpleFastMutexLock.html#1e02c08e74981429e5a8919632cf2fa4">Unlock</a>(); <a name="l00211"></a>00211 <span class="keywordflow">if</span> ( status ) <a name="l00212"></a>00212 { <a name="l00213"></a>00213 <span class="comment">// Visitor class was successfully registered</span> <a name="l00214"></a>00214 } <a name="l00215"></a>00215 <span class="keywordflow">else</span> <a name="l00216"></a>00216 { <a name="l00217"></a>00217 <span class="comment">// The visitor function was already registered.</span> <a name="l00218"></a>00218 <span class="comment">// FIXME: implement the proper error handler if required</span> <a name="l00219"></a>00219 std::cout<<<span class="stringliteral">"Warning: Visitor "</span><<<span class="keyword">typeid</span>(VisitorClass).name()<<<span class="stringliteral">" that operates on objects of "</span><<<span class="keyword">typeid</span>(<a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#64d1b5951871170dc17fe8b9482b9f27">VisitedClass</a>).name()<<<span class="stringliteral">" was already registered! Ignoring the re-registration.\n"</span>; <a name="l00220"></a>00220 } <a name="l00221"></a>00221 <span class="keywordflow">return</span> status; <a name="l00222"></a>00222 } <a name="l00224"></a>00224 <a name="l00238"></a>00238 <span class="keyword">static</span> <a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#3bf241cc37137a7746999dd02b00aec6">VisitFunctionPointerType</a> <a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#b02a2fab0b4f1415c02b27a18e2c9535">Visit</a>(<a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#0220e3b0e160b3214ff23e1fbdb3c5e1">VisitorBasePointer</a> l); <a name="l00239"></a>00239 <a name="l00240"></a>00240 <span class="keyword">private</span>: <a name="l00241"></a>00241 <a name="l00242"></a>00242 <span class="keyword">static</span> <a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html" title="This class provides the functionality needed to apply the correct visitor function...">VisitorDispatcher</a>& Instance(); <a name="l00243"></a>00243 <a name="l00244"></a>00244 <span class="keyword">static</span> <span class="keywordtype">void</span> CleanUP(<span class="keywordtype">void</span>) { <span class="keyword">delete</span> obj; } <a name="l00245"></a>00245 <a name="l00249"></a>00249 <span class="keyword">static</span> VisitorDispatcher* obj; <a name="l00250"></a>00250 <a name="l00251"></a>00251 <a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#fdfe4fcafeb424c112e4ed9159bad497">VisitorsArrayType</a> visitors; <a name="l00252"></a>00252 <a name="l00257"></a>00257 <span class="keyword">mutable</span> <a class="code" href="classitk_1_1SimpleFastMutexLock.html" title="Critical section locking class that can be allocated on the stack.">SimpleFastMutexLock</a> m_MutexLock; <a name="l00258"></a>00258 <a name="l00259"></a>00259 }; <a name="l00260"></a>00260 <a name="l00261"></a>00261 <span class="keyword">template</span><<span class="keyword">class</span> TVisitedClass, <span class="keyword">class</span> TVisitorBase, <span class="keyword">class</span> TVisitFunctionPo<span class="keywordtype">int</span>erType> <a name="l00262"></a>00262 VisitorDispatcher<TVisitedClass, TVisitorBase, TVisitFunctionPointerType>* <a name="l00263"></a>00263 VisitorDispatcher<TVisitedClass, TVisitorBase, TVisitFunctionPointerType> <a name="l00264"></a>00264 ::obj = 0; <a name="l00265"></a>00265 <a name="l00266"></a>00266 <span class="keyword">extern</span> <span class="stringliteral">"C"</span> <a name="l00267"></a>00267 { <a name="l00268"></a>00268 <span class="keyword">typedef</span> void(*<a class="code" href="namespaceitk_1_1fem.html#626e8408eaa44a728b6d124d7d59d6a9">c_void_cast</a>)(); <a name="l00269"></a>00269 } <a name="l00270"></a>00270 <a name="l00271"></a>00271 <span class="keyword">template</span><<span class="keyword">class</span> TVisitedClass, <span class="keyword">class</span> TVisitorBase, <span class="keyword">class</span> TVisitFunctionPo<span class="keywordtype">int</span>erType> <a name="l00272"></a>00272 VisitorDispatcher<TVisitedClass, TVisitorBase, TVisitFunctionPointerType>& <a name="l00273"></a>00273 VisitorDispatcher<TVisitedClass, TVisitorBase, TVisitFunctionPointerType> <a name="l00274"></a>00274 ::Instance() <a name="l00275"></a>00275 { <a name="l00276"></a>00276 <span class="comment">// Implementation of the singleton design pattern</span> <a name="l00277"></a>00277 <span class="keywordflow">if</span> (!obj) <a name="l00278"></a>00278 { <a name="l00279"></a>00279 <span class="comment">// Create a new VisitorDispatcher object if we don't have it already.</span> <a name="l00280"></a>00280 obj=<span class="keyword">new</span> VisitorDispatcher; <a name="l00281"></a>00281 <a name="l00282"></a>00282 <span class="comment">// Make sure that the object that we just created is also destroyed</span> <a name="l00283"></a>00283 <span class="comment">// when program finishes.</span> <a name="l00284"></a>00284 atexit(reinterpret_cast<c_void_cast>(&CleanUP)); <a name="l00285"></a>00285 } <a name="l00286"></a>00286 <a name="l00287"></a>00287 <span class="comment">// Return the actual VisitorDispatcher object</span> <a name="l00288"></a>00288 <span class="keywordflow">return</span> *obj; <a name="l00289"></a>00289 } <a name="l00290"></a>00290 <a name="l00291"></a>00291 <span class="keyword">template</span><<span class="keyword">class</span> TVisitedClass, <span class="keyword">class</span> TVisitorBase, <span class="keyword">class</span> TVisitFunctionPo<span class="keywordtype">int</span>erType> <a name="l00292"></a>00292 <span class="keyword">typename</span> VisitorDispatcher<TVisitedClass, TVisitorBase, TVisitFunctionPointerType>::VisitFunctionPointerType <a name="l00293"></a>00293 <a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#b02a2fab0b4f1415c02b27a18e2c9535">VisitorDispatcher<TVisitedClass, TVisitorBase, TVisitFunctionPointerType></a> <a name="l00294"></a><a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#b02a2fab0b4f1415c02b27a18e2c9535">00294</a> <a class="code" href="classitk_1_1fem_1_1VisitorDispatcher.html#b02a2fab0b4f1415c02b27a18e2c9535">::Visit</a>(VisitorBasePointer l) <a name="l00295"></a>00295 { <a name="l00296"></a>00296 <span class="keyword">typename</span> VisitorsArrayType::const_iterator i = Instance().visitors.find(l->ClassID()); <a name="l00297"></a>00297 <span class="keywordflow">if</span>( i==Instance().visitors.end() ) <a name="l00298"></a>00298 { <a name="l00299"></a>00299 <span class="comment">// Visitor function not found... FIXME: write the proper error handler.</span> <a name="l00300"></a>00300 std::cout<<<span class="stringliteral">"Error: Visitor "</span><<<span class="keyword">typeid</span>(*l).name()<<<span class="stringliteral">" that operates on objects of "</span><<<span class="keyword">typeid</span>(VisitedClass).name()<<<span class="stringliteral">" not found!\n"</span>; <a name="l00301"></a>00301 <span class="keywordflow">throw</span> <a class="code" href="classitk_1_1fem_1_1FEMException.html" title="Base class for all exception&#39;s that can occur within FEM classes.">FEMException</a>(__FILE__, __LINE__, <span class="stringliteral">"FEM error"</span>); <a name="l00302"></a>00302 } <a name="l00303"></a>00303 <span class="keywordflow">return</span> i->second; <a name="l00304"></a>00304 } <a name="l00305"></a>00305 <a name="l00306"></a>00306 }} <span class="comment">// end namespace itk::fem</span> <a name="l00307"></a>00307 <a name="l00308"></a>00308 <span class="preprocessor">#endif // __VisitorDispatcher_h</span> <a name="l00309"></a>00309 <span class="preprocessor"></span> </pre></div></div> <hr><address><small> Generated at Fri May 8 00:35:46 2009 for ITK by <a href="http://www.stack.nl/~dimitri/doxygen/index.html"> <img src="http://www.stack.nl/~dimitri/doxygen/doxygen.png" alt="doxygen" align="middle" border=0 width=110 height=53> </a> 1.5.9 written by <a href="mailto:dimitri@stack.nl">Dimitri van Heesch</a>, © 1997-2000</small></address> </body> </html>