00001 /* 00002 ** (c) 1996-2000 The Regents of the University of California (through 00003 ** E.O. Lawrence Berkeley National Laboratory), subject to approval by 00004 ** the U.S. Department of Energy. Your use of this software is under 00005 ** license -- the license agreement is attached and included in the 00006 ** directory as license.txt or you may contact Berkeley Lab's Technology 00007 ** Transfer Department at TTD@lbl.gov. NOTICE OF U.S. GOVERNMENT RIGHTS. 00008 ** The Software was developed under funding from the U.S. Government 00009 ** which consequently retains certain rights as follows: the 00010 ** U.S. Government has been granted for itself and others acting on its 00011 ** behalf a paid-up, nonexclusive, irrevocable, worldwide license in the 00012 ** Software to reproduce, prepare derivative works, and perform publicly 00013 ** and display publicly. Beginning five (5) years after the date 00014 ** permission to assert copyright is obtained from the U.S. Department of 00015 ** Energy, and subject to any subsequent five (5) year renewals, the 00016 ** U.S. Government is granted for itself and others acting on its behalf 00017 ** a paid-up, nonexclusive, irrevocable, worldwide license in the 00018 ** Software to reproduce, prepare derivative works, distribute copies to 00019 ** the public, perform publicly and display publicly, and to permit 00020 ** others to do so. 00021 */ 00022 00023 #ifndef BL_POINTERS_H 00024 #define BL_POINTERS_H 00025 // 00026 // $Id: Pointers.H,v 1.16 2001/07/31 22:43:19 lijewski Exp $ 00027 // 00028 #include <BLassert.H> 00029 #include <BoxLib.H> 00030 #include <UseCount.H> 00031 // 00032 // : 00034 00048 template <class T> 00049 class CpPtr 00050 { 00051 public: 00052 // 00054 // 00055 CpPtr (); 00056 // 00058 // 00059 explicit CpPtr (T* rhs); 00060 // 00062 // 00063 ~CpPtr (); 00064 00073 CpPtr (const CpPtr<T>& rhs); 00074 00078 CpPtr<T>& operator= (T* rhs); 00079 00088 CpPtr<T>& operator= (const CpPtr<T>& rhs); 00089 00095 T& operator* () const; 00096 // 00098 // 00099 bool isNull () const; 00100 // 00102 // 00103 T* release (); 00104 // 00106 // 00107 bool operator== (const CpPtr<T>& rhs) const; 00108 // 00110 // 00111 bool operator!= (const CpPtr<T>& rhs) const; 00112 00113 protected: 00114 00115 T* ptr; 00116 }; 00117 00118 // 00119 // : 00121 00130 template<class T> 00131 class CpClassPtr 00132 : 00133 public CpPtr<T> 00134 { 00135 public: 00136 // 00138 // 00139 CpClassPtr (); 00140 // 00142 // 00143 explicit CpClassPtr (T* rhs); 00144 00153 CpClassPtr (const CpClassPtr<T>& rhs); 00154 00158 CpClassPtr<T>& operator= (T* rhs); 00159 00168 CpClassPtr<T>& operator= (const CpClassPtr<T>& rhs); 00169 // 00171 // 00172 T* operator-> () const; 00173 }; 00174 00175 // 00176 // : 00178 00188 template<class T> 00189 class LnPtr 00190 { 00191 public: 00192 // 00194 // 00195 LnPtr (); 00196 // 00198 // 00199 explicit LnPtr (T* rhs); 00200 00206 LnPtr<T>& operator= (const LnPtr<T>& rhs); 00207 00212 LnPtr<T>& operator= (T* rhs); 00213 00217 ~LnPtr (); 00218 // 00220 // 00221 bool unique () const; 00222 // 00224 // 00225 int linkCount () const; 00226 00232 T& operator* () const; 00233 // 00235 // 00236 bool isNull () const; 00237 // 00239 // 00240 bool operator== (const LnPtr<T>& rhs) const; 00241 // 00243 // 00244 bool operator!= (const LnPtr<T>& rhs) const; 00245 00246 protected: 00247 T* ptr; 00248 00249 private: 00250 UseCount ucnt; 00251 }; 00252 00253 // 00254 // : 00256 00264 template<class T> 00265 class LnClassPtr 00266 : 00267 public LnPtr<T> 00268 { 00269 public: 00270 // 00272 // 00273 LnClassPtr (); 00274 // 00276 // 00277 explicit LnClassPtr (T* rhs); 00278 00284 LnClassPtr<T>& operator= (const LnClassPtr<T>& rhs); 00285 00290 LnClassPtr<T>& operator= (T* rhs); 00291 // 00293 // 00294 T* operator->() const; 00295 }; 00296 00297 template <class T> 00298 inline 00299 CpPtr<T>::CpPtr () 00300 : 00301 ptr(0) 00302 {} 00303 00304 template <class T> 00305 inline 00306 CpPtr<T>::CpPtr (T* rhs) 00307 : 00308 ptr(rhs) 00309 {} 00310 00311 template <class T> 00312 CpPtr<T>::~CpPtr() 00313 { 00314 delete ptr; 00315 } 00316 00317 template <class T> 00318 inline 00319 bool 00320 CpPtr<T>::isNull () const 00321 { 00322 return ptr == 0; 00323 } 00324 00325 template <class T> 00326 CpPtr<T>::CpPtr (const CpPtr<T>& rhs) 00327 { 00328 ptr = rhs.isNull() ? 0 : new T(*rhs.ptr); 00329 } 00330 00331 template <class T> 00332 CpPtr<T>& 00333 CpPtr<T>::operator= (const CpPtr<T>& rhs) 00334 { 00335 if (!(ptr == rhs.ptr)) 00336 { 00337 delete ptr; 00338 ptr = rhs.isNull() ? 0 : new T(*rhs.ptr); 00339 } 00340 return *this; 00341 } 00342 00343 template <class T> 00344 CpPtr<T>& 00345 CpPtr<T>::operator= (T* rhs) 00346 { 00347 delete ptr; 00348 ptr = rhs; 00349 return *this; 00350 } 00351 00352 template <class T> 00353 inline 00354 T& 00355 CpPtr<T>::operator* () const 00356 { 00357 BL_ASSERT(ptr != 0); 00358 return *ptr; 00359 } 00360 00361 template <class T> 00362 T* 00363 CpPtr<T>::release () 00364 { 00365 T* old = ptr; 00366 ptr = 0; 00367 return old; 00368 } 00369 00370 template <class T> 00371 inline 00372 bool 00373 CpPtr<T>::operator== (const CpPtr<T>& rhs) const 00374 { 00375 return ptr == rhs.ptr; 00376 } 00377 00378 template <class T> 00379 inline 00380 bool 00381 CpPtr<T>::operator!= (const CpPtr<T>& rhs) const 00382 { 00383 return ptr != rhs.ptr; 00384 } 00385 00386 template <class T> 00387 inline 00388 CpClassPtr<T>::CpClassPtr () 00389 : 00390 CpPtr<T>() 00391 {} 00392 00393 template <class T> 00394 inline 00395 CpClassPtr<T>::CpClassPtr (T* rhs) 00396 : 00397 CpPtr<T>(rhs) 00398 {} 00399 00400 template <class T> 00401 CpClassPtr<T>::CpClassPtr (const CpClassPtr<T>& rhs) 00402 : 00403 CpPtr<T>(rhs) 00404 {} 00405 00406 template <class T> 00407 inline 00408 CpClassPtr<T>& 00409 CpClassPtr<T>::operator= (T* rhs) 00410 { 00411 CpPtr<T>::operator= (rhs); 00412 return *this; 00413 } 00414 00415 template <class T> 00416 inline 00417 CpClassPtr<T>& 00418 CpClassPtr<T>::operator= (const CpClassPtr<T>& rhs) 00419 { 00420 CpPtr<T>::operator= (rhs); 00421 return *this; 00422 } 00423 00424 template <class T> 00425 inline 00426 T* 00427 CpClassPtr<T>::operator-> () const 00428 { 00429 return ptr; 00430 } 00431 00432 template <class T> 00433 LnPtr<T>::LnPtr () 00434 : 00435 ptr(0) 00436 {} 00437 00438 template <class T> 00439 LnPtr<T>::LnPtr(T* rhs) 00440 : 00441 ptr(rhs) 00442 {} 00443 00444 template <class T> 00445 LnPtr<T>& 00446 LnPtr<T>::operator= (T* rhs) 00447 { 00448 if (unique()) 00449 delete ptr; 00450 ptr = rhs; 00451 ucnt = UseCount(); 00452 return *this; 00453 } 00454 00455 template <class T> 00456 inline 00457 bool 00458 LnPtr<T>::unique () const 00459 { 00460 return ucnt.unique(); 00461 } 00462 00463 template <class T> 00464 LnPtr<T>::~LnPtr () 00465 { 00466 if (ucnt.unique()) 00467 delete ptr; 00468 } 00469 00470 template <class T> 00471 LnPtr<T>& 00472 LnPtr<T>::operator= (const LnPtr<T>& rhs) 00473 { 00474 if (ptr != rhs.ptr) 00475 { 00476 if (unique()) 00477 delete ptr; 00478 ptr = rhs.ptr; 00479 ucnt = rhs.ucnt; 00480 } 00481 return *this; 00482 } 00483 00484 template <class T> 00485 inline 00486 int 00487 LnPtr<T>::linkCount () const 00488 { 00489 return ucnt.linkCount(); 00490 } 00491 00492 template <class T> 00493 inline 00494 T& 00495 LnPtr<T>::operator* () const 00496 { 00497 BL_ASSERT(ptr != 0); 00498 return *ptr; 00499 } 00500 00501 template <class T> 00502 inline 00503 bool 00504 LnPtr<T>::isNull () const 00505 { 00506 return ptr == 0; 00507 } 00508 00509 template <class T> 00510 bool 00511 LnPtr<T>::operator== (const LnPtr<T>& rhs) const 00512 { 00513 return ptr == rhs.ptr; 00514 } 00515 00516 template <class T> 00517 bool 00518 LnPtr<T>::operator!= (const LnPtr<T>& rhs) const 00519 { 00520 return ptr != rhs.ptr; 00521 } 00522 00523 template <class T> 00524 LnClassPtr<T>::LnClassPtr () 00525 {} 00526 00527 template <class T> 00528 LnClassPtr<T>::LnClassPtr (T* rhs) 00529 : 00530 LnPtr<T>(rhs) 00531 {} 00532 00533 template <class T> 00534 LnClassPtr<T>& 00535 LnClassPtr<T>::operator= (const LnClassPtr<T>& rhs) 00536 { 00537 LnPtr<T>::operator=(rhs); 00538 return *this; 00539 } 00540 00541 template <class T> 00542 inline 00543 LnClassPtr<T>& 00544 LnClassPtr<T>::operator= (T* rhs) 00545 { 00546 LnPtr<T>::operator=(rhs); 00547 return *this; 00548 } 00549 00550 template <class T> 00551 inline 00552 T* 00553 LnClassPtr<T>::operator->() const 00554 { 00555 return ptr; 00556 } 00557 00558 #endif /*BL_POINTERS_H*/