Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_Handle.hpp
Go to the documentation of this file.
1 // $Id$
2 // $Source$
3 // @HEADER
4 // ***********************************************************************
5 //
6 // Sacado Package
7 // Copyright (2006) Sandia Corporation
8 //
9 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
10 // license for use of this work by or on behalf of the U.S. Government.
11 //
12 // This library is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as
14 // published by the Free Software Foundation; either version 2.1 of the
15 // License, or (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25 // USA
26 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
27 // (etphipp@sandia.gov).
28 //
29 // ***********************************************************************
30 // @HEADER
31 
32 #ifndef SACADO_HANDLE_HPP
33 #define SACADO_HANDLE_HPP
34 
35 namespace Sacado {
36 
40  template <typename T>
41  class Handle {
42  public:
43 
45  Handle(T* p) : rep(p), count(new int(1)) {}
46 
48  Handle(const Handle& h) : rep(h.rep), count(h.count) { (*count)++; }
49 
52 
54  T* get() { return rep; }
55 
57  const T* get() const { return rep; }
58 
60  void Assign(const Handle& h) {
61  decrementRef();
62  rep = new T(*(h.rep));
63  count = new int(1);
64  }
65 
67  void makeOwnCopy() {
68  T *tmp;
69  if (*count > 1) {
70  tmp = rep;
71  (*count)--;
72  rep = new T(*tmp);
73  count = new int(1);
74  }
75  }
76 
78  Handle& operator = (const Handle& h) {
79  if (this != &h) {
80  decrementRef();
81  rep = h.rep;
82  count = h.count;
83  (*count)++;
84  }
85  return *this;
86  }
87 
89  T* operator -> () const { return rep; }
90 
92  const T& operator * () const { return *rep; }
93 
95  T& operator * () { return *rep; }
96 
97  private:
98 
100  T *rep;
101 
103  int *count;
104 
106  void decrementRef() {
107  (*count)--;
108  if (*count == 0) {
109  delete rep;
110  delete count;
111  }
112  }
113 
114  }; // class Handle
115 
117  template <typename T>
118  bool operator==(const Handle<T>& h1, const Handle<T>& h2) {
119  return h1.get() == h2.get();
120  }
121 
122 } // namespace Sacado
123 
124 #endif // SACADO_HANDLE_HPP
T * operator->() const
Dereference.
A generic handle class.
void decrementRef()
Decrement reference.
void makeOwnCopy()
Make handle have its own copy of rep.
void Assign(const Handle &h)
Assign to handle h as its own copy.
int * count
Reference count.
#define T
Definition: Sacado_rad.hpp:573
Handle(const Handle &h)
Copy handle.
Handle & operator=(const Handle &h)
Assignment operator.
~Handle()
Destroy handle.
Handle(T *p)
Create new handle from pointer p.
const T & operator*() const
Dereference.
bool operator==(const Handle< T > &h1, const Handle< T > &h2)
Compare two handles.
T * rep
Pointer to data.
T * get()
Return pointer to underlying data.