Claw
1.7.3
|
00001 /* 00002 CLAW - a C++ Library Absolutely Wonderful 00003 00004 CLAW is a free library without any particular aim but being useful to 00005 anyone. 00006 00007 Copyright (C) 2005-2011 Julien Jorge 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00022 00023 contact: julien.jorge@gamned.org 00024 */ 00030 #ifndef __CLAW_FACTORY_HPP__ 00031 #define __CLAW_FACTORY_HPP__ 00032 00033 #ifdef CLAW_FACTORY_IS_SINGLETON 00034 #include <claw/basic_singleton.hpp> 00035 #endif 00036 00037 #include <claw/exception.hpp> 00038 #include <map> 00039 00040 namespace claw 00041 { 00042 namespace pattern 00043 { 00048 class bad_type_identifier: 00049 public exception 00050 { 00051 public: 00055 bad_type_identifier() throw() 00056 : exception("No type has this identifier.") 00057 { } 00058 }; // class bad_type_identifier 00059 00072 template<typename BaseClass, typename IdentifierType> 00073 #ifdef CLAW_FACTORY_IS_SINGLETON 00074 class factory: 00075 public basic_singleton< factory<BaseClass, IdentifierType> > 00076 #else 00077 class factory 00078 #endif 00079 { 00080 private: 00085 class class_creator_base 00086 { 00087 public: 00088 virtual ~class_creator_base(); 00089 virtual BaseClass* create() const = 0; 00090 00091 }; // class class_creator_base 00092 00102 template<typename Derived> 00103 class class_creator: 00104 public class_creator_base 00105 { 00106 public: 00107 virtual Derived* create() const; 00108 00109 }; // class class_creator 00110 00112 typedef IdentifierType identifier_type; 00113 00115 typedef BaseClass base_class; 00116 00118 typedef std::map<identifier_type, class_creator_base*> class_map; 00119 00120 public: 00121 ~factory(); 00122 00123 template<typename T> 00124 bool register_type( const identifier_type& id ); 00125 00126 base_class* create( const identifier_type& id ) const; 00127 00128 bool is_known_type( const identifier_type& id ) const; 00129 00130 private: 00132 class_map m_classes; 00133 00134 }; // class factory 00135 00136 } // namespace pattern 00137 } // namespace claw 00138 00139 #include <claw/impl/factory.tpp> 00140 00141 #endif // __CLAW_FACTORY_HPP__