User Manual, Developers Guide and API Documentation

StaticFactoryBroker.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 5, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #ifndef WNS_STATICFACTORYBROKER_HPP
00029 #define WNS_STATICFACTORYBROKER_HPP
00030 
00031 #include <WNS/PyConfigViewCreator.hpp>
00032 #include <WNS/StaticFactory.hpp>
00033 #include <WNS/pyconfig/View.hpp>
00034 #include <WNS/container/Registry.hpp>
00035 #include <WNS/Assure.hpp>
00036 
00037 #include <utility>
00038 #include <string>
00039 
00040 namespace wns {
00041 
00048     template <typename ELEMENT, typename CREATOR>
00049     class StaticFactoryBroker
00050     {
00051     };
00052 
00053     template <typename ELEMENT>
00054     class StaticFactoryBroker<ELEMENT, PyConfigViewCreator<ELEMENT> >
00055     {
00062         class RegistryKey :
00063             public std::pair<std::string, pyconfig::View>
00064             {
00065             public:
00066                 RegistryKey(const std::string& s, const pyconfig::View& pyco)
00067                     : std::pair<std::string, pyconfig::View>(s, pyco)
00068                     {}
00069 
00070                 friend std::ostream& operator <<(std::ostream& str, const StaticFactoryBroker<ELEMENT, PyConfigViewCreator<ELEMENT> >::RegistryKey& k)
00071                 {
00072                     str << "(" << k.first << ", " << k.second << ")";
00073                     return str;
00074                 }
00075             };
00076 
00080         typedef container::Registry<RegistryKey, ELEMENT*, container::registry::DeleteOnErase> Registry;
00084         Registry registry;
00085     public:
00089         ~StaticFactoryBroker()
00090         {
00091             registry.clear();
00092         }
00096         ELEMENT*
00097         procure(const std::string name, const pyconfig::View& pyco)
00098         {
00099             typedef PyConfigViewCreator<ELEMENT> ElementCreator;
00100             typedef StaticFactory<ElementCreator> ElementFactory;
00101             assure(ElementFactory::knows(name), "StaticFactoryBroker: Factory does not know '" + name + "'");
00102             const RegistryKey key(name, pyco);
00103             try
00104             {
00105                 return registry.find(key);
00106             }
00107             catch (const typename Registry::UnknownKeyValue&)
00108             {
00109                 ELEMENT* element = ElementFactory::creator(name)->create(pyco);
00110                 registry.insert(key, element);
00111                 return element;
00112             }
00113             catch (...)
00114             {
00115                 throw;
00116             }
00117         }
00118 
00119     };
00120 
00121 
00122     template <typename ELEMENT>
00123     class StaticFactoryBroker<ELEMENT, Creator<ELEMENT> >
00124     {
00128         typedef container::Registry<std::string, ELEMENT*, container::registry::DeleteOnErase> Registry;
00132         Registry registry;
00133     public:
00137         ~StaticFactoryBroker()
00138         {
00139             registry.clear();
00140         }
00141 
00145         ELEMENT*
00146         procure(const std::string name)
00147         {
00148             typedef wns::Creator<ELEMENT> ElementCreator;
00149             typedef StaticFactory<ElementCreator> ElementFactory;
00150             assure(ElementFactory::knows(name), "StaticFactoryBroker: Factory does not know '" + name + "'");
00151             const std::string key(name);
00152             try
00153             {
00154                 return registry.find(key);
00155             }
00156             catch (const typename Registry::UnknownKeyValue&)
00157             {
00158                 ELEMENT* element = ElementFactory::creator(name)->create();
00159                 registry.insert(key, element);
00160                 return element;
00161             }
00162             catch (...)
00163             {
00164                 throw;
00165             }
00166         }
00167     };
00168 
00169 } // wns
00170 
00171 #define STATIC_FACTORY_BROKER_REGISTER(CLASS, INTERFACE, NAME) STATIC_FACTORY_REGISTER_WITH_CREATOR(CLASS, INTERFACE, NAME, ::wns::PyConfigViewCreator)
00172 #define STATIC_FACTORY_BROKER_REGISTER_PLAIN(CLASS, INTERFACE, NAME) STATIC_FACTORY_REGISTER(CLASS, INTERFACE, NAME)
00173 
00174 #endif // NOT defined WNS_STATICFACTORYBROKER_HPP

Generated on Fri May 25 03:31:56 2012 for openWNS by  doxygen 1.5.5