![]() |
User Manual, Developers Guide and API Documentation |
![]() |
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_CLONEABLE_HPP 00029 #define WNS_CLONEABLE_HPP 00030 00031 #include <WNS/Assure.hpp> 00032 #include <WNS/TypeInfo.hpp> 00033 #include <WNS/Exception.hpp> 00034 #include <ostream> 00035 #include <memory> 00036 00037 namespace wns { 00038 00042 class CloneableInterface 00043 { 00044 public: 00048 virtual 00049 ~CloneableInterface() 00050 {} 00051 00055 virtual CloneableInterface* 00056 clone() const = 0; 00057 }; // class CloneableInterface 00058 00065 template< class T > 00066 class Cloneable : 00067 public virtual CloneableInterface 00068 { 00069 public: 00074 virtual CloneableInterface* 00075 clone() const 00076 { 00077 assure( 00078 TypeInfo::create(*this) == TypeInfo::create<T>(), 00079 "wns::Cloneable: " << 00080 "Cloned and orignal type are different! \n" << 00081 "Orignal type: " << TypeInfo::create(*this) << 00082 " <-> Clone type: " << TypeInfo::create<T>()); 00083 00084 T* t = new T( *(static_cast<const T*>(this) ) ); 00085 00086 return t; 00087 } 00088 }; // class Cloneable 00089 00090 00101 class CloneNotSupported : 00102 public Exception 00103 { 00104 public: 00105 CloneNotSupported() : 00106 Exception("CloneableInterface not supported.") 00107 {} 00108 00109 template <typename T> 00110 CloneNotSupported(T* t) : 00111 Exception("CloneableInterface not supported for: ") 00112 { 00113 (*this) << TypeInfo::create(*t); 00114 } 00115 00116 ~CloneNotSupported() throw() {} 00117 }; 00118 00128 class NotCloneable : 00129 virtual public CloneableInterface 00130 { 00131 public: 00132 virtual CloneableInterface* 00133 clone() const 00134 { 00135 throw CloneNotSupported(this); 00136 } 00137 }; 00138 00148 template <typename T> 00149 T* 00150 clone(T* t) 00151 { 00152 CloneableInterface* ci = t->clone(); 00153 assureType(ci, T*); 00154 // can't use static cast here 00155 return dynamic_cast<T*>(ci); 00156 } 00157 00164 template <typename T> 00165 std::auto_ptr<T> 00166 clone(const std::auto_ptr<T>& t) 00167 { 00168 CloneableInterface* ci = t->clone(); 00169 assureType(ci, T*); 00170 // can't use static cast here 00171 return std::auto_ptr<T>(dynamic_cast<T*>(ci)); 00172 } 00173 } // namespace wns 00174 00175 #endif // WNS_CLONEABLE_HPP 00176 00177
1.5.5