• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Directories
  • File List
  • File Members

factory.hpp

Go to the documentation of this file.
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-2010 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@yahoo.fr
00024 */
00030 #ifndef __CLAW_FACTORY_HPP__
00031 #define __CLAW_FACTORY_HPP__
00032 
00033 #include <claw/basic_singleton.hpp>
00034 #include <claw/exception.hpp>
00035 #include <map>
00036 
00037 namespace claw
00038 {
00039   namespace pattern
00040   {
00045     class bad_type_identifier:
00046       public exception
00047     {
00048     public:
00052       bad_type_identifier() throw()
00053         : exception("No type has this identifier.")
00054       { }
00055     }; // class bad_type_identifier
00056 
00069     template<typename BaseClass, typename IdentifierType>
00070     class factory:
00071       public basic_singleton< factory<BaseClass, IdentifierType> >
00072     {
00073     private:
00078       class class_creator_base
00079       {
00080       public:
00081         virtual ~class_creator_base();
00082         virtual BaseClass* create() const = 0;
00083 
00084       }; // class class_creator_base
00085 
00095       template<typename Derived>
00096       class class_creator:
00097         public class_creator_base
00098       {
00099       public:
00100         virtual Derived* create() const;
00101 
00102       }; // class class_creator
00103 
00105       typedef IdentifierType identifier_type;
00106 
00108       typedef BaseClass base_class;
00109 
00111       typedef std::map<identifier_type, class_creator_base*> class_map;
00112 
00113     public:
00114       ~factory();
00115 
00116       template<typename T>
00117       bool register_type( const identifier_type& id );
00118 
00119       base_class* create( const identifier_type& id ) const;
00120 
00121       bool is_known_type( const identifier_type& id ) const;
00122 
00123     private:
00125       class_map m_classes;
00126 
00127     }; // class factory
00128 
00129   } // namespace pattern
00130 } // namespace claw
00131 
00132 #include <claw/impl/factory.tpp>
00133 
00134 #endif // __CLAW_FACTORY_HPP__

Generated on Fri Dec 24 2010 13:18:31 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.7.2