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: 00134 // 00136 // 00137 CpClassPtr (); 00138 // 00140 // 00141 explicit CpClassPtr (T* rhs); 00142 ~CpClassPtr (); 00143 00152 CpClassPtr (const CpClassPtr<T>& rhs); 00153 00157 CpClassPtr<T>& operator= (T* rhs); 00158 00167 CpClassPtr<T>& operator= (const CpClassPtr<T>& rhs); 00168 // 00170 // 00171 T& operator* () const; 00172 bool isNull() const; 00173 T* release (); 00174 bool operator== (const CpClassPtr<T>& rhs) const; 00175 bool operator!= (const CpClassPtr<T>& rhs) const; 00176 T* operator-> () const; 00177 00178 protected: 00179 00180 T* ptr; 00181 00182 }; 00183 00184 // 00185 // : 00187 00197 template<class T> 00198 class LnPtr 00199 { 00200 public: 00201 // 00203 // 00204 LnPtr (); 00205 // 00207 // 00208 explicit LnPtr (T* rhs); 00209 00215 LnPtr<T>& operator= (const LnPtr<T>& rhs); 00216 00221 LnPtr<T>& operator= (T* rhs); 00222 00226 ~LnPtr (); 00227 // 00229 // 00230 bool unique () const; 00231 // 00233 // 00234 int linkCount () const; 00235 00241 T& operator* () const; 00242 // 00244 // 00245 bool isNull () const; 00246 // 00248 // 00249 bool operator== (const LnPtr<T>& rhs) const; 00250 // 00252 // 00253 bool operator!= (const LnPtr<T>& rhs) const; 00254 00255 protected: 00256 T* ptr; 00257 00258 private: 00259 UseCount ucnt; 00260 }; 00261 00262 // 00263 // : 00265 00273 template<class T> 00274 class LnClassPtr 00275 { 00276 public: 00277 // 00279 // 00280 LnClassPtr (); 00281 // 00283 // 00284 explicit LnClassPtr (T* rhs); 00285 00291 LnClassPtr<T>& operator= (const LnClassPtr<T>& rhs); 00292 00297 LnClassPtr<T>& operator= (T* rhs); 00298 // 00300 // 00301 ~LnClassPtr(); 00302 bool unique() const; 00303 int linkCount () const; 00304 T& operator* () const; 00305 bool isNull () const; 00306 bool operator== (const LnClassPtr<T>& rhs) const; 00307 bool operator!= (const LnClassPtr<T>& rhs) const; 00308 T* operator->() const; 00309 00310 protected: 00311 T* ptr; 00312 00313 private: 00314 UseCount ucnt; 00315 00316 }; 00317 00318 template <class T> 00319 inline 00320 CpPtr<T>::CpPtr () 00321 : 00322 ptr(0) 00323 {} 00324 00325 template <class T> 00326 inline 00327 CpPtr<T>::CpPtr (T* rhs) 00328 : 00329 ptr(rhs) 00330 {} 00331 00332 template <class T> 00333 CpPtr<T>::~CpPtr() 00334 { 00335 delete ptr; 00336 } 00337 00338 template <class T> 00339 inline 00340 bool 00341 CpPtr<T>::isNull () const 00342 { 00343 return ptr == 0; 00344 } 00345 00346 template <class T> 00347 CpPtr<T>::CpPtr (const CpPtr<T>& rhs) 00348 { 00349 ptr = rhs.isNull() ? 0 : new T(*rhs.ptr); 00350 } 00351 00352 template <class T> 00353 CpPtr<T>& 00354 CpPtr<T>::operator= (const CpPtr<T>& rhs) 00355 { 00356 if (!(ptr == rhs.ptr)) 00357 { 00358 delete ptr; 00359 ptr = rhs.isNull() ? 0 : new T(*rhs.ptr); 00360 } 00361 return *this; 00362 } 00363 00364 template <class T> 00365 CpPtr<T>& 00366 CpPtr<T>::operator= (T* rhs) 00367 { 00368 delete ptr; 00369 ptr = rhs; 00370 return *this; 00371 } 00372 00373 template <class T> 00374 inline 00375 T& 00376 CpPtr<T>::operator* () const 00377 { 00378 BL_ASSERT(ptr != 0); 00379 return *ptr; 00380 } 00381 00382 template <class T> 00383 T* 00384 CpPtr<T>::release () 00385 { 00386 T* old = ptr; 00387 ptr = 0; 00388 return old; 00389 } 00390 00391 template <class T> 00392 inline 00393 bool 00394 CpPtr<T>::operator== (const CpPtr<T>& rhs) const 00395 { 00396 return ptr == rhs.ptr; 00397 } 00398 00399 template <class T> 00400 inline 00401 bool 00402 CpPtr<T>::operator!= (const CpPtr<T>& rhs) const 00403 { 00404 return ptr != rhs.ptr; 00405 } 00406 00407 template <class T> 00408 inline 00409 CpClassPtr<T>::CpClassPtr () : ptr(0) 00410 {} 00411 00412 template <class T> 00413 inline 00414 CpClassPtr<T>::CpClassPtr (T* rhs) : ptr(rhs) 00415 {} 00416 00417 template <class T> 00418 CpClassPtr<T>::~CpClassPtr() 00419 { 00420 delete ptr; 00421 } 00422 00423 template <class T> 00424 inline 00425 bool 00426 CpClassPtr<T>::isNull () const 00427 { 00428 return ptr == 0; 00429 } 00430 00431 template <class T> 00432 CpClassPtr<T>::CpClassPtr (const CpClassPtr<T>& rhs) 00433 { 00434 ptr = rhs.isNull() ? 0 : new T(*rhs.ptr); 00435 } 00436 00437 template <class T> 00438 CpClassPtr<T>& 00439 CpClassPtr<T>::operator= (const CpClassPtr<T>& rhs) 00440 { 00441 if (!(ptr == rhs.ptr)) 00442 { 00443 delete ptr; 00444 ptr = rhs.isNull() ? 0 : new T(*rhs.ptr); 00445 } 00446 return *this; 00447 } 00448 00449 template <class T> 00450 CpClassPtr<T>& 00451 CpClassPtr<T>::operator= (T* rhs) 00452 { 00453 delete ptr; 00454 ptr = rhs; 00455 return *this; 00456 } 00457 00458 00459 template <class T> 00460 inline 00461 T& 00462 CpClassPtr<T>::operator* () const 00463 { 00464 BL_ASSERT(ptr != 0); 00465 return *ptr; 00466 } 00467 00468 template <class T> 00469 T* 00470 CpClassPtr<T>::release () 00471 { 00472 T* old = ptr; 00473 ptr = 0; 00474 return old; 00475 } 00476 00477 template <class T> 00478 inline 00479 bool 00480 CpClassPtr<T>::operator== (const CpClassPtr<T>& rhs) const 00481 { 00482 return ptr == rhs.ptr; 00483 } 00484 00485 template <class T> 00486 inline 00487 bool 00488 CpClassPtr<T>::operator!= (const CpClassPtr<T>& rhs) const 00489 { 00490 return ptr != rhs.ptr; 00491 } 00492 00493 template <class T> 00494 inline 00495 T* 00496 CpClassPtr<T>::operator-> () const 00497 { 00498 return ptr; 00499 } 00500 00501 template <class T> 00502 LnPtr<T>::LnPtr () 00503 : 00504 ptr(0) 00505 {} 00506 00507 template <class T> 00508 LnPtr<T>::LnPtr(T* rhs) 00509 : 00510 ptr(rhs) 00511 {} 00512 00513 template <class T> 00514 LnPtr<T>& 00515 LnPtr<T>::operator= (T* rhs) 00516 { 00517 if (unique()) 00518 delete ptr; 00519 ptr = rhs; 00520 ucnt = UseCount(); 00521 return *this; 00522 } 00523 00524 template <class T> 00525 inline 00526 bool 00527 LnPtr<T>::unique () const 00528 { 00529 return ucnt.unique(); 00530 } 00531 00532 template <class T> 00533 LnPtr<T>::~LnPtr () 00534 { 00535 if (ucnt.unique()) 00536 delete ptr; 00537 } 00538 00539 template <class T> 00540 LnPtr<T>& 00541 LnPtr<T>::operator= (const LnPtr<T>& rhs) 00542 { 00543 if (ptr != rhs.ptr) 00544 { 00545 if (unique()) 00546 delete ptr; 00547 ptr = rhs.ptr; 00548 ucnt = rhs.ucnt; 00549 } 00550 return *this; 00551 } 00552 00553 template <class T> 00554 inline 00555 int 00556 LnPtr<T>::linkCount () const 00557 { 00558 return ucnt.linkCount(); 00559 } 00560 00561 template <class T> 00562 inline 00563 T& 00564 LnPtr<T>::operator* () const 00565 { 00566 BL_ASSERT(ptr != 0); 00567 return *ptr; 00568 } 00569 00570 template <class T> 00571 inline 00572 bool 00573 LnPtr<T>::isNull () const 00574 { 00575 return ptr == 0; 00576 } 00577 00578 template <class T> 00579 bool 00580 LnPtr<T>::operator== (const LnPtr<T>& rhs) const 00581 { 00582 return ptr == rhs.ptr; 00583 } 00584 00585 template <class T> 00586 bool 00587 LnPtr<T>::operator!= (const LnPtr<T>& rhs) const 00588 { 00589 return ptr != rhs.ptr; 00590 } 00591 00592 template <class T> 00593 LnClassPtr<T>::LnClassPtr () 00594 {} 00595 00596 template <class T> 00597 LnClassPtr<T>::LnClassPtr (T* rhs) : ptr(rhs) 00598 {} 00599 00600 template <class T> 00601 inline 00602 bool 00603 LnClassPtr<T>::unique () const 00604 { 00605 return ucnt.unique(); 00606 } 00607 00608 template <class T> 00609 inline 00610 int 00611 LnClassPtr<T>::linkCount () const 00612 { 00613 return ucnt.linkCount(); 00614 } 00615 00616 template <class T> 00617 LnClassPtr<T>& 00618 LnClassPtr<T>::operator= (const LnClassPtr<T>& rhs) 00619 { 00620 if (ptr != rhs.ptr) 00621 { 00622 if (unique()) 00623 delete ptr; 00624 ptr = rhs.ptr; 00625 ucnt = rhs.ucnt; 00626 } 00627 return *this; 00628 } 00629 00630 00631 template <class T> 00632 LnClassPtr<T>& 00633 LnClassPtr<T>::operator= (T* rhs) 00634 { 00635 if (unique()) 00636 delete ptr; 00637 ptr = rhs; 00638 ucnt = UseCount(); 00639 return *this; 00640 } 00641 00642 template <class T> 00643 LnClassPtr<T>::~LnClassPtr () 00644 { 00645 if (ucnt.unique()) 00646 delete ptr; 00647 } 00648 00649 template <class T> 00650 inline 00651 T& 00652 LnClassPtr<T>::operator* () const 00653 { 00654 BL_ASSERT(ptr != 0); 00655 return *ptr; 00656 } 00657 00658 template <class T> 00659 inline 00660 bool 00661 LnClassPtr<T>::isNull () const 00662 { 00663 return ptr == 0; 00664 } 00665 00666 00667 template <class T> 00668 bool 00669 LnClassPtr<T>::operator== (const LnClassPtr<T>& rhs) const 00670 { 00671 return ptr == rhs.ptr; 00672 } 00673 00674 template <class T> 00675 bool 00676 LnClassPtr<T>::operator!= (const LnClassPtr<T>& rhs) const 00677 { 00678 return ptr != rhs.ptr; 00679 } 00680 template <class T> 00681 inline 00682 T* 00683 LnClassPtr<T>::operator->() const 00684 { 00685 return ptr; 00686 } 00687 00688 #endif /*BL_POINTERS_H*/