57d1db96e6cd6b50789e82ebae52825dc635a84c
1 /* Reference-counted smart pointer class
3 Copyright (C) 2016-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 /* An instance of this class either holds a reference to a
29 reference-counted object or is "NULL". Reference counting is
30 handled externally by a policy class. If the object holds a
31 reference, then when the object is destroyed, the reference is
34 Normally an instance is constructed using a pointer. This sort of
35 initialization lets this class manage the lifetime of that
38 Assignment and copy construction will make a new reference as
39 appropriate. Assignment from a plain pointer is disallowed to
40 avoid confusion about whether this acquires a new reference;
41 instead use the "reset" method -- which, like the pointer
42 constructor, transfers ownership.
44 The policy class must provide two static methods:
48 template<typename T
, typename Policy
>
53 /* Create a new NULL instance. */
59 /* Create a new NULL instance. Note that this is not explicit. */
60 ref_ptr (const std::nullptr_t
)
65 /* Create a new instance. OBJ is a reference, management of which
66 is now transferred to this class. */
67 explicit ref_ptr (T
*obj
)
72 /* Copy another instance. */
73 ref_ptr (const ref_ptr
&other
)
77 Policy::incref (m_obj
);
80 /* Transfer ownership from OTHER. */
81 ref_ptr (ref_ptr
&&other
)
87 /* Destroy this instance. */
91 Policy::decref (m_obj
);
94 /* Copy another instance. */
95 ref_ptr
&operator= (const ref_ptr
&other
)
97 /* Do nothing on self-assignment. */
102 Policy::incref (m_obj
);
107 /* Transfer ownership from OTHER. */
108 ref_ptr
&operator= (ref_ptr
&&other
)
110 /* Do nothing on self-assignment. */
119 /* Change this instance's referent. OBJ is a reference, management
120 of which is now transferred to this class. */
124 Policy::decref (m_obj
);
128 /* Return this instance's referent without changing the state of
135 /* Return this instance's referent, and stop managing this
136 reference. The caller is now responsible for the ownership of
146 /* Let users refer to members of the underlying pointer. */
147 T
*operator-> () const
157 template<typename T
, typename Policy
>
158 inline bool operator== (const ref_ptr
<T
, Policy
> &lhs
,
159 const ref_ptr
<T
, Policy
> &rhs
)
161 return lhs
.get () == rhs
.get ();
164 template<typename T
, typename Policy
>
165 inline bool operator== (const ref_ptr
<T
, Policy
> &lhs
, const T
*rhs
)
167 return lhs
.get () == rhs
;
170 template<typename T
, typename Policy
>
171 inline bool operator== (const ref_ptr
<T
, Policy
> &lhs
, const std::nullptr_t
)
173 return lhs
.get () == nullptr;
176 template<typename T
, typename Policy
>
177 inline bool operator== (const T
*lhs
, const ref_ptr
<T
, Policy
> &rhs
)
179 return lhs
== rhs
.get ();
182 template<typename T
, typename Policy
>
183 inline bool operator== (const std::nullptr_t
, const ref_ptr
<T
, Policy
> &rhs
)
185 return nullptr == rhs
.get ();
188 template<typename T
, typename Policy
>
189 inline bool operator!= (const ref_ptr
<T
, Policy
> &lhs
,
190 const ref_ptr
<T
, Policy
> &rhs
)
192 return lhs
.get () != rhs
.get ();
195 template<typename T
, typename Policy
>
196 inline bool operator!= (const ref_ptr
<T
, Policy
> &lhs
, const T
*rhs
)
198 return lhs
.get () != rhs
;
201 template<typename T
, typename Policy
>
202 inline bool operator!= (const ref_ptr
<T
, Policy
> &lhs
, const std::nullptr_t
)
204 return lhs
.get () != nullptr;
207 template<typename T
, typename Policy
>
208 inline bool operator!= (const T
*lhs
, const ref_ptr
<T
, Policy
> &rhs
)
210 return lhs
!= rhs
.get ();
213 template<typename T
, typename Policy
>
214 inline bool operator!= (const std::nullptr_t
, const ref_ptr
<T
, Policy
> &rhs
)
216 return nullptr != rhs
.get ();
221 #endif /* GDB_REF_PTR_H */
This page took 0.052136 seconds and 4 git commands to generate.