00001 // -*- C++ -*- 00019 #ifndef DOIL_SERVANTFACTORY_H 00020 #define DOIL_SERVANTFACTORY_H 00021 00022 #include <doil/ServantBase.h> 00023 #include <doil/ImplBase.h> 00024 00025 namespace doil 00026 { 00027 // Servant 生成・削除関数のtypedef 00028 typedef ServantBase* (*ServantNewFunc)(ImplBase*); 00029 typedef void (*ServantDeleteFunc)(ServantBase*); 00030 00031 // Servant 生成のためのテンプレート関数 00032 template <class Servant> 00033 ServantBase* New(ImplBase* impl) 00034 { 00035 return new Servant(impl); 00036 } 00037 00038 // Servant 削除のためのテンプレート関数 00039 template <class Servant> 00040 void Delete(ServantBase* servant) 00041 { 00042 if (servant != NULL) 00043 { 00044 delete servant; 00045 servant = NULL; 00046 } 00047 } 00048 00049 class ServantFactoryBase 00050 { 00051 public: 00052 virtual ~ServantFactoryBase(){} 00053 virtual const char* name() = 0; 00054 virtual ServantBase* create() = 0; 00055 virtual void destroy(ServantBase* servant) = 0; 00056 }; 00057 00058 00059 class ServantFactory 00060 // : public ServantFactoryBase 00061 { 00062 public: 00063 ServantFactory(const char* id, 00064 ServantNewFunc new_func, 00065 ServantDeleteFunc delete_func) 00066 : m_id(id), m_new(new_func), m_delete(delete_func) 00067 { 00068 } 00069 00070 /* 00071 ServantFactory(const ServantFactory& sf) 00072 { 00073 m_name = sf.m_name; 00074 m_new = sf.m_new; 00075 m_delete = sf.m_delete; 00076 } 00077 00078 ServantFactory& operator=(ServantFactory& sf) 00079 { 00080 ServantFactory tmp(sf); 00081 std::swap(*this, tmp); 00082 return *this; 00083 } 00084 */ 00085 virtual ~ServantFactory(){}; 00086 00087 virtual const char* id() 00088 { 00089 return m_id.c_str(); 00090 } 00091 virtual ServantBase* create(ImplBase* impl) 00092 { 00093 return m_new(impl); 00094 } 00095 virtual void destroy(ServantBase* servant) 00096 { 00097 m_delete(servant); 00098 } 00099 private: 00100 std::string m_id; 00101 ServantNewFunc m_new; 00102 ServantDeleteFunc m_delete; 00103 }; 00104 00105 00106 }; 00107 #endif // DOIL_SERVANTFACTORY_H