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
00085 public:
00086
00090 static typename T::Pointer Create(int id) {
00091 return (Instance().cofs_[id].first)();
00092 }
00093
00100 static int Register(COF f, const char *str)
00101 {
00102 int clid=-1;
00103 Instance().m_MutexLock.Lock();
00104
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 template<class T>
00210 FEMObjectFactory<T>& FEMObjectFactory<T>::Instance()
00211 {
00212 if (!obj)
00213 {
00217 obj=new FEMObjectFactory;
00218
00223 atexit(&CleanUP);
00224
00225 }
00226
00230 return *obj;
00231 }
00232
00233 template<class T>
00234 void FEMObjectFactory<T>::CleanUP() { delete obj; }
00235
00236
00237
00238
00239 }}
00240
00241 #endif // #ifndef __itkFEMFEMObjectFactory_h