| 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 |
#ifndef PY_SMARTPTRPY_H |
| 27 |
#define PY_SMARTPTRPY_H |
| 28 |
|
| 29 |
#include <FCGlobal.h> |
| 30 |
|
| 31 |
// forward declarations |
| 32 |
using PyObject = struct _object; |
| 33 |
|
| 34 |
namespace Py |
| 35 |
{ |
| 36 |
class Object; |
| 37 |
|
| 38 |
/** |
| 39 |
* \brief This is a stripped-down version of Py::Object. |
| 40 |
* The purpose of this class is to avoid to include any other header file |
| 41 |
* and therefore the PyObject is forward declared and the implementation |
| 42 |
* is separated from the class declaration. |
| 43 |
* |
| 44 |
* \author Werner Mayer |
| 45 |
*/ |
| 46 |
class BaseExport SmartPtr // NOLINT |
| 47 |
{ |
| 48 |
private: |
| 49 |
PyObject* p; |
| 50 |
|
| 51 |
protected: |
| 52 |
void set(PyObject* pyob, bool owned = false); |
| 53 |
void release(); |
| 54 |
|
| 55 |
public: |
| 56 |
SmartPtr(); |
| 57 |
|
| 58 |
// Constructor acquires new ownership of pointer unless explicitly told not to. |
| 59 |
explicit SmartPtr(PyObject* pyob, bool owned = false); |
| 60 |
|
| 61 |
// Copy constructor acquires new ownership of pointer |
| 62 |
SmartPtr(const SmartPtr& ob); |
| 63 |
|
| 64 |
// Assignment acquires new ownership of pointer |
| 65 |
SmartPtr& operator=(const SmartPtr& rhs); |
| 66 |
SmartPtr& operator=(const Object& rhs); |
| 67 |
|
| 68 |
SmartPtr& operator=(PyObject* rhsp); |
| 69 |
|
| 70 |
// Destructor |
| 71 |
virtual ~SmartPtr(); |
| 72 |
|
| 73 |
// Loaning the pointer to others, retain ownership |
| 74 |
PyObject* operator*() const; |
| 75 |
|
| 76 |
// Would like to call this pointer() but messes up STL in SeqBase<T> |
| 77 |
PyObject* ptr() const; |
| 78 |
// |
| 79 |
// Queries |
| 80 |
// |
| 81 |
|
| 82 |
bool is(PyObject* pother) const; |
| 83 |
bool is(const SmartPtr& other) const; |
| 84 |
|
| 85 |
bool isNull() const; |
| 86 |
}; |
| 87 |
|
| 88 |
BaseExport PyObject* new_reference_to(const SmartPtr&); |
| 89 |
|
| 90 |
} // namespace Py |
| 91 |
|
| 92 |
#endif // PY_SMARTPTRPY_H |