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: 2009-04-05 10:56:50 $
00007   Version:   $Revision: 1.21 $
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 __itkFEMObjectFactory_h
00018 #define __itkFEMObjectFactory_h
00019 
00020 #include "itkFastMutexLock.h"
00021 #include <string>
00022 #include <vector>
00023 #include <cstdlib>
00024 #include <stdlib.h>
00025 
00026 namespace itk {
00027 namespace fem {
00028 
00065 template<class T>
00066 class FEMObjectFactory
00067 {
00069 
00073   typedef typename T::Pointer (*COF)();
00074 
00078   typedef std::string StrClassName;
00079 
00083   typedef std::vector<std::pair<COF,StrClassName> > COF_Array;
00084   typedef typename COF_Array::value_type COF_Array_value_type;
00085 
00086 public:
00087 
00091   static typename T::Pointer Create(int id)
00092     {
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       {
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 }} // end namespace itk::fem
00245 
00246 #endif // #ifndef __itkFEMFEMObjectFactory_h
00247 

Generated at Mon Jul 12 2010 18:19:20 for ITK by doxygen 1.7.1 written by Dimitri van Heesch, © 1997-2000