1 /* _________________________________________________________________________ 2 * 3 * UTILIB: A utility library for developing portable C++ codes. 4 * Copyright (c) 2008 Sandia Corporation. 5 * This software is distributed under the BSD License. 6 * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, 7 * the U.S. Government retains certain rights in this software. 8 * For more information, see the README file in the top UTILIB directory. 9 * _________________________________________________________________________ 10 */ 11 12 #ifndef utilib_SmartHandle_h 13 #define utilib_SmartHandle_h 14 15 #if 0 16 #include <utilib/SmartPtr.h> 17 18 namespace utilib { 19 20 template <class Type> 21 class SmartHandle 22 { 23 public: 24 25 SmartHandle(bool initialize=false) 26 { 27 if (initialize) 28 data = new Type(); 29 } 30 31 SmartHandle(const SmartHandle<Type>& other) 32 {data = other.data;} 33 34 ~SmartHandle() {} 35 36 SmartHandle<Type>& operator=(const SmartHandle<Type>& other) 37 {data = other.data; return *this;} 38 39 bool operator==(const SmartHandle<Type>& other) const 40 {return *data == *(other.data);} 41 42 bool operator<(const SmartHandle<Type>& other) const 43 {return *data < *(other.data);} 44 45 Type& operator()() 46 {return *data;} 47 48 const Type& operator()() const 49 {return *data;} 50 51 protected: 52 53 SmartPtr<Type> data; 54 }; 55 56 } 57 58 #else 59 60 #include <utilib/RefCount.h> 61 62 namespace utilib { 63 64 template <class Type> 65 class SmartHandle 66 { 67 public: 68 69 SmartHandle(bool initialize=false) 70 : ref(0) 71 { 72 if (initialize) 73 ref = new RefCount<Type>(new Type(),AssumeOwnership); 74 } 75 SmartHandle(const SmartHandle<Type> & other)76 SmartHandle(const SmartHandle<Type>& other) 77 : ref(0) 78 {*this = other;} 79 ~SmartHandle()80 ~SmartHandle() {if (ref && ref->decrement()) delete ref;} 81 82 SmartHandle<Type>& operator=(const SmartHandle<Type>& other) 83 { 84 if (ref && ref->decrement()) delete ref; 85 ref = other.ref; 86 if (ref) 87 ref->increment(); 88 return *this; 89 } 90 91 bool operator==(const SmartHandle<Type>& other) const 92 {return *(ref->data()) == *(other.ref->data());} 93 94 bool operator<(const SmartHandle<Type>& other) const 95 {return *(ref->data()) < *(other.ref->data());} 96 operator()97 Type& operator()() 98 {return *(ref->data());} 99 operator()100 const Type& operator()() const 101 {return *(ref->data());} 102 103 protected: 104 105 RefCount<Type>* ref; 106 }; 107 108 } 109 110 #endif 111 112 #endif 113