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: 2005/11/17 19:27:40 $
00007   Version:   $Revision: 1.18 $
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 
00024 namespace itk {
00025 namespace fem {
00026 
00027 
00028 
00029 
00066 template<class T>
00067 class FEMObjectFactory
00068 {
00070 
00074   typedef typename T::Pointer (*COF)();
00075 
00079   typedef std::string StrClassName;
00080 
00084   typedef std::vector<std::pair<COF,StrClassName> > COF_Array;
00085   typedef typename COF_Array::value_type COF_Array_value_type;
00086 
00087 public:
00088 
00092   static typename T::Pointer Create(int id) {
00093     return (Instance().cofs_[id].first)();
00094   }
00095 
00102   static int Register(COF f, const char *str)
00103   {
00104     int clid=-1;
00105     Instance().m_MutexLock.Lock();
00106     Instance().cofs_.push_back( COF_Array_value_type(f,str) );
00107     clid = static_cast<int>( Instance().cofs_.size()-1 );
00108     Instance().m_MutexLock.Unlock();
00109     return clid;
00110   }
00112 
00116   static StrClassName ID2ClassName(int id)
00117   {
00118     return Instance().cofs_[id].second;
00119   }
00120 
00127   static int ClassName2ID(StrClassName str)
00128   {
00129     int j=0;
00130     for(typename COF_Array::const_iterator i=Instance().cofs_.begin(); i!=Instance().cofs_.end(); i++) {
00131       if (i->second==str) return j;
00132       j++;
00133     }
00134     return -1;
00135   }
00137 
00138 private:
00139 
00143   COF_Array cofs_;
00144 
00149   mutable SimpleFastMutexLock m_MutexLock;
00150 
00155   FEMObjectFactory();
00156 
00160   FEMObjectFactory(const FEMObjectFactory&);
00161 
00165   ~FEMObjectFactory();
00166 
00170   inline static FEMObjectFactory& Instance();
00171 
00176   static void CleanUP();
00177 
00181   static FEMObjectFactory* obj;
00182 
00183 private:
00189   class Dummy {};
00190 
00195   friend class Dummy;
00196 
00197 };
00198 
00199 
00200 template<class T>
00201 FEMObjectFactory<T>* FEMObjectFactory<T>::obj = 0;
00202 
00203 template<class T>
00204 FEMObjectFactory<T>::FEMObjectFactory() {}
00205 
00206 template<class T>
00207 FEMObjectFactory<T>::FEMObjectFactory(const FEMObjectFactory<T>&) {}
00208 
00209 template<class T>
00210 FEMObjectFactory<T>::~FEMObjectFactory() {}
00211 
00212 extern "C"
00213 {
00214   typedef void(*c_void_cast)();
00215 }
00216 template<class T>
00217 FEMObjectFactory<T>& FEMObjectFactory<T>::Instance() 
00218   {
00219   if (!obj) 
00220     { 
00224     obj=new FEMObjectFactory;
00225 
00230     atexit(reinterpret_cast<c_void_cast>(&CleanUP));
00231 
00232     }
00233 
00237   return *obj;
00238   }
00239 
00240 template<class T>
00241 void FEMObjectFactory<T>::CleanUP() { delete obj; }
00242 
00243 
00244 
00245 
00246 }} // end namespace itk::fem
00247 
00248 #endif // #ifndef __itkFEMFEMObjectFactory_h
00249 

Generated at Sun Sep 23 12:32:54 2007 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000