claw  1.9.0
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@stuff-o-matic.com
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  {
50  public:
55  : exception("No type has this identifier.")
56  {}
57  }; // class bad_type_identifier
58 
72  template <typename BaseClass, typename IdentifierType>
73 #ifdef CLAW_FACTORY_IS_SINGLETON
74  class factory : public basic_singleton<factory<BaseClass, IdentifierType> >
75 #else
76  class factory
77 #endif
78  {
79  private:
84  class class_creator_base
85  {
86  public:
87  virtual ~class_creator_base();
88  virtual BaseClass* create() const = 0;
89 
90  }; // class class_creator_base
91 
101  template <typename Derived>
102  class class_creator : public class_creator_base
103  {
104  public:
105  virtual Derived* create() const;
106 
107  }; // class class_creator
108 
110  typedef IdentifierType identifier_type;
111 
113  typedef BaseClass base_class;
114 
116  typedef std::map<identifier_type, class_creator_base*> class_map;
117 
118  public:
119  ~factory();
120 
121  template <typename T>
122  bool register_type(const identifier_type& id);
123 
124  base_class* create(const identifier_type& id) const;
125 
126  bool is_known_type(const identifier_type& id) const;
127 
128  private:
130  class_map m_classes;
131 
132  }; // class factory
133 
134  }
135 }
136 
137 #include <claw/factory.tpp>
138 
139 #endif // __CLAW_FACTORY_HPP__
A simple class to use as exception with string message.
Definition: exception.hpp:42
The design pattern of the factory allow to dynamically instanciate classes of various types given an ...
Definition: factory.hpp:76
A (really) basic implementation of the singleton design pattern.
Exception thrown when an incorrect identifier is given to a type.
Definition: factory.hpp:48
A simple class to use as exception with string message.
bad_type_identifier()
Constructor.
Definition: factory.hpp:54
This is the main namespace.
Definition: application.hpp:49