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 {
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 }}
00247
00248 #endif // #ifndef __itkFEMFEMObjectFactory_h
00249