Base: implement a lightweight smart pointer for PyObject like Py::Object to reduce includes of Python.h in header files
This commit is contained in:
@@ -254,6 +254,7 @@ SET(FreeCADBase_CPP_SRCS
|
||||
Rotation.cpp
|
||||
RotationPyImp.cpp
|
||||
Sequencer.cpp
|
||||
SmartPtrPy.cpp
|
||||
Stream.cpp
|
||||
Swap.cpp
|
||||
${SWIG_SRCS}
|
||||
@@ -312,6 +313,7 @@ SET(FreeCADBase_HPP_SRCS
|
||||
Reader.h
|
||||
Rotation.h
|
||||
Sequencer.h
|
||||
SmartPtrPy.h
|
||||
Stream.h
|
||||
Swap.h
|
||||
${SWIG_HEADERS}
|
||||
|
||||
119
src/Base/SmartPtrPy.cpp
Normal file
119
src/Base/SmartPtrPy.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
/***************************************************************************
|
||||
* Copyright (c) 2020 Werner Mayer <wmayer[at]users.sourceforge.net> *
|
||||
* *
|
||||
* This file is part of the FreeCAD CAx development system. *
|
||||
* *
|
||||
* This library is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU Library General Public *
|
||||
* License as published by the Free Software Foundation; either *
|
||||
* version 2 of the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This library is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU Library General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Library General Public *
|
||||
* License along with this library; see the file COPYING.LIB. If not, *
|
||||
* write to the Free Software Foundation, Inc., 59 Temple Place, *
|
||||
* Suite 330, Boston, MA 02111-1307, USA *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#include "PreCompiled.h"
|
||||
#ifndef _PreComp_
|
||||
#endif
|
||||
|
||||
#include "SmartPtrPy.h"
|
||||
#include <CXX/Objects.hxx>
|
||||
|
||||
|
||||
namespace Py {
|
||||
void SmartPtr::set(PyObject *pyob, bool owned)
|
||||
{
|
||||
release();
|
||||
p = pyob;
|
||||
if( !owned )
|
||||
{
|
||||
Py::_XINCREF( p );
|
||||
}
|
||||
}
|
||||
|
||||
void SmartPtr::release()
|
||||
{
|
||||
Py::_XDECREF( p );
|
||||
p = nullptr;
|
||||
}
|
||||
|
||||
SmartPtr::SmartPtr()
|
||||
: p( Py::_None() )
|
||||
{
|
||||
Py::_XINCREF( p );
|
||||
}
|
||||
|
||||
SmartPtr::SmartPtr( PyObject *pyob, bool owned)
|
||||
: p( pyob )
|
||||
{
|
||||
if( !owned )
|
||||
{
|
||||
Py::_XINCREF( p );
|
||||
}
|
||||
}
|
||||
|
||||
SmartPtr::SmartPtr( const SmartPtr &ob )
|
||||
: p( ob.p )
|
||||
{
|
||||
Py::_XINCREF( p );
|
||||
}
|
||||
|
||||
SmartPtr &SmartPtr::operator=( const Object &rhs )
|
||||
{
|
||||
set( rhs.ptr() );
|
||||
return *this;
|
||||
}
|
||||
|
||||
SmartPtr &SmartPtr::operator=( PyObject *rhsp )
|
||||
{
|
||||
if( ptr() != rhsp )
|
||||
set( rhsp );
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
SmartPtr::~SmartPtr()
|
||||
{
|
||||
release();
|
||||
}
|
||||
|
||||
PyObject *SmartPtr::operator*() const
|
||||
{
|
||||
return p;
|
||||
}
|
||||
|
||||
PyObject *SmartPtr::ptr() const
|
||||
{
|
||||
return p;
|
||||
}
|
||||
|
||||
bool SmartPtr::is( PyObject *pother ) const
|
||||
{ // identity test
|
||||
return p == pother;
|
||||
}
|
||||
|
||||
bool SmartPtr::is( const SmartPtr &other ) const
|
||||
{ // identity test
|
||||
return p == other.p;
|
||||
}
|
||||
|
||||
bool SmartPtr::isNull() const
|
||||
{
|
||||
return p == nullptr;
|
||||
}
|
||||
|
||||
BaseExport PyObject* new_reference_to(const SmartPtr& g) {
|
||||
PyObject* p = g.ptr();
|
||||
Py::_XINCREF(p);
|
||||
return p;
|
||||
}
|
||||
}
|
||||
86
src/Base/SmartPtrPy.h
Normal file
86
src/Base/SmartPtrPy.h
Normal file
@@ -0,0 +1,86 @@
|
||||
/***************************************************************************
|
||||
* Copyright (c) 2020 Werner Mayer <wmayer[at]users.sourceforge.net> *
|
||||
* *
|
||||
* This file is part of the FreeCAD CAx development system. *
|
||||
* *
|
||||
* This library is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU Library General Public *
|
||||
* License as published by the Free Software Foundation; either *
|
||||
* version 2 of the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This library is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU Library General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Library General Public *
|
||||
* License along with this library; see the file COPYING.LIB. If not, *
|
||||
* write to the Free Software Foundation, Inc., 59 Temple Place, *
|
||||
* Suite 330, Boston, MA 02111-1307, USA *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef PY_SMARTPTRPY_H
|
||||
#define PY_SMARTPTRPY_H
|
||||
|
||||
// forward declarations
|
||||
typedef struct _object PyObject;
|
||||
|
||||
namespace Py {
|
||||
class Object;
|
||||
|
||||
/**
|
||||
* \brief This is a stripped-down version of Py::Object.
|
||||
* The purpose of this class is to avoid to include any other header file
|
||||
* and therefore the PyObject is forward declared and the implementation
|
||||
* is separated from the class declaration.
|
||||
*
|
||||
* \author Werner Mayer
|
||||
*/
|
||||
class BaseExport SmartPtr
|
||||
{
|
||||
private:
|
||||
PyObject *p;
|
||||
|
||||
protected:
|
||||
void set(PyObject *pyob, bool owned = false);
|
||||
void release();
|
||||
|
||||
public:
|
||||
SmartPtr();
|
||||
|
||||
// Constructor acquires new ownership of pointer unless explicitly told not to.
|
||||
explicit SmartPtr(PyObject *pyob, bool owned = false);
|
||||
|
||||
// Copy constructor acquires new ownership of pointer
|
||||
SmartPtr(const SmartPtr &ob);
|
||||
|
||||
// Assignment acquires new ownership of pointer
|
||||
SmartPtr &operator=( const Object &SmartPtr );
|
||||
|
||||
SmartPtr &operator=(PyObject *rhsp);
|
||||
|
||||
// Destructor
|
||||
virtual ~SmartPtr();
|
||||
|
||||
// Loaning the pointer to others, retain ownership
|
||||
PyObject *operator*() const;
|
||||
|
||||
// Would like to call this pointer() but messes up STL in SeqBase<T>
|
||||
PyObject *ptr() const;
|
||||
//
|
||||
// Queries
|
||||
//
|
||||
|
||||
bool is(PyObject *pother) const;
|
||||
bool is(const SmartPtr &other) const;
|
||||
|
||||
bool isNull() const;
|
||||
};
|
||||
|
||||
BaseExport PyObject* new_reference_to(const SmartPtr&);
|
||||
|
||||
}
|
||||
|
||||
#endif // PY_SMARTPTRPY_H
|
||||
Reference in New Issue
Block a user