| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2020 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 21 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
|
| 26 |
#include "SmartPtrPy.h" |
| 27 |
#include "Interpreter.h" |
| 28 |
#include <CXX/Objects.hxx> |
| 29 |
|
| 30 |
|
| 31 |
namespace Py |
| 32 |
{ |
| 33 |
void SmartPtr::set(PyObject* pyob, bool owned) |
| 34 |
{ |
| 35 |
release(); |
| 36 |
p = pyob; |
| 37 |
if (!owned) { |
| 38 |
Py::_XINCREF(p); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
void SmartPtr::release() |
| 43 |
{ |
| 44 |
Base::PyGILStateLocker lock; |
| 45 |
Py::_XDECREF(p); |
| 46 |
p = nullptr; |
| 47 |
} |
| 48 |
|
| 49 |
SmartPtr::SmartPtr() |
| 50 |
: p(Py::_None()) |
| 51 |
{ |
| 52 |
Py::_XINCREF(p); |
| 53 |
} |
| 54 |
|
| 55 |
SmartPtr::SmartPtr(PyObject* pyob, bool owned) |
| 56 |
: p(pyob) |
| 57 |
{ |
| 58 |
if (!owned) { |
| 59 |
Py::_XINCREF(p); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
SmartPtr::SmartPtr(const SmartPtr& ob) |
| 64 |
: p(ob.p) |
| 65 |
{ |
| 66 |
Py::_XINCREF(p); |
| 67 |
} |
| 68 |
|
| 69 |
SmartPtr& SmartPtr::operator=(const SmartPtr& rhs) |
| 70 |
{ |
| 71 |
set(rhs.ptr()); |
| 72 |
return *this; |
| 73 |
} |
| 74 |
|
| 75 |
SmartPtr& SmartPtr::operator=(const Object& rhs) |
| 76 |
{ |
| 77 |
set(rhs.ptr()); |
| 78 |
return *this; |
| 79 |
} |
| 80 |
|
| 81 |
SmartPtr& SmartPtr::operator=(PyObject* rhsp) |
| 82 |
{ |
| 83 |
if (ptr() != rhsp) { |
| 84 |
set(rhsp); |
| 85 |
} |
| 86 |
|
| 87 |
return *this; |
| 88 |
} |
| 89 |
|
| 90 |
SmartPtr::~SmartPtr() |
| 91 |
{ |
| 92 |
release(); |
| 93 |
} |
| 94 |
|
| 95 |
PyObject* SmartPtr::operator*() const |
| 96 |
{ |
| 97 |
return p; |
| 98 |
} |
| 99 |
|
| 100 |
PyObject* SmartPtr::ptr() const |
| 101 |
{ |
| 102 |
return p; |
| 103 |
} |
| 104 |
|
| 105 |
bool SmartPtr::is(PyObject* pother) const |
| 106 |
{ // identity test |
| 107 |
return p == pother; |
| 108 |
} |
| 109 |
|
| 110 |
bool SmartPtr::is(const SmartPtr& other) const |
| 111 |
{ // identity test |
| 112 |
return p == other.p; |
| 113 |
} |
| 114 |
|
| 115 |
bool SmartPtr::isNull() const |
| 116 |
{ |
| 117 |
return p == nullptr; |
| 118 |
} |
| 119 |
|
| 120 |
BaseExport PyObject* new_reference_to(const SmartPtr& ptr) |
| 121 |
{ |
| 122 |
PyObject* py = ptr.ptr(); |
| 123 |
Py::_XINCREF(py); |
| 124 |
return py; |
| 125 |
} |
| 126 |
} // namespace Py |