itkFEMObjectFactory.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 __itkFEMObjectFactory_h
00018 #define __itkFEMObjectFactory_h
00019
00020 #include "itkFastMutexLock.h"
00021 #include <string>
00022 #include <vector>
00023 #include <cstdlib>
00024
00025 namespace itk {
00026 namespace fem {
00027
00064 template<class T>
00065 class FEMObjectFactory
00066 {
00068
00072 typedef typename T::Pointer (*COF)();
00073
00077 typedef std::string StrClassName;
00078
00082 typedef std::vector<std::pair<COF,StrClassName> > COF_Array;
00083 typedef typename COF_Array::value_type COF_Array_value_type;
00084
00085 public:
00086
00090 static typename T::Pointer Create(int id)
00091 {
00092 return (Instance().cofs_[id].first)();
00093 }
00094
00101 static int Register(COF f, const char *str)
00102 {
00103 int clid=-1;
00104 Instance().m_MutexLock.Lock();
00105 Instance().cofs_.push_back( COF_Array_value_type(f,str) );
00106 clid = static_cast<int>( Instance().cofs_.size()-1 );
00107 Instance().m_MutexLock.Unlock();
00108 return clid;
00109 }
00111
00115 static StrClassName ID2ClassName(int id)
00116 {
00117 return Instance().cofs_[id].second;
00118 }
00119
00126 static int ClassName2ID(StrClassName str)
00127 {
00128 int j=0;
00129 for(typename COF_Array::const_iterator i=Instance().cofs_.begin(); i != Instance().cofs_.end(); i++)
00130 {
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 #endif // #ifndef __itkFEMFEMObjectFactory_h
00246