Qore Programming Language  0.8.11
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ReferenceHolder.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  ReferenceHolder.h
4 
5  Smart pointer like class that dereferences
6  obtained pointer to a QoreReferenceCounter in its destructor.
7 
8  Qore Programming Language
9 
10  Copyright (C) 2006 - 2013 Qore Technologies
11 
12  This library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU Lesser General Public
14  License as published by the Free Software Foundation; either
15  version 2.1 of the License, or (at your option) any later version.
16 
17  This library is distributed in the hope that it will be useful,
18  but 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 USA
25 */
26 
27 #ifndef QORE_REFERENCE_HOLDER_H_
28 #define QORE_REFERENCE_HOLDER_H_
29 
30 #include <stdlib.h>
31 
33 
42 template<typename T = class AbstractQoreNode>
44 private:
45  DLLLOCAL ReferenceHolder(const ReferenceHolder&); // not implemented
46  DLLLOCAL ReferenceHolder& operator=(const ReferenceHolder&); // not implemented
47  DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
48 
49  T* p;
50  ExceptionSink* xsink;
51 
52 public:
54  DLLLOCAL ReferenceHolder(ExceptionSink* xsink_) : p(0), xsink(xsink_) {}
55 
57  DLLLOCAL ReferenceHolder(T* p_, ExceptionSink* xsink_) : p(p_), xsink(xsink_) {}
58 
60  DLLLOCAL ~ReferenceHolder() { if (p) p->deref(xsink);}
61 
63  DLLLOCAL T* operator->() { return p; }
64 
66  DLLLOCAL T* operator*() { return p; }
67 
69  DLLLOCAL void operator=(T *nv) {
70  if (p)
71  p->deref(xsink);
72  p = nv;
73  }
74 
76  DLLLOCAL T* release() {
77  T *rv = p;
78  p = 0;
79  return rv;
80  }
81 
83  DLLLOCAL operator bool() const { return p != 0; }
84 
86  DLLLOCAL T** getPtrPtr() { return &p; }
87 
89  DLLLOCAL T*& getRef() { return p; }
90 };
91 
93 
102 template<typename T>
104 private:
105  DLLLOCAL SimpleRefHolder(const SimpleRefHolder&); // not implemented
106  DLLLOCAL SimpleRefHolder& operator=(const SimpleRefHolder&); // not implemented
107  DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
108 
109  T* p;
110 
111 public:
112  DLLLOCAL SimpleRefHolder() : p(0) {}
113  DLLLOCAL SimpleRefHolder(T* p_) : p(p_) {}
114  DLLLOCAL ~SimpleRefHolder() { if (p) p->deref(); }
115 
116  DLLLOCAL T* operator->() { return p; }
117  DLLLOCAL T* operator*() { return p; }
118  DLLLOCAL void operator=(T *nv) {
119  if (p)
120  p->deref();
121  p = nv;
122  }
123  DLLLOCAL T *release() {
124  T *rv = p;
125  p = 0;
126  return rv;
127  }
128  DLLLOCAL operator bool() const { return p != 0; }
129 };
130 
131 #endif
132 // EOF
DLLLOCAL T ** getPtrPtr()
returns a pointer to the pointer being managed
Definition: ReferenceHolder.h:86
DLLLOCAL ReferenceHolder(T *p_, ExceptionSink *xsink_)
populates with object with data and the ExceptionSink pointer
Definition: ReferenceHolder.h:57
DLLLOCAL T * release()
releases the pointer to the caller
Definition: ReferenceHolder.h:76
DLLLOCAL T * operator*()
returns the pointer being managed
Definition: ReferenceHolder.h:66
DLLLOCAL ~ReferenceHolder()
calls deref(ExceptionSink *) on the pointer being managed if not 0
Definition: ReferenceHolder.h:60
DLLLOCAL T *& getRef()
returns a reference to the ptr being managed
Definition: ReferenceHolder.h:89
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:35
manages a reference count of a pointer to a class that takes a simple "deref()" call with no argument...
Definition: ReferenceHolder.h:103
DLLLOCAL T * operator->()
returns the pointer being managed
Definition: ReferenceHolder.h:63
DLLLOCAL ReferenceHolder(ExceptionSink *xsink_)
creates an empty ReferenceHolder
Definition: ReferenceHolder.h:54
DLLLOCAL void operator=(T *nv)
assigns a new pointer to the holder, dereferences the current pointer if any
Definition: ReferenceHolder.h:69
a templated class to manage a reference count of an object that can throw a Qore-language exception w...
Definition: ReferenceHolder.h:43