| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2002 Jürgen Riegel <[email protected]> * |
| 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 |
/** \file PyExport.h |
| 27 |
* \brief the python object export base class |
| 28 |
* \author Juergen Riegel |
| 29 |
* \version 0.1 |
| 30 |
* \date 5.2001 |
| 31 |
*/ |
| 32 |
|
| 33 |
#ifndef BASE_PYEXPORT_H |
| 34 |
#define BASE_PYEXPORT_H |
| 35 |
|
| 36 |
// (re-)defined in pyconfig.h |
| 37 |
#if defined(_POSIX_C_SOURCE) |
| 38 |
# undef _POSIX_C_SOURCE |
| 39 |
#endif |
| 40 |
#if defined(_XOPEN_SOURCE) |
| 41 |
# undef _XOPEN_SOURCE |
| 42 |
#endif |
| 43 |
|
| 44 |
#include <Python.h> |
| 45 |
|
| 46 |
#include <FCConfig.h> |
| 47 |
|
| 48 |
#ifdef FC_OS_MACOSX |
| 49 |
# undef toupper |
| 50 |
# undef tolower |
| 51 |
# undef isupper |
| 52 |
# undef islower |
| 53 |
# undef isspace |
| 54 |
# undef isalpha |
| 55 |
# undef isalnum |
| 56 |
#endif |
| 57 |
|
| 58 |
namespace Base |
| 59 |
{ |
| 60 |
class PyObjectBase; |
| 61 |
|
| 62 |
/** The PyHandler class |
| 63 |
* This class is the base class of all FreeCAD classes |
| 64 |
* which exports into the python space. This class handles the |
| 65 |
* creation referencing of the python export object. |
| 66 |
* |
| 67 |
* @remark GetPyObject() returns the associated Python object to any C++ subclasses. As we cannot |
| 68 |
* determine for sure if we can increment the returned Python object from outside of GetPyObject() |
| 69 |
* we have specified that GetPyObject() does already the increment of the reference counter |
| 70 |
* if needed. |
| 71 |
* |
| 72 |
* E.g. if GetPyObject() always returns a new Python object then no increment is necessary, |
| 73 |
* because at construction time the reference counter is already set to 1. If the Python |
| 74 |
* interpreter stores this object pointer into a local variable and destroys this variable |
| 75 |
* then the reference counter gets decremented (to 0) and the object gets destroyed automatically. |
| 76 |
* In case we didn't make this specification and increment the Python object from outside once |
| 77 |
* again then the reference counter would be set to 2 and there would be no chance to destroy the |
| 78 |
* object again. |
| 79 |
* |
| 80 |
* The other case is that we have a member variable in our C++ class that holds the Python object |
| 81 |
* then we either can create this Python in the constructor or create it the first time when |
| 82 |
* GetPyObject() gets called. In the destructor then we must decrement the Python object to avoid a |
| 83 |
* memory leak while GetPyObject() then increments the Python object every time it gets called. |
| 84 |
* |
| 85 |
* @remark One big consequence of this specification is that the programmer must know whether the |
| 86 |
* Python interpreter gets the Python object or not. If the interpreter gets the object then it |
| 87 |
* decrements the counter later on when the internal variable is freed. In case the interpreter |
| 88 |
* doesn't get this object then the programmer must do the decrement on their own. |
| 89 |
* |
| 90 |
* @note To not to undermine this specification the programmer must make sure to get the Python |
| 91 |
* object always via GetPyObject(). |
| 92 |
* |
| 93 |
* @see PyHandle |
| 94 |
* @ |
| 95 |
*/ |
| 96 |
// class BaseExport PyHandler |
| 97 |
//{ |
| 98 |
// public: |
| 99 |
// void IncRef(void); |
| 100 |
// void DecRef(void); |
| 101 |
// virtual ~PyHandler(); |
| 102 |
// virtual PyObjectBase *GetPyObject(void)=0; |
| 103 |
// |
| 104 |
// }; |
| 105 |
|
| 106 |
|
| 107 |
/** Python Object handle class |
| 108 |
* Using pointers on classes derived from PyObjectBase would |
| 109 |
* be potentionaly dangerous because you would have to take |
| 110 |
* care of the reference counting of python by your self. Hence |
| 111 |
* this class was designed. It takes care of references and |
| 112 |
* as long as a object of this class exists the handled class get |
| 113 |
* not destructed. That means a PyObjectBase derived object you can |
| 114 |
* only destruct by destructing all FCPyHandle and all python |
| 115 |
* references on it! |
| 116 |
* @see PyObjectBase |
| 117 |
*/ |
| 118 |
template<class HandledType> |
| 119 |
class PyHandle // NOLINT |
| 120 |
{ |
| 121 |
public: |
| 122 |
//************************************************************************** |
| 123 |
// construction destruction |
| 124 |
|
| 125 |
/** pointer and default constructor |
| 126 |
* the good way would be not using pointer |
| 127 |
* instead using a overwritten new operator in the |
| 128 |
* HandledType class! But is not easy to enforce! |
| 129 |
*/ |
| 130 |
PyHandle(HandledType* ToHandle = 0L) |
| 131 |
: _pHandles(ToHandle) |
| 132 |
{ |
| 133 |
if (_pHandles) { |
| 134 |
_pHandles->IncRef(); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/// Copy constructor |
| 139 |
PyHandle(const PyHandle<HandledType>& ToHandle) |
| 140 |
: _pHandles(ToHandle._pHandles) |
| 141 |
{ |
| 142 |
if (_pHandles) { |
| 143 |
_pHandles->IncRef(); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** destructor |
| 148 |
* Release the reference count which cause, |
| 149 |
* if was the last one, the referenced object to |
| 150 |
* destruct! |
| 151 |
*/ |
| 152 |
~PyHandle() |
| 153 |
{ |
| 154 |
if (_pHandles) { |
| 155 |
_pHandles->DecRef(); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
//************************************************************************** |
| 160 |
// operator implementation |
| 161 |
|
| 162 |
// assign operator from a pointer |
| 163 |
PyHandle<HandledType>& operator=(/*const*/ HandledType* other) |
| 164 |
{ |
| 165 |
if (_pHandles) { |
| 166 |
_pHandles->DecRef(); |
| 167 |
} |
| 168 |
// FIXME: Should be without "->_pHandles", shouldn't it? (Werner) |
| 169 |
_pHandles = other; //_pHandles = other->_pHandles; |
| 170 |
if (_pHandles) { |
| 171 |
_pHandles->IncRef(); |
| 172 |
} |
| 173 |
return *this; |
| 174 |
} |
| 175 |
|
| 176 |
// assign operator from a handle |
| 177 |
PyHandle<HandledType>& operator=(const PyHandle<HandledType>& other) |
| 178 |
{ |
| 179 |
if (_pHandles) { |
| 180 |
_pHandles->DecRef(); |
| 181 |
} |
| 182 |
_pHandles = other._pHandles; |
| 183 |
if (_pHandles) { |
| 184 |
_pHandles->IncRef(); |
| 185 |
} |
| 186 |
return *this; |
| 187 |
} |
| 188 |
|
| 189 |
/// dereference operators |
| 190 |
HandledType& operator*() |
| 191 |
{ |
| 192 |
return *_pHandles; |
| 193 |
} |
| 194 |
|
| 195 |
/// dereference operators |
| 196 |
HandledType* operator->() |
| 197 |
{ |
| 198 |
return _pHandles; |
| 199 |
} |
| 200 |
|
| 201 |
/// dereference operators |
| 202 |
const HandledType& operator*() const |
| 203 |
{ |
| 204 |
return _pHandles; |
| 205 |
} |
| 206 |
|
| 207 |
/// dereference operators |
| 208 |
const HandledType* operator->() const |
| 209 |
{ |
| 210 |
return _pHandles; |
| 211 |
} |
| 212 |
|
| 213 |
/** lower operator |
| 214 |
* needed for sorting in maps and sets |
| 215 |
*/ |
| 216 |
bool operator<(const PyHandle<HandledType>& other) const |
| 217 |
{ |
| 218 |
// return _pHandles<&other; |
| 219 |
// FIXME: Shouldn't we compare both pointers?. (Werner) |
| 220 |
return _pHandles < other._pHandles; |
| 221 |
} |
| 222 |
|
| 223 |
/// equal operator |
| 224 |
bool operator==(const PyHandle<HandledType>& other) const |
| 225 |
{ |
| 226 |
// return _pHandles==&other; |
| 227 |
// FIXME: Shouldn't we compare both pointers?. (Werner) |
| 228 |
return _pHandles == other._pHandles; |
| 229 |
} |
| 230 |
|
| 231 |
/// returns the type as PyObject |
| 232 |
PyObject* getPyObject() const |
| 233 |
{ |
| 234 |
// return (PyObject*) _pHandles; |
| 235 |
// FIXME: Shouldn't we return the pointer's object?. (Werner) |
| 236 |
return const_cast<HandledType*>(_pHandles)->getPyObject(); // NOLINT |
| 237 |
} |
| 238 |
//************************************************************************** |
| 239 |
// checking on the state |
| 240 |
|
| 241 |
/// Test if it handles something |
| 242 |
bool IsValid() const |
| 243 |
{ |
| 244 |
return _pHandles != 0; |
| 245 |
} |
| 246 |
|
| 247 |
/// Test if it not handles something |
| 248 |
bool IsNull() const |
| 249 |
{ |
| 250 |
return _pHandles == 0; |
| 251 |
} |
| 252 |
|
| 253 |
private: |
| 254 |
/// the pointer on the handled object |
| 255 |
HandledType* _pHandles; |
| 256 |
}; |
| 257 |
|
| 258 |
|
| 259 |
} // namespace Base |
| 260 |
|
| 261 |
#endif // BASE_PYEXPORT_H |