Claw  1.7.3
factory.tpp
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 */
31 /*----------------------------------------------------------------------------*/
35 template<typename BaseClass, typename IdentifierType>
37 <BaseClass, IdentifierType>::class_creator_base::~class_creator_base()
38 {
39 
40 } // factory::class_creator_base::~class_creator_base()
41 
42 
43 
44 
45 /*----------------------------------------------------------------------------*/
49 template<typename BaseClass, typename IdentifierType>
50 template<typename Derived>
52 <BaseClass, IdentifierType>::class_creator<Derived>::create() const
53 {
54  return new Derived;
55 } // factory::class_creator::create()
56 
57 
58 
59 
60 /*----------------------------------------------------------------------------*/
64 template<typename BaseClass, typename IdentifierType>
66 {
67  typename class_map::const_iterator it;
68 
69  for (it=m_classes.begin(); it!=m_classes.end(); ++it)
70  delete it->second;
71 
72  m_classes.clear();
73 } // factory::~factory()
74 
75 /*----------------------------------------------------------------------------*/
84 template<typename BaseClass, typename IdentifierType>
85 template<typename T>
86 bool
88 ( const identifier_type& id )
89 {
90  typename class_map::iterator it = m_classes.find(id);
91 
92  if ( it == m_classes.end() )
93  {
94  m_classes[id] = new class_creator<T>;
95  return true;
96  }
97  else
98  return false;
99 } // factory::register_type()
100 
101 /*----------------------------------------------------------------------------*/
107 template<typename BaseClass, typename IdentifierType>
108 typename claw::pattern::factory<BaseClass, IdentifierType>::base_class*
110 ( const identifier_type& id ) const
111 {
112  typename class_map::const_iterator it = m_classes.find(id);
113 
114  if ( it==m_classes.end() )
115  throw bad_type_identifier();
116  else
117  return it->second->create();
118 } // factory::create()
119 
120 /*----------------------------------------------------------------------------*/
125 template<typename BaseClass, typename IdentifierType>
127 ( const identifier_type& id ) const
128 {
129  return m_classes.find(id) != m_classes.end();
130 } // factory::is_known_type()