User Manual, Developers Guide and API Documentation

Chamaeleon.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_CHAMAELEON_HPP
00029 #define WNS_CHAMAELEON_HPP
00030 
00031 #include <WNS/TypeInfo.hpp>
00032 #include <WNS/Exception.hpp>
00033 
00034 namespace wns {
00035 
00036     template <typename T> class Chamaeleon;
00037 
00042     class ChamaeleonBase
00043     {
00044     public:
00048         class BadCast :
00049             public Exception
00050         {
00051         public:
00056             BadCast(
00057                 const TypeInfo& expectedType,
00058                 const TypeInfo& actualType) throw();
00059 
00063             virtual
00064             ~BadCast() throw();
00065         };
00066 
00067         // Default copy constructor is ok
00068 
00072         virtual
00073         ~ChamaeleonBase() throw();
00074 
00079         TypeInfo
00080         getTypeInfo() const throw();
00081 
00085         template <typename T>
00086         Chamaeleon<T>*
00087         downCast() throw(BadCast)
00088         {
00089             Chamaeleon<T>* chamaeleon = dynamic_cast<Chamaeleon<T>*>(this);
00090             if(NULL == chamaeleon)
00091             {
00092                 throw BadCast(TypeInfo::create<T>(), getTypeInfo());
00093             }
00094             return chamaeleon;
00095         }
00096 
00097     protected:
00105         explicit
00106         ChamaeleonBase(const TypeInfo& typeInfo) throw();
00107 
00108     private:
00112         TypeInfo typeInfo_;
00113     };
00114 
00138     template <typename VALUETYPE>
00139     class Chamaeleon :
00140         public ChamaeleonBase
00141     {
00145         template<class> friend class Chamaeleon;
00146     public:
00150         typedef VALUETYPE ValueType;
00151 
00160         explicit
00161         Chamaeleon(ValueType data) throw() :
00162             ChamaeleonBase(TypeInfo::create<ValueType>()),
00163             data_(data)
00164         {
00165         }
00166 
00167         // Default copy c'tor is ok
00168 
00172         template <typename U>
00173         explicit
00174         Chamaeleon(const Chamaeleon<U>& other) throw() :
00175             ChamaeleonBase(other),
00176             data_(other.data_)
00177         {
00178         }
00179 
00183         virtual
00184         ~Chamaeleon() throw()
00185         {
00186         }
00187 
00196         ValueType
00197         unHide() const throw()
00198         {
00199             return data_;
00200         }
00201 
00205         template <typename U>
00206         Chamaeleon&
00207         operator =(const Chamaeleon<U>& other) throw()
00208         {
00209             data_ = other.data_;
00210             return *this;
00211         }
00212 
00213     private:
00217         ValueType data_;
00218     };
00219 }
00220 
00221 #endif // NOT defined WNS_CHAMAELEON_HPP

Generated on Tue May 22 03:31:32 2012 for openWNS by  doxygen 1.5.5