00001 /* ES40 emulator. 00002 * Copyright (C) 2007-2008 by the ES40 Emulator Project 00003 * 00004 * WWW : http://sourceforge.net/projects/es40 00005 * E-mail : camiel@camicom.com 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU General Public License 00009 * as published by the Free Software Foundation; either version 2 00010 * of the License, or (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00020 * 00021 * Although this is not required, the author would appreciate being notified of, 00022 * and receiving any modifications you may make to the source code that might serve 00023 * the general public. 00024 */ 00025 00037 // 00038 // AutoPtr.h 00039 // 00040 // $Id: AutoPtr.h,v 1.2 2008/04/01 11:44:44 iamcamiel Exp $ 00041 // 00042 // Library: Foundation 00043 // Package: Core 00044 // Module: AutoPtr 00045 // 00046 // Definition of the AutoPtr template class. 00047 // 00048 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. 00049 // and Contributors. 00050 // 00051 // Permission is hereby granted, free of charge, to any person or organization 00052 // obtaining a copy of the software and accompanying documentation covered by 00053 // this license (the "Software") to use, reproduce, display, distribute, 00054 // execute, and transmit the Software, and to prepare derivative works of the 00055 // Software, and to permit third-parties to whom the Software is furnished to 00056 // do so, all subject to the following: 00057 // 00058 // The copyright notices in the Software and this entire statement, including 00059 // the above license grant, this restriction and the following disclaimer, 00060 // must be included in all copies of the Software, in whole or in part, and 00061 // all derivative works of the Software, unless such copies or derivative 00062 // works are solely in the form of machine-executable object code generated by 00063 // a source language processor. 00064 // 00065 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00066 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00067 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 00068 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 00069 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 00070 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00071 // DEALINGS IN THE SOFTWARE. 00072 // 00073 00074 00075 #ifndef Foundation_AutoPtr_INCLUDED 00076 #define Foundation_AutoPtr_INCLUDED 00077 00078 00079 #include "VMS/Foundation.h" 00080 #include "VMS/Exception.h" 00081 #include <algorithm> 00082 00083 00084 namespace Poco { 00085 00086 00087 template <class C> 00088 class AutoPtr 00120 { 00121 public: 00122 AutoPtr(): _ptr(0) 00123 { 00124 } 00125 00126 AutoPtr(C* ptr): _ptr(ptr) 00127 { 00128 } 00129 00130 AutoPtr(C* ptr, bool shared): _ptr(ptr) 00131 { 00132 if (shared && _ptr) _ptr->duplicate(); 00133 } 00134 00135 AutoPtr(const AutoPtr& ptr): _ptr(ptr._ptr) 00136 { 00137 if (_ptr) _ptr->duplicate(); 00138 } 00139 00140 template <class Other> 00141 AutoPtr(const AutoPtr<Other>& ptr): _ptr(const_cast<Other*>(ptr.get())) 00142 { 00143 if (_ptr) _ptr->duplicate(); 00144 } 00145 00146 ~AutoPtr() 00147 { 00148 if (_ptr) _ptr->release(); 00149 } 00150 00151 AutoPtr& assign(C* ptr) 00152 { 00153 if (_ptr != ptr) 00154 { 00155 if (_ptr) _ptr->release(); 00156 _ptr = ptr; 00157 } 00158 return *this; 00159 } 00160 00161 AutoPtr& assign(C* ptr, bool shared) 00162 { 00163 if (_ptr != ptr) 00164 { 00165 if (_ptr) _ptr->release(); 00166 _ptr = ptr; 00167 if (shared && _ptr) _ptr->duplicate(); 00168 } 00169 return *this; 00170 } 00171 00172 AutoPtr& assign(const AutoPtr& ptr) 00173 { 00174 if (&ptr != this) 00175 { 00176 if (_ptr) _ptr->release(); 00177 _ptr = ptr._ptr; 00178 if (_ptr) _ptr->duplicate(); 00179 } 00180 return *this; 00181 } 00182 00183 template <class Other> 00184 AutoPtr& assign(const AutoPtr<Other>& ptr) 00185 { 00186 if (ptr.get() != _ptr) 00187 { 00188 if (_ptr) _ptr->release(); 00189 _ptr = const_cast<Other*>(ptr.get()); 00190 if (_ptr) _ptr->duplicate(); 00191 } 00192 return *this; 00193 } 00194 00195 AutoPtr& operator = (C* ptr) 00196 { 00197 return assign(ptr); 00198 } 00199 00200 AutoPtr& operator = (const AutoPtr& ptr) 00201 { 00202 return assign(ptr); 00203 } 00204 00205 template <class Other> 00206 AutoPtr& operator = (const AutoPtr<Other>& ptr) 00207 { 00208 return assign<Other>(ptr); 00209 } 00210 00211 void swap(AutoPtr& ptr) 00212 { 00213 std::swap(_ptr, ptr._ptr); 00214 } 00215 00216 template <class Other> 00217 AutoPtr<Other> cast() const 00224 { 00225 Other* pOther = dynamic_cast<Other*>(_ptr); 00226 return AutoPtr<Other>(pOther, true); 00227 } 00228 00229 template <class Other> 00230 AutoPtr<Other> unsafeCast() const 00236 { 00237 Other* pOther = static_cast<Other*>(_ptr); 00238 return AutoPtr<Other>(pOther, true); 00239 } 00240 00241 C* operator -> () 00242 { 00243 if (_ptr) 00244 return _ptr; 00245 else 00246 throw NullPointerException(); 00247 } 00248 00249 const C* operator -> () const 00250 { 00251 if (_ptr) 00252 return _ptr; 00253 else 00254 throw NullPointerException(); 00255 } 00256 00257 C& operator * () 00258 { 00259 if (_ptr) 00260 return *_ptr; 00261 else 00262 throw NullPointerException(); 00263 } 00264 00265 const C& operator * () const 00266 { 00267 if (_ptr) 00268 return *_ptr; 00269 else 00270 throw NullPointerException(); 00271 } 00272 00273 C* get() 00274 { 00275 return _ptr; 00276 } 00277 00278 const C* get() const 00279 { 00280 return _ptr; 00281 } 00282 00283 operator C* () 00284 { 00285 return _ptr; 00286 } 00287 00288 operator const C* () const 00289 { 00290 return _ptr; 00291 } 00292 00293 bool operator ! () const 00294 { 00295 return _ptr == 0; 00296 } 00297 00298 bool isNull() const 00299 { 00300 return _ptr == 0; 00301 } 00302 00303 C* duplicate() 00304 { 00305 if (_ptr) _ptr->duplicate(); 00306 return _ptr; 00307 } 00308 00309 bool operator == (const AutoPtr& ptr) const 00310 { 00311 return _ptr == ptr._ptr; 00312 } 00313 00314 bool operator == (const C* ptr) const 00315 { 00316 return _ptr == ptr; 00317 } 00318 00319 bool operator == (C* ptr) const 00320 { 00321 return _ptr == ptr; 00322 } 00323 00324 bool operator != (const AutoPtr& ptr) const 00325 { 00326 return _ptr != ptr._ptr; 00327 } 00328 00329 bool operator != (const C* ptr) const 00330 { 00331 return _ptr != ptr; 00332 } 00333 00334 bool operator != (C* ptr) const 00335 { 00336 return _ptr != ptr; 00337 } 00338 00339 bool operator < (const AutoPtr& ptr) const 00340 { 00341 return _ptr < ptr._ptr; 00342 } 00343 00344 bool operator < (const C* ptr) const 00345 { 00346 return _ptr < ptr; 00347 } 00348 00349 bool operator < (C* ptr) const 00350 { 00351 return _ptr < ptr; 00352 } 00353 00354 bool operator <= (const AutoPtr& ptr) const 00355 { 00356 return _ptr <= ptr._ptr; 00357 } 00358 00359 bool operator <= (const C* ptr) const 00360 { 00361 return _ptr <= ptr; 00362 } 00363 00364 bool operator <= (C* ptr) const 00365 { 00366 return _ptr <= ptr; 00367 } 00368 00369 bool operator > (const AutoPtr& ptr) const 00370 { 00371 return _ptr > ptr._ptr; 00372 } 00373 00374 bool operator > (const C* ptr) const 00375 { 00376 return _ptr > ptr; 00377 } 00378 00379 bool operator > (C* ptr) const 00380 { 00381 return _ptr > ptr; 00382 } 00383 00384 bool operator >= (const AutoPtr& ptr) const 00385 { 00386 return _ptr >= ptr._ptr; 00387 } 00388 00389 bool operator >= (const C* ptr) const 00390 { 00391 return _ptr >= ptr; 00392 } 00393 00394 bool operator >= (C* ptr) const 00395 { 00396 return _ptr >= ptr; 00397 } 00398 00399 private: 00400 C* _ptr; 00401 }; 00402 00403 00404 template <class C> 00405 inline void swap(AutoPtr<C>& p1, AutoPtr<C>& p2) 00406 { 00407 p1.swap(p2); 00408 } 00409 00410 00411 } // namespace Poco 00412 00413 00414 #endif // Foundation_AutoPtr_INCLUDED