Claw  1.7.0
factory.tpp
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-2011 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@gamned.org
00024 */
00031 /*----------------------------------------------------------------------------*/
00035 template<typename BaseClass, typename IdentifierType>
00036 claw::pattern::factory
00037 <BaseClass, IdentifierType>::class_creator_base::~class_creator_base()
00038 {
00039 
00040 } //  factory::class_creator_base::~class_creator_base()
00041 
00042 
00043 
00044 
00045 /*----------------------------------------------------------------------------*/
00049 template<typename BaseClass, typename IdentifierType>
00050 template<typename Derived>
00051 Derived* claw::pattern::factory
00052 <BaseClass, IdentifierType>::class_creator<Derived>::create() const
00053 {
00054   return new Derived;
00055 } // factory::class_creator::create()
00056 
00057 
00058 
00059 
00060 /*----------------------------------------------------------------------------*/
00064 template<typename BaseClass, typename IdentifierType>
00065 claw::pattern::factory<BaseClass, IdentifierType>::~factory()
00066 {
00067   typename class_map::const_iterator it;
00068 
00069   for (it=m_classes.begin(); it!=m_classes.end(); ++it)
00070     delete it->second;
00071 
00072   m_classes.clear();
00073 } // factory::~factory()
00074 
00075 /*----------------------------------------------------------------------------*/
00084 template<typename BaseClass, typename IdentifierType>
00085 template<typename T>
00086 bool
00087 claw::pattern::factory<BaseClass, IdentifierType>::register_type
00088 ( const identifier_type& id )
00089 {
00090   typename class_map::iterator it = m_classes.find(id);
00091 
00092   if ( it == m_classes.end() )
00093     {
00094       m_classes[id] = new class_creator<T>;
00095       return true;
00096     }
00097   else
00098     return false;
00099 } // factory::register_type()
00100 
00101 /*----------------------------------------------------------------------------*/
00107 template<typename BaseClass, typename IdentifierType>
00108 typename claw::pattern::factory<BaseClass, IdentifierType>::base_class*
00109 claw::pattern::factory<BaseClass, IdentifierType>::create
00110 ( const identifier_type& id ) const
00111 {
00112   typename class_map::const_iterator it = m_classes.find(id);
00113 
00114   if ( it==m_classes.end() )
00115     throw bad_type_identifier();
00116   else
00117     return it->second->create();
00118 } // factory::create()
00119 
00120 /*----------------------------------------------------------------------------*/
00125 template<typename BaseClass, typename IdentifierType>
00126 bool claw::pattern::factory<BaseClass, IdentifierType>::is_known_type
00127 ( const identifier_type& id ) const
00128 {
00129   return m_classes.find(id) != m_classes.end();
00130 } // factory::is_known_type()