Claw  1.7.3
factory.hpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
30 #ifndef __CLAW_FACTORY_HPP__
31 #define __CLAW_FACTORY_HPP__
32 
33 #ifdef CLAW_FACTORY_IS_SINGLETON
34 #include <claw/basic_singleton.hpp>
35 #endif
36 
37 #include <claw/exception.hpp>
38 #include <map>
39 
40 namespace claw
41 {
42  namespace pattern
43  {
49  public exception
50  {
51  public:
56  : exception("No type has this identifier.")
57  { }
58  }; // class bad_type_identifier
59 
72  template<typename BaseClass, typename IdentifierType>
73 #ifdef CLAW_FACTORY_IS_SINGLETON
74  class factory:
75  public basic_singleton< factory<BaseClass, IdentifierType> >
76 #else
77  class factory
78 #endif
79  {
80  private:
85  class class_creator_base
86  {
87  public:
88  virtual ~class_creator_base();
89  virtual BaseClass* create() const = 0;
90 
91  }; // class class_creator_base
92 
102  template<typename Derived>
103  class class_creator:
104  public class_creator_base
105  {
106  public:
107  virtual Derived* create() const;
108 
109  }; // class class_creator
110 
112  typedef IdentifierType identifier_type;
113 
115  typedef BaseClass base_class;
116 
118  typedef std::map<identifier_type, class_creator_base*> class_map;
119 
120  public:
121  ~factory();
122 
123  template<typename T>
124  bool register_type( const identifier_type& id );
125 
126  base_class* create( const identifier_type& id ) const;
127 
128  bool is_known_type( const identifier_type& id ) const;
129 
130  private:
132  class_map m_classes;
133 
134  }; // class factory
135 
136  } // namespace pattern
137 } // namespace claw
138 
139 #include <claw/impl/factory.tpp>
140 
141 #endif // __CLAW_FACTORY_HPP__