CLAW Library (a C++ Library Absolutely Wonderful) 1.5.5

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-2010 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@yahoo.fr
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 } // factory::~factory()
00072 
00073 /*----------------------------------------------------------------------------*/
00082 template<typename BaseClass, typename IdentifierType>
00083 template<typename T>
00084 bool
00085 claw::pattern::factory<BaseClass, IdentifierType>::register_type
00086 ( const identifier_type& id )
00087 {
00088   typename class_map::iterator it = m_classes.find(id);
00089 
00090   if ( it == m_classes.end() )
00091     {
00092       m_classes[id] = new class_creator<T>;
00093       return true;
00094     }
00095   else
00096     return false;
00097 } // factory::register_type()
00098 
00099 /*----------------------------------------------------------------------------*/
00105 template<typename BaseClass, typename IdentifierType>
00106 typename claw::pattern::factory<BaseClass, IdentifierType>::base_class*
00107 claw::pattern::factory<BaseClass, IdentifierType>::create
00108 ( const identifier_type& id ) const
00109 {
00110   typename class_map::const_iterator it = m_classes.find(id);
00111 
00112   if ( it==m_classes.end() )
00113     throw bad_type_identifier();
00114   else
00115     return it->second->create();
00116 } // factory::create()
00117 
00118 /*----------------------------------------------------------------------------*/
00123 template<typename BaseClass, typename IdentifierType>
00124 bool claw::pattern::factory<BaseClass, IdentifierType>::is_known_type
00125 ( const identifier_type& id ) const
00126 {
00127   return m_classes.find(id) != m_classes.end();
00128 } // factory::is_known_type()