Qore Programming Language 2.0.0
Loading...
Searching...
No Matches
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 - 2024 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
51template<typename T = class AbstractQoreNode>
53public:
55 DLLLOCAL ReferenceHolder(ExceptionSink* xsink_) : xsink(xsink_) {}
56
58 DLLLOCAL ReferenceHolder(T* p_, ExceptionSink* xsink_) : p(p_), xsink(xsink_) {}
59
61 DLLLOCAL ~ReferenceHolder() { if (p) p->deref(xsink);}
62
64 DLLLOCAL T* operator->() { return p; }
65
67 DLLLOCAL T* operator*() { return p; }
68
70 DLLLOCAL const T* operator->() const { return p; }
71
73 DLLLOCAL const T* operator*() const { return p; }
74
76 DLLLOCAL void operator=(T *nv) {
77 if (p)
78 p->deref(xsink);
79 p = nv;
80 }
81
83 DLLLOCAL T* release() {
84 T* rv = p;
85 p = nullptr;
86 return rv;
87 }
88
90
92 DLLLOCAL T* swap(T* val) {
93 T* rv = p;
94 p = val;
95 return rv;
96 }
97
99 DLLLOCAL operator bool() const { return p != nullptr; }
100
102 DLLLOCAL T** getPtrPtr() { return &p; }
103
105 DLLLOCAL T*& getRef() { return p; }
106
107protected:
108 T* p = nullptr;
109 ExceptionSink* xsink;
110
111private:
112 ReferenceHolder(const ReferenceHolder&) = delete;
113 ReferenceHolder& operator=(const ReferenceHolder&) = delete;
114 void* operator new(size_t) = delete;
115};
116
118
126template<typename T = class SimpleQoreNode>
128public:
129 DLLLOCAL SimpleRefHolder() {}
130 DLLLOCAL SimpleRefHolder(T* p_) : p(p_) {}
131
133
135 DLLLOCAL SimpleRefHolder(SimpleRefHolder&& old) : p(std::move(old.p)) {
136 old.p = nullptr;
137 }
138
139 DLLLOCAL ~SimpleRefHolder() { if (p) p->deref(); }
140
141 DLLLOCAL void discard() {
142 if (p) {
143 p->deref();
144 p = nullptr;
145 }
146 }
147
148 DLLLOCAL T* operator->() { return p; }
149 DLLLOCAL T* operator*() { return p; }
150 DLLLOCAL const T* operator->() const { return p; }
151 DLLLOCAL const T* operator*() const { return p; }
152
153 DLLLOCAL void operator=(T* nv) {
154 if (p) {
155 p->deref();
156 }
157 p = nv;
158 }
159
160 DLLLOCAL T* release() {
161 T* rv = p;
162 p = nullptr;
163 return rv;
164 }
165
167
169 DLLLOCAL T* swap(T* val) {
170 T* rv = p;
171 p = val;
172 return rv;
173 }
174
175 DLLLOCAL operator bool() const {
176 return p != nullptr;
177 }
178
179protected:
180 T* p = nullptr;
181
182private:
183 SimpleRefHolder(const SimpleRefHolder&) = delete;
184 SimpleRefHolder& operator=(const SimpleRefHolder&) = delete;
185 void* operator new(size_t) = delete;
186};
187
188#endif
189// EOF
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition ExceptionSink.h:50
a templated class to manage a reference count of an object that can throw a Qore-language exception w...
Definition ReferenceHolder.h:52
DLLLOCAL ReferenceHolder(T *p_, ExceptionSink *xsink_)
populates with object with data and the ExceptionSink pointer
Definition ReferenceHolder.h:58
DLLLOCAL T * operator->()
returns the pointer being managed
Definition ReferenceHolder.h:64
DLLLOCAL ~ReferenceHolder()
calls deref(ExceptionSink *) on the pointer being managed if not 0
Definition ReferenceHolder.h:61
DLLLOCAL const T * operator*() const
returns the pointer being managed
Definition ReferenceHolder.h:73
DLLLOCAL const T * operator->() const
returns the pointer being managed
Definition ReferenceHolder.h:70
DLLLOCAL T ** getPtrPtr()
returns a pointer to the pointer being managed
Definition ReferenceHolder.h:102
DLLLOCAL ReferenceHolder(ExceptionSink *xsink_)
creates an empty ReferenceHolder
Definition ReferenceHolder.h:55
DLLLOCAL T *& getRef()
returns a reference to the ptr being managed
Definition ReferenceHolder.h:105
DLLLOCAL T * operator*()
returns the pointer being managed
Definition ReferenceHolder.h:67
DLLLOCAL T * release()
releases the pointer to the caller
Definition ReferenceHolder.h:83
DLLLOCAL T * swap(T *val)
swaps the current value for a new one
Definition ReferenceHolder.h:92
DLLLOCAL void operator=(T *nv)
assigns a new pointer to the holder, dereferences the current pointer if any
Definition ReferenceHolder.h:76
manages a reference count of a pointer to a class that takes a simple "deref()" call with no argument...
Definition ReferenceHolder.h:127
DLLLOCAL T * swap(T *val)
swaps the current value for a new one
Definition ReferenceHolder.h:169
DLLLOCAL SimpleRefHolder(SimpleRefHolder &&old)
move constructor
Definition ReferenceHolder.h:135