Qore Programming Language  0.9.3
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 - 2015 Qore Technologies
11 
12  Permission is hereby granted, free of charge, to any person obtaining a
13  copy of this software and associated documentation files (the "Software"),
14  to deal in the Software without restriction, including without limitation
15  the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  and/or sell copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following conditions:
18 
19  The above copyright notice and this permission notice shall be included in
20  all copies or substantial portions of the Software.
21 
22  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  DEALINGS IN THE SOFTWARE.
29 
30  Note that the Qore library is released under a choice of three open-source
31  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
32  information.
33 */
34 
35 #ifndef QORE_REFERENCE_HOLDER_H_
36 #define QORE_REFERENCE_HOLDER_H_
37 
38 #include <cstdlib>
39 #include <utility>
40 
42 
51 template<typename T = class AbstractQoreNode>
53 private:
54  DLLLOCAL ReferenceHolder(const ReferenceHolder&); // not implemented
55  DLLLOCAL ReferenceHolder& operator=(const ReferenceHolder&); // not implemented
56  DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
57 
58  T* p;
59  ExceptionSink* xsink;
60 
61 public:
63  DLLLOCAL ReferenceHolder(ExceptionSink* xsink_) : p(0), xsink(xsink_) {}
64 
66  DLLLOCAL ReferenceHolder(T* p_, ExceptionSink* xsink_) : p(p_), xsink(xsink_) {}
67 
69  DLLLOCAL ~ReferenceHolder() { if (p) p->deref(xsink);}
70 
72  DLLLOCAL T* operator->() { return p; }
73 
75  DLLLOCAL T* operator*() { return p; }
76 
78  DLLLOCAL const T* operator->() const { return p; }
79 
81  DLLLOCAL const T* operator*() const { return p; }
82 
84  DLLLOCAL void operator=(T *nv) {
85  if (p)
86  p->deref(xsink);
87  p = nv;
88  }
89 
91  DLLLOCAL T* release() {
92  T *rv = p;
93  p = 0;
94  return rv;
95  }
96 
98  DLLLOCAL operator bool() const { return p != 0; }
99 
101  DLLLOCAL T** getPtrPtr() { return &p; }
102 
104  DLLLOCAL T*& getRef() { return p; }
105 };
106 
108 
117 template<typename T>
119 private:
120  DLLLOCAL SimpleRefHolder(const SimpleRefHolder&); // not implemented
121  DLLLOCAL SimpleRefHolder& operator=(const SimpleRefHolder&); // not implemented
122  DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
123 
124  T* p;
125 
126 public:
127  DLLLOCAL SimpleRefHolder() : p(0) {}
128  DLLLOCAL SimpleRefHolder(T* p_) : p(p_) {}
129 
131 
133  DLLLOCAL SimpleRefHolder(SimpleRefHolder&& old) : p(std::move(old.p)) {
134  old.p = nullptr;
135  }
136 
137  DLLLOCAL ~SimpleRefHolder() { if (p) p->deref(); }
138 
139  DLLLOCAL T* operator->() { return p; }
140  DLLLOCAL T* operator*() { return p; }
141  DLLLOCAL const T* operator->() const { return p; }
142  DLLLOCAL const T* operator*() const { return p; }
143  DLLLOCAL void operator=(T *nv) {
144  if (p)
145  p->deref();
146  p = nv;
147  }
148  DLLLOCAL T *release() {
149  T *rv = p;
150  p = 0;
151  return rv;
152  }
153  DLLLOCAL operator bool() const { return p != 0; }
154 };
155 
156 #endif
157 // EOF
DLLLOCAL T ** getPtrPtr()
returns a pointer to the pointer being managed
Definition: ReferenceHolder.h:101
DLLLOCAL T * release()
releases the pointer to the caller
Definition: ReferenceHolder.h:91
DLLLOCAL T * operator*()
returns the pointer being managed
Definition: ReferenceHolder.h:75
DLLLOCAL ~ReferenceHolder()
calls deref(ExceptionSink *) on the pointer being managed if not 0
Definition: ReferenceHolder.h:69
DLLLOCAL T *& getRef()
returns a reference to the ptr being managed
Definition: ReferenceHolder.h:104
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:46
manages a reference count of a pointer to a class that takes a simple "deref()" call with no argument...
Definition: ReferenceHolder.h:118
DLLLOCAL T * operator->()
returns the pointer being managed
Definition: ReferenceHolder.h:72
a templated class to manage a reference count of an object that can throw a Qore-language exception w...
Definition: ReferenceHolder.h:52