1 /* Python reference-holding class
3 Copyright (C) 2016 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/>. */
20 #ifndef GDB_PYTHON_REF_H
21 #define GDB_PYTHON_REF_H
23 /* An instance of this class either holds a reference to a PyObject,
24 or is "NULL". If it holds a reference, then when the object is
25 destroyed, the PyObject is decref'd.
27 Normally an instance is constructed using a PyObject*. This sort
28 of initialization lets this class manage the lifetime of that
31 Assignment and copy construction will make a new reference as
32 appropriate. Assignment from a plain PyObject* is disallowed to
33 avoid confusion about whether this acquires a new reference;
34 instead use the "reset" method -- which, like the PyObject*
35 constructor, transfers ownership.
41 /* Create a new NULL instance. */
47 /* Create a new instance. OBJ is a reference, management of which
48 is now transferred to this class. */
49 explicit gdbpy_ref (PyObject
*obj
)
54 /* Copy another instance. */
55 gdbpy_ref (const gdbpy_ref
&other
)
61 /* Transfer ownership from OTHER. */
62 gdbpy_ref (gdbpy_ref
&&other
)
68 /* Destroy this instance. */
74 /* Copy another instance. */
75 gdbpy_ref
&operator= (const gdbpy_ref
&other
)
77 /* Do nothing on self-assignment. */
86 /* Transfer ownership from OTHER. */
87 gdbpy_ref
&operator= (gdbpy_ref
&&other
)
89 /* Do nothing on self-assignment. */
98 /* Change this instance's referent. OBJ is a reference, management
99 of which is now transferred to this class. */
100 void reset (PyObject
*obj
)
106 /* Return this instance's referent. In Python terms this is a
108 PyObject
*get () const
113 /* Return this instance's referent, and stop managing this
114 reference. The caller is now responsible for the ownership of
118 PyObject
*result
= m_obj
;
129 inline bool operator== (const gdbpy_ref
&self
, const gdbpy_ref
&other
)
131 return self
.get () == other
.get ();
134 inline bool operator== (const gdbpy_ref
&self
, const PyObject
*other
)
136 return self
.get () == other
;
139 inline bool operator== (const PyObject
*self
, const gdbpy_ref
&other
)
141 return self
== other
.get ();
144 inline bool operator!= (const gdbpy_ref
&self
, const gdbpy_ref
&other
)
146 return self
.get () != other
.get ();
149 inline bool operator!= (const gdbpy_ref
&self
, const PyObject
*other
)
151 return self
.get () != other
;
154 inline bool operator!= (const PyObject
*self
, const gdbpy_ref
&other
)
156 return self
!= other
.get ();
159 #endif /* GDB_PYTHON_REF_H */