User Manual, Developers Guide and API Documentation

UntypedRegistry.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. 16, 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_CONTAINER_UNTYPEDREGISTRY_HPP
00029 #define WNS_CONTAINER_UNTYPEDREGISTRY_HPP
00030 
00031 #include <memory>
00032 #include <WNS/container/Registry.hpp>
00033 #include <WNS/Chamaeleon.hpp>
00034 
00035 namespace wns { namespace container {
00036 
00041     template <typename KEY,
00042               typename SORTINGPOLICY = std::less<KEY> >
00043     class UntypedRegistry
00044     {
00045         // ATTENTION: CLEANUPPOLICY here refers to cleanup of
00046         // ChamaeleonBase type, which must be deleted in any case
00047         typedef wns::container::Registry<KEY,
00048                                          ChamaeleonBase*,
00049                                          wns::container::registry::DeleteOnErase,
00050                                          SORTINGPOLICY> InstanceContainer;
00051     public:
00052         typedef KEY KeyType;
00053         typedef typename InstanceContainer::UnknownKeyValue UnknownKeyValue;
00054         typedef typename InstanceContainer::DuplicateKeyValue DuplicateKeyValue;
00055         typedef typename InstanceContainer::KeyList KeyList;
00056         typedef ChamaeleonBase::BadCast BadCast;
00057 
00061         UntypedRegistry() :
00062             instances_()
00063         {
00064         }
00065 
00069         virtual
00070         ~UntypedRegistry()
00071         {
00072         }
00073 
00077         template<typename ELEMENT>
00078         void
00079         insert(const KEY& key, ELEMENT element)
00080             throw(DuplicateKeyValue)
00081         {
00082             // to avoid memory leak in case of exception we use an auto_ptr
00083             std::auto_ptr<ChamaeleonBase>
00084                 chamaeleonPtr(new Chamaeleon<ELEMENT>(element));
00085             instances_.insert(key, chamaeleonPtr.get());
00086             // release the auto_ptr, everything went fine, registry takes
00087             // control
00088             chamaeleonPtr.release();
00089         }
00090 
00094         virtual bool
00095         knows(const KEY& key) const
00096         {
00097             return instances_.knows(key);
00098         }
00099 
00104         template <typename T>
00105         T
00106         find(const KEY& key) const
00107             throw(UnknownKeyValue, BadCast)
00108         {
00109 
00110             ChamaeleonBase* cb = instances_.find(key);
00111             return cb->downCast<T>()->unHide();
00112         }
00113 
00127         template <typename ELEMENT>
00128         void
00129         update(const KeyType& key, ELEMENT element)
00130             throw(UnknownKeyValue)
00131         {
00132             // to avoid memory leak in case of exception we use an auto_ptr
00133             std::auto_ptr<ChamaeleonBase>
00134                 chamaeleonPtr(new Chamaeleon<ELEMENT>(element));
00135             instances_.update(key, chamaeleonPtr.get());
00136             // release the auto_ptr, everything went fine, registry takes
00137             // control
00138             chamaeleonPtr.release();
00139         }
00140 
00144         virtual void
00145         erase(const KEY& key)
00146             throw(UnknownKeyValue)
00147         {
00148             instances_.erase(key);
00149         }
00150 
00151 
00155         virtual void
00156         clear()
00157         {
00158             instances_.clear();
00159         }
00160 
00164         virtual bool
00165         empty() const
00166             throw()
00167         {
00168             return instances_.empty();
00169         }
00170 
00174         virtual size_t
00175         size() const
00176             throw()
00177         {
00178             return instances_.size();
00179         }
00180 
00181         virtual KeyList
00182         keys() const
00183             throw()
00184         {
00185             return instances_.keys();
00186         }
00187 
00188     private:
00189         // Disallow Copy Constructor
00190         UntypedRegistry(const UntypedRegistry&);
00191 
00192         InstanceContainer instances_;
00193     };
00194 
00195 } // container
00196 } // wns
00197 #endif // NOT defined WNS_CONTAINER_UNTYPEDREGISTRY_HPP
00198 

Generated on Sat May 26 03:31:35 2012 for openWNS by  doxygen 1.5.5