00001 // $Id: Sacado_Handle.hpp,v 1.2 2008/01/22 17:57:42 etphipp Exp $ 00002 // $Source: /space/CVS/Trilinos/packages/sacado/src/Sacado_Handle.hpp,v $ 00003 // @HEADER 00004 // *********************************************************************** 00005 // 00006 // Sacado Package 00007 // Copyright (2006) Sandia Corporation 00008 // 00009 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00010 // license for use of this work by or on behalf of the U.S. Government. 00011 // 00012 // This library is free software; you can redistribute it and/or modify 00013 // it under the terms of the GNU Lesser General Public License as 00014 // published by the Free Software Foundation; either version 2.1 of the 00015 // License, or (at your option) any later version. 00016 // 00017 // This library is distributed in the hope that it will be useful, but 00018 // WITHOUT ANY WARRANTY; without even the implied warranty of 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00020 // Lesser General Public License for more details. 00021 // 00022 // You should have received a copy of the GNU Lesser General Public 00023 // License along with this library; if not, write to the Free Software 00024 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00025 // USA 00026 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps 00027 // (etphipp@sandia.gov). 00028 // 00029 // *********************************************************************** 00030 // @HEADER 00031 00032 #ifndef SACADO_HANDLE_HPP 00033 #define SACADO_HANDLE_HPP 00034 00035 namespace Sacado { 00036 00040 template <typename T> 00041 class Handle { 00042 public: 00043 00045 Handle(T* p) : rep(p), count(new int(1)) {} 00046 00048 Handle(const Handle& h) : rep(h.rep), count(h.count) { (*count)++; } 00049 00051 ~Handle() { decrementRef(); } 00052 00054 T* get() { return rep; } 00055 00057 const T* get() const { return rep; } 00058 00060 void Assign(const Handle& h) { 00061 decrementRef(); 00062 rep = new T(*(h.rep)); 00063 count = new int(1); 00064 } 00065 00067 void makeOwnCopy() { 00068 T *tmp; 00069 if (*count > 1) { 00070 tmp = rep; 00071 (*count)--; 00072 rep = new T(*tmp); 00073 count = new int(1); 00074 } 00075 } 00076 00078 Handle& operator = (const Handle& h) { 00079 if (this != &h) { 00080 decrementRef(); 00081 rep = h.rep; 00082 count = h.count; 00083 (*count)++; 00084 } 00085 return *this; 00086 } 00087 00089 T* operator -> () const { return rep; } 00090 00092 const T& operator * () const { return *rep; } 00093 00095 T& operator * () { return *rep; } 00096 00097 private: 00098 00100 T *rep; 00101 00103 int *count; 00104 00106 void decrementRef() { 00107 (*count)--; 00108 if (*count == 0) { 00109 delete rep; 00110 delete count; 00111 } 00112 } 00113 00114 }; // class Handle 00115 00117 template <typename T> 00118 bool operator==(const Handle<T>& h1, const Handle<T>& h2) { 00119 return h1.get() == h2.get(); 00120 } 00121 00122 } // namespace Sacado 00123 00124 #endif // SACADO_HANDLE_HPP
1.4.7