00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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 {
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 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 }
00110
00114 static StrClassName ID2ClassName(int id)
00115 {
00116 return Instance().cofs_[id].second;
00117 }
00118
00125 static int ClassName2ID(StrClassName str)
00126 {
00127 int j=0;
00128 for(typename COF_Array::const_iterator i=Instance().cofs_.begin(); i!=Instance().cofs_.end(); i++) {
00129 if (i->second==str) return j;
00130 j++;
00131 }
00132 return -1;
00133 }
00134
00135 private:
00136
00140 COF_Array cofs_;
00141
00146 mutable SimpleFastMutexLock m_MutexLock;
00147
00152 FEMObjectFactory();
00153
00157 FEMObjectFactory(const FEMObjectFactory&);
00158
00162 ~FEMObjectFactory();
00163
00167 inline static FEMObjectFactory& Instance();
00168
00173 static void CleanUP();
00174
00178 static FEMObjectFactory* obj;
00179
00180 private:
00186 class Dummy {};
00187
00192 friend class Dummy;
00193
00194 };
00195
00196
00197 template<class T>
00198 FEMObjectFactory<T>* FEMObjectFactory<T>::obj = 0;
00199
00200 template<class T>
00201 FEMObjectFactory<T>::FEMObjectFactory() {}
00202
00203 template<class T>
00204 FEMObjectFactory<T>::FEMObjectFactory(const FEMObjectFactory<T>&) {}
00205
00206 template<class T>
00207 FEMObjectFactory<T>::~FEMObjectFactory() {}
00208
00209 extern "C"
00210 {
00211 typedef void(*c_void_cast)();
00212 }
00213 template<class T>
00214 FEMObjectFactory<T>& FEMObjectFactory<T>::Instance()
00215 {
00216 if (!obj)
00217 {
00221 obj=new FEMObjectFactory;
00222
00227 atexit(reinterpret_cast<c_void_cast>(&CleanUP));
00228
00229 }
00230
00234 return *obj;
00235 }
00236
00237 template<class T>
00238 void FEMObjectFactory<T>::CleanUP() { delete obj; }
00239
00240
00241
00242
00243 }}
00244
00245 #endif // #ifndef __itkFEMFEMObjectFactory_h