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

itkFEMObjectFactory.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkFEMObjectFactory.h,v $
00005   Language:  C++
00006   Date:      $Date: 2008-05-26 00:38:28 $
00007   Version:   $Revision: 1.19 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
00015 
00016 =========================================================================*/
00017 #ifndef __itkFEMFEMObjectFactory_h
00018 #define __itkFEMFEMObjectFactory_h
00019 
00020 #include "itkFastMutexLock.h"
00021 #include <string>
00022 #include <vector>
00023 #include <cstdlib>
00024 
00025 namespace itk {
00026 namespace fem {
00027 
00028 
00029 
00030 
00067 template<class T>
00068 class FEMObjectFactory
00069 {
00071 
00075   typedef typename T::Pointer (*COF)();
00076 
00080   typedef std::string StrClassName;
00081 
00085   typedef std::vector<std::pair<COF,StrClassName> > COF_Array;
00086   typedef typename COF_Array::value_type COF_Array_value_type;
00087 
00088 public:
00089 
00093   static typename T::Pointer Create(int id) {
00094     return (Instance().cofs_[id].first)();
00095   }
00096 
00103   static int Register(COF f, const char *str)
00104   {
00105     int clid=-1;
00106     Instance().m_MutexLock.Lock();
00107     Instance().cofs_.push_back( COF_Array_value_type(f,str) );
00108     clid = static_cast<int>( Instance().cofs_.size()-1 );
00109     Instance().m_MutexLock.Unlock();
00110     return clid;
00111   }
00113 
00117   static StrClassName ID2ClassName(int id)
00118   {
00119     return Instance().cofs_[id].second;
00120   }
00121 
00128   static int ClassName2ID(StrClassName str)
00129   {
00130     int j=0;
00131     for(typename COF_Array::const_iterator i=Instance().cofs_.begin(); i!=Instance().cofs_.end(); i++) {
00132       if (i->second==str) return j;
00133       j++;
00134     }
00135     return -1;
00136   }
00138 
00139 private:
00140 
00144   COF_Array cofs_;
00145 
00150   mutable SimpleFastMutexLock m_MutexLock;
00151 
00156   FEMObjectFactory();
00157 
00161   FEMObjectFactory(const FEMObjectFactory&);
00162 
00166   ~FEMObjectFactory();
00167 
00171   inline static FEMObjectFactory& Instance();
00172 
00177   static void CleanUP();
00178 
00182   static FEMObjectFactory* obj;
00183 
00184 private:
00190   class Dummy {};
00191 
00196   friend class Dummy;
00197 
00198 };
00199 
00200 
00201 template<class T>
00202 FEMObjectFactory<T>* FEMObjectFactory<T>::obj = 0;
00203 
00204 template<class T>
00205 FEMObjectFactory<T>::FEMObjectFactory() {}
00206 
00207 template<class T>
00208 FEMObjectFactory<T>::FEMObjectFactory(const FEMObjectFactory<T>&) {}
00209 
00210 template<class T>
00211 FEMObjectFactory<T>::~FEMObjectFactory() {}
00212 
00213 extern "C"
00214 {
00215   typedef void(*c_void_cast)();
00216 }
00217 template<class T>
00218 FEMObjectFactory<T>& FEMObjectFactory<T>::Instance() 
00219   {
00220   if (!obj) 
00221     { 
00225     obj=new FEMObjectFactory;
00226 
00231     atexit(reinterpret_cast<c_void_cast>(&CleanUP));
00232 
00233     }
00234 
00238   return *obj;
00239   }
00240 
00241 template<class T>
00242 void FEMObjectFactory<T>::CleanUP() { delete obj; }
00243 
00244 
00245 
00246 
00247 }} // end namespace itk::fem
00248 
00249 #endif // #ifndef __itkFEMFEMObjectFactory_h
00250 

Generated at Wed Nov 5 21:24:46 2008 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000