Claw 1.7.3
factory.tpp
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*/
25/**
26 * \file factory.tpp
27 * \brief Implementation of the claw::pattern::factory class.
28 * \author Julien Jorge
29 */
30
31/*----------------------------------------------------------------------------*/
32/**
33 * \brief Destructor.
34 */
35template<typename BaseClass, typename IdentifierType>
36claw::pattern::factory
37<BaseClass, IdentifierType>::class_creator_base::~class_creator_base()
38{
39
40} // factory::class_creator_base::~class_creator_base()
41
42
43
44
45/*----------------------------------------------------------------------------*/
46/**
47 * \brief Create an instance of the derived type..
48 */
49template<typename BaseClass, typename IdentifierType>
50template<typename Derived>
51Derived* claw::pattern::factory
52<BaseClass, IdentifierType>::class_creator<Derived>::create() const
53{
54 return new Derived;
55} // factory::class_creator::create()
56
57
58
59
60/*----------------------------------------------------------------------------*/
61/**
62 * \brief Destructor.
63 */
64template<typename BaseClass, typename IdentifierType>
65claw::pattern::factory<BaseClass, IdentifierType>::~factory()
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/*----------------------------------------------------------------------------*/
76/**
77 * \brief Register a type in the factory.
78 * \param id The identifier to which the type is associated.
79 * \return true if successful.
80 *
81 * \b Template \b parameters:
82 * - \a T The type to register.
83 */
84template<typename BaseClass, typename IdentifierType>
85template<typename T>
86bool
87claw::pattern::factory<BaseClass, IdentifierType>::register_type
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/*----------------------------------------------------------------------------*/
102/**
103 * \brief Create a new instance.
104 * \param id The identifier of the type to instanciate.
105 * \return A pointer to the newly created instance.
106 */
107template<typename BaseClass, typename IdentifierType>
108typename claw::pattern::factory<BaseClass, IdentifierType>::base_class*
109claw::pattern::factory<BaseClass, IdentifierType>::create
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/*----------------------------------------------------------------------------*/
121/**
122 * \brief Tell in a type with a given id is known by the factory.
123 * \param id The identifier of the type to check.
124 */
125template<typename BaseClass, typename IdentifierType>
126bool claw::pattern::factory<BaseClass, IdentifierType>::is_known_type
127( const identifier_type& id ) const
128{
129 return m_classes.find(id) != m_classes.end();
130} // factory::is_known_type()