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:
@@ -30,6 +30,7 @@
|
||||
#include <App/PropertyLinks.h>
|
||||
#include <App/PropertyStandard.h>
|
||||
#include <Base/Matrix.h>
|
||||
#include <Base/SmartPtrPy.h>
|
||||
|
||||
#include <bitset>
|
||||
#include <unordered_map>
|
||||
@@ -609,7 +610,7 @@ protected:
|
||||
|
||||
/// python object of this class and all descendent
|
||||
protected: // attributes
|
||||
Py::Object PythonObject;
|
||||
Py::SmartPtr PythonObject;
|
||||
/// pointer to the document this object belongs to
|
||||
App::Document* _pDoc;
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
#include <Base/Interpreter.h>
|
||||
#include <Base/QuantityPy.h>
|
||||
#include <Base/Console.h>
|
||||
#include <CXX/Objects.hxx>
|
||||
|
||||
#include "ObjectIdentifier.h"
|
||||
#include "Application.h"
|
||||
|
||||
@@ -30,10 +30,11 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <boost/any.hpp>
|
||||
#include <CXX/Objects.hxx>
|
||||
#include <FCConfig.h>
|
||||
|
||||
|
||||
namespace Py {
|
||||
class Object;
|
||||
}
|
||||
namespace App
|
||||
{
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include <atomic>
|
||||
#include <Base/Tools.h>
|
||||
#include <Base/Writer.h>
|
||||
#include <CXX/Objects.hxx>
|
||||
|
||||
#include "Property.h"
|
||||
#include "ObjectIdentifier.h"
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "PreCompiled.h"
|
||||
|
||||
#include <Base/Console.h>
|
||||
#include <Base/PyObjectBase.h>
|
||||
#include <Base/Reader.h>
|
||||
#include <Base/Writer.h>
|
||||
#include <Base/Uuid.h>
|
||||
|
||||
@@ -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
|
||||
@@ -24,6 +24,7 @@
|
||||
#define MEASURE_MEASUREMENT_H
|
||||
|
||||
#include <Base/BaseClass.h>
|
||||
#include <Base/SmartPtrPy.h>
|
||||
#include <Base/Vector3D.h>
|
||||
|
||||
#include <App/DocumentObject.h>
|
||||
@@ -84,7 +85,7 @@ public:
|
||||
protected:
|
||||
TopoDS_Shape getShape(App::DocumentObject *obj , const char *subName) const;
|
||||
MeasureType measureType;
|
||||
Py::Object PythonObject;
|
||||
Py::SmartPtr PythonObject;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <App/DocumentObject.h>
|
||||
#include <App/PropertyLinks.h>
|
||||
#include <App/PropertyLinks.h>
|
||||
#include <Base/SmartPtrPy.h>
|
||||
#include "Cell.h"
|
||||
|
||||
namespace Spreadsheet
|
||||
@@ -283,7 +284,7 @@ private:
|
||||
std::map<std::string, App::CellAddress> revAliasProp;
|
||||
|
||||
/*! The associated python object */
|
||||
Py::Object PythonObject;
|
||||
Py::SmartPtr PythonObject;
|
||||
|
||||
std::map<const App::DocumentObject*, boost::signals2::scoped_connection> depConnections;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user