CHROMA
handle.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /*! @file
3  * @brief Class for counted reference semantics
4  *
5  * Holds and object, and deletes it when the last Handle to it
6  * is destroyed
7  *
8  * Code from * "The C++ Standard Library - A Tutorial and Reference"
9  * by Nicolai M. Josuttis, Addison-Wesley, 1999.
10  *
11  * An almost identical version is in Stroustrup, "C++ Programming Language",
12  * 3rd ed., Section 25.7. The names used there are used for this class.
13  *
14  */
15 
16 #ifndef __handle_h__
17 #define __handle_h__
18 
19 #include "chromabase.h"
20 #include <iostream>
21 
22 using namespace QDP;
23 
24 namespace Chroma
25 {
26  //! Class for counted reference semantics
27  /*!
28  * Holds and object, and deletes it when the last Handle to it
29  * is destroyed
30  */
31  template <class T>
32  class Handle
33  {
34  public:
35  //! Initialize pointer with existing pointer
36  /*! Requires that the pointer p is a return value of new */
37  Handle(T* p=0) : ptr(p), count(new int(1)) {}
38 
39  //! Copy pointer (one more owner)
40  Handle(const Handle& p) : ptr(p.ptr), count(p.count)
41  {++*count;}
42 
43  //! Destructor (delete value if this was the last owner)
44  ~Handle() {dispose();}
45 
46  //! Assignment (unshare old and share new value)
48  {
49  if (ptr != p.ptr)
50  {
51  dispose();
52  ptr = p.ptr;
53  count = p.count;
54  ++*count;
55  }
56  return *this;
57  }
58 
59  //! RGE's addition. A cast function to morph the actual type
60  template<typename Q>
62  {
63  Handle<Q> q;
64  q.ptr = dynamic_cast<Q*>(ptr);
65  if( q.ptr == 0x0 ) {
66  QDPIO::cerr << "Dynamic cast failed in Handle::cast()" <<std::endl;
67  QDPIO::cerr << "You are trying to cast to a class you cannot cast to" << std::endl;
68  QDP_abort(1);
69  }
70  delete q.count;
71 
72  q.count = count;
73  ++*count;
74  return q;
75  }
76 
77  //! The cast function requires all Handles<Q> to be friends of Handle<T>
78  template<typename Q> friend class Handle;
79 
80  //! Access the value to which the pointer refers
81  T& operator*() const {return *ptr;}
82  T* operator->() const {return ptr;}
83 
84  private:
85  void dispose()
86  {
87  if (--*count == 0)
88  {
89  delete count;
90  delete ptr;
91  }
92  }
93 
94  private:
95  T* ptr; // pointer to the value
96  int* count; // shared number of owners
97  };
98 
99 }
100 
101 
102 #endif
Primary include file for CHROMA library code.
Class for counted reference semantics.
Definition: handle.h:33
T * operator->() const
Definition: handle.h:82
Handle< Q > cast()
RGE's addition. A cast function to morph the actual type.
Definition: handle.h:61
T & operator*() const
Access the value to which the pointer refers.
Definition: handle.h:81
Handle(T *p=0)
Initialize pointer with existing pointer.
Definition: handle.h:37
int * count
Definition: handle.h:96
Handle & operator=(const Handle &p)
Assignment (unshare old and share new value)
Definition: handle.h:47
~Handle()
Destructor (delete value if this was the last owner)
Definition: handle.h:44
void dispose()
Definition: handle.h:85
Handle(const Handle &p)
Copy pointer (one more owner)
Definition: handle.h:40
Double q
Definition: mesq.cc:17
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
LinOpSysSolverMGProtoClover::Q Q
LinOpSysSolverMGProtoClover::T T
int count
Definition: octave.h:14