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 Instance().cofs_.push_back( COF_Array::value_type(f,str) );
00105 clid = static_cast<int>( Instance().cofs_.size()-1 );
00106 Instance().m_MutexLock.Unlock();
00107 return clid;
00108 }
00109
00113 static StrClassName ID2ClassName(int id)
00114 {
00115 return Instance().cofs_[id].second;
00116 }
00117
00124 static int ClassName2ID(StrClassName str)
00125 {
00126 int j=0;
00127 for(typename COF_Array::const_iterator i=Instance().cofs_.begin(); i!=Instance().cofs_.end(); i++) {
00128 if (i->second==str) return j;
00129 j++;
00130 }
00131 return -1;
00132 }
00133
00134 private:
00135
00139 COF_Array cofs_;
00140
00145 mutable SimpleFastMutexLock m_MutexLock;
00146
00151 FEMObjectFactory();
00152
00156 FEMObjectFactory(const FEMObjectFactory&);
00157
00161 ~FEMObjectFactory();
00162
00166 inline static FEMObjectFactory& Instance();
00167
00172 static void CleanUP();
00173
00177 static FEMObjectFactory* obj;
00178
00179 private:
00185 class Dummy {};
00186
00191 friend class Dummy;
00192
00193 };
00194
00195
00196 template<class T>
00197 FEMObjectFactory<T>* FEMObjectFactory<T>::obj = 0;
00198
00199 template<class T>
00200 FEMObjectFactory<T>::FEMObjectFactory() {}
00201
00202 template<class T>
00203 FEMObjectFactory<T>::FEMObjectFactory(const FEMObjectFactory<T>&) {}
00204
00205 template<class T>
00206 FEMObjectFactory<T>::~FEMObjectFactory() {}
00207
00208 template<class T>
00209 FEMObjectFactory<T>& FEMObjectFactory<T>::Instance()
00210 {
00211 if (!obj)
00212 {
00216 obj=new FEMObjectFactory;
00217
00222 atexit(&CleanUP);
00223
00224 }
00225
00229 return *obj;
00230 }
00231
00232 template<class T>
00233 void FEMObjectFactory<T>::CleanUP() { delete obj; }
00234
00235
00236
00237
00238 }}
00239
00240 #endif // #ifndef __itkFEMFEMObjectFactory_h