Sacado Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
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
35namespace 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) {
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
79 if (this != &h) {
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
101
103 int *count;
104
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
#define T
Definition: Sacado_rad.hpp:573
A generic handle class.
T * rep
Pointer to data.
const T & operator*() const
Dereference.
T * operator->() const
Dereference.
int * count
Reference count.
void Assign(const Handle &h)
Assign to handle h as its own copy.
Handle(T *p)
Create new handle from pointer p.
const T * get() const
Return pointer to underlying data.
Handle(const Handle &h)
Copy handle.
void makeOwnCopy()
Make handle have its own copy of rep.
T * get()
Return pointer to underlying data.
Handle & operator=(const Handle &h)
Assignment operator.
void decrementRef()
Decrement reference.
~Handle()
Destroy handle.
const char * p
bool operator==(const Handle< T > &h1, const Handle< T > &h2)
Compare two handles.