MySQL 8.4.0
Source Code Documentation
ref_ptr.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef MEMORY_REF_PTR_INCLUDED
25#define MEMORY_REF_PTR_INCLUDED
26
27#include <sys/stat.h>
28#include <sys/types.h>
29#include <algorithm>
30#include <iostream>
31#include <memory>
32#include <string>
33#include <tuple>
34
35namespace memory {
36/**
37 Class that holds the pointer to a variable in a static and
38 non-destructible way. The purpose is both to clearly state the ownership
39 of the memory being pointed to and to avoid unwanted pointer operations
40 (a `delete` on a pointer pointing to a stack memory block, for instance).
41
42 It's a convenience class for clearly stating the ownership of the underlying
43 pointer and is used for interface and code clarity.
44 */
45template <typename T>
46class Ref_ptr {
47 public:
48 /**
49 Default class constructor.
50 */
51 Ref_ptr() = default;
52 /**
53 Class constructor that receives the reference to be managed.
54
55 @param target The reference to be managed.
56 */
57 Ref_ptr(T &target);
58 /**
59 Copy constructor.
60
61 @param rhs The object to copy from.
62 */
63 Ref_ptr(Ref_ptr<T> const &rhs);
64 /**
65 Move constructor.
66
67 @param rhs The object to move from.
68 */
69 Ref_ptr(Ref_ptr<T> &&rhs);
70 /**
71 Default destructor.
72 */
73 virtual ~Ref_ptr() = default;
74
75 /**
76 Assignment operator to instantiate the reference to be managed.
77
78 @param rhs The reference to be managed.
79
80 @return A reference to `this` object.
81 */
82 Ref_ptr<T> &operator=(T &rhs);
83 /**
84 Copy operator.
85
86 @param rhs The object to copy from.
87
88 @return A reference to `this` object.
89 */
90 Ref_ptr<T> &operator=(Ref_ptr<T> const &rhs);
91 /**
92 Move operator.
93
94 @param rhs The object to move from.
95
96 @return A reference to `this` object.
97 */
99 /**
100 Negation operator.
101
102 @return `true` if there is no managed reference, `false` otherwise.
103 */
104 bool operator!() const;
105 /**
106 Arrow operator to access the underlying object of type `T`.
107
108 @return A pointer to the underlying object of type `T`.
109 */
110 T *operator->() const;
111 /**
112 Star operator to access the underlying object of type `T`.
113
114 @return A reference to the underlying object of type `T`.
115 */
116 T &operator*() const;
117 /**
118 Resets the managed reference and stops managing any pointer.
119
120 @return A reference to `this` object, for chaining purposes.
121 */
122 Ref_ptr<T> &reset();
123 /**
124 Equality to `nullptr` operator.
125
126 @param rhs nullptr value
127
128 @return `true` if the managed reference is not instantiated.
129 */
130 bool operator==(std::nullptr_t rhs) const;
131 /**
132 Inequality to `nullptr` operator.
133
134 @param rhs nullptr value
135
136 @return `false` if the managed reference is not instantiated.
137 */
138 bool operator!=(std::nullptr_t rhs) const;
139 /**
140 Equality operator.
141
142 @param rhs The object to compare to.
143
144 @return `true` if the managed reference is the same as one managed by the
145 `rhs` object.
146 */
147 template <typename R>
148 bool operator==(memory::Ref_ptr<R> const &rhs) const;
149 /**
150 Inequality operator.
151
152 @param rhs The object to compare to.
153
154 @return `true` if the managed reference is not the same as one managed by
155 the `rhs` object.
156 */
157 template <typename R>
158 bool operator!=(memory::Ref_ptr<R> const &rhs) const;
159
160 private:
161 /** The reference to be managed. */
162 T *m_underlying{nullptr};
163};
164} // namespace memory
165
166template <typename T>
167memory::Ref_ptr<T>::Ref_ptr(T &target) : m_underlying{&target} {}
168
169template <typename T>
171 : m_underlying{rhs.m_underlying} {}
172
173template <typename T>
175 : m_underlying{rhs.m_underlying} {
176 rhs.reset();
177}
178
179template <typename T>
181 this->m_underlying = &rhs;
182 return (*this);
183}
184
185template <typename T>
187 memory::Ref_ptr<T> const &rhs) {
188 this->m_underlying = rhs.m_underlying;
189 return (*this);
190}
191
192template <typename T>
194 this->m_underlying = rhs.m_underlying;
195 rhs.reset();
196 return (*this);
197}
198
199template <typename T>
201 return this->m_underlying == nullptr;
202}
203
204template <typename T>
206 return *this->m_underlying;
207}
208
209template <typename T>
211 return this->m_underlying;
212}
213
214template <typename T>
215bool memory::Ref_ptr<T>::operator==(std::nullptr_t) const {
216 return this->m_underlying == nullptr;
217}
218
219template <typename T>
220bool memory::Ref_ptr<T>::operator!=(std::nullptr_t) const {
221 return this->m_underlying != nullptr;
222}
223
224template <typename T>
225template <typename R>
227 return this->m_underlying == rhs.m_underlying;
228}
229
230template <typename T>
231template <typename R>
233 return this->m_underlying != rhs.m_underlying;
234}
235
236template <typename T>
238 this->m_underlying = nullptr;
239 return (*this);
240}
241
242#endif // MEMORY_REF_PTR_INCLUDED
Class that holds the pointer to a variable in a static and non-destructible way.
Definition: ref_ptr.h:46
bool operator==(std::nullptr_t rhs) const
Equality to nullptr operator.
Definition: ref_ptr.h:215
bool operator!=(std::nullptr_t rhs) const
Inequality to nullptr operator.
Definition: ref_ptr.h:220
Ref_ptr< T > & reset()
Resets the managed reference and stops managing any pointer.
Definition: ref_ptr.h:237
virtual ~Ref_ptr()=default
Default destructor.
T * m_underlying
The reference to be managed.
Definition: ref_ptr.h:162
T & operator*() const
Star operator to access the underlying object of type T.
Definition: ref_ptr.h:205
T * operator->() const
Arrow operator to access the underlying object of type T.
Definition: ref_ptr.h:210
Ref_ptr()=default
Default class constructor.
bool operator!() const
Negation operator.
Definition: ref_ptr.h:200
Ref_ptr< T > & operator=(T &rhs)
Assignment operator to instantiate the reference to be managed.
Definition: ref_ptr.h:180
Definition: aligned_atomic.h:44