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 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef SACADO_HANDLE_HPP
11 #define SACADO_HANDLE_HPP
12 
13 namespace Sacado {
14 
18  template <typename T>
19  class Handle {
20  public:
21 
23  Handle(T* p) : rep(p), count(new int(1)) {}
24 
26  Handle(const Handle& h) : rep(h.rep), count(h.count) { (*count)++; }
27 
30 
32  T* get() { return rep; }
33 
35  const T* get() const { return rep; }
36 
38  void Assign(const Handle& h) {
39  decrementRef();
40  rep = new T(*(h.rep));
41  count = new int(1);
42  }
43 
45  void makeOwnCopy() {
46  T *tmp;
47  if (*count > 1) {
48  tmp = rep;
49  (*count)--;
50  rep = new T(*tmp);
51  count = new int(1);
52  }
53  }
54 
56  Handle& operator = (const Handle& h) {
57  if (this != &h) {
58  decrementRef();
59  rep = h.rep;
60  count = h.count;
61  (*count)++;
62  }
63  return *this;
64  }
65 
67  T* operator -> () const { return rep; }
68 
70  const T& operator * () const { return *rep; }
71 
73  T& operator * () { return *rep; }
74 
75  private:
76 
78  T *rep;
79 
81  int *count;
82 
84  void decrementRef() {
85  (*count)--;
86  if (*count == 0) {
87  delete rep;
88  delete count;
89  }
90  }
91 
92  }; // class Handle
93 
95  template <typename T>
96  bool operator==(const Handle<T>& h1, const Handle<T>& h2) {
97  return h1.get() == h2.get();
98  }
99 
100 } // namespace Sacado
101 
102 #endif // SACADO_HANDLE_HPP
const char * p
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:553
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.