From 82459fa21b47e89a301f5d7e394d868bb254f503 Mon Sep 17 00:00:00 2001 From: rdb Date: Tue, 6 Nov 2018 17:22:41 +0100 Subject: [PATCH] ode: remove dependency on Python.h from odeBody.h --- panda/src/ode/odeBody.I | 4 +-- panda/src/ode/odeBody.cxx | 27 +++---------------- panda/src/ode/odeBody.h | 15 ++++++----- panda/src/ode/odeBody_ext.I | 13 ++++++++++ panda/src/ode/odeBody_ext.cxx | 37 +++++++++++++++++++++++++++ panda/src/ode/odeBody_ext.h | 3 +++ panda/src/ode/p3ode_ext_composite.cxx | 1 + 7 files changed, 67 insertions(+), 33 deletions(-) create mode 100644 panda/src/ode/odeBody_ext.cxx diff --git a/panda/src/ode/odeBody.I b/panda/src/ode/odeBody.I index ab241fb455..72ecafd754 100644 --- a/panda/src/ode/odeBody.I +++ b/panda/src/ode/odeBody.I @@ -89,12 +89,10 @@ set_data(void *data) { dBodySetData(_id, data); } -#ifndef HAVE_PYTHON -INLINE void* OdeBody:: +INLINE void *OdeBody:: get_data() const { return dBodyGetData(_id); } -#endif INLINE void OdeBody:: set_position(dReal x, dReal y, dReal z) { diff --git a/panda/src/ode/odeBody.cxx b/panda/src/ode/odeBody.cxx index 70ad46169d..3f8e5d671a 100644 --- a/panda/src/ode/odeBody.cxx +++ b/panda/src/ode/odeBody.cxx @@ -15,10 +15,6 @@ #include "odeBody.h" #include "odeJoint.h" -#ifdef HAVE_PYTHON -#include "py_panda.h" -#endif - TypeHandle OdeBody::_type_handle; OdeBody:: @@ -38,29 +34,14 @@ OdeBody:: void OdeBody:: destroy() { -#ifdef HAVE_PYTHON - Py_XDECREF((PyObject*) dBodyGetData(_id)); -#endif + if (_destroy_callback != nullptr) { + _destroy_callback(*this); + _destroy_callback = nullptr; + } nassertv(_id); dBodyDestroy(_id); } -#ifdef HAVE_PYTHON -void OdeBody:: -set_data(PyObject *data) { - Py_XDECREF((PyObject*) dBodyGetData(_id)); - Py_XINCREF(data); - dBodySetData(_id, data); -} - -PyObject* OdeBody:: -get_data() const { - PyObject* data = (PyObject*) dBodyGetData(_id); - Py_XINCREF(data); - return data; -} -#endif - OdeJoint OdeBody:: get_joint(int index) const { nassertr(_id != nullptr, OdeJoint(nullptr)); diff --git a/panda/src/ode/odeBody.h b/panda/src/ode/odeBody.h index 8e27dd8802..f1cd9dae68 100644 --- a/panda/src/ode/odeBody.h +++ b/panda/src/ode/odeBody.h @@ -51,9 +51,7 @@ PUBLISHED: INLINE void set_auto_disable_flag(int do_auto_disable); INLINE void set_auto_disable_defaults(); INLINE void set_data(void *data); -#ifdef HAVE_PYTHON - void set_data(PyObject *data); -#endif + EXTENSION(void set_data(PyObject *data)); INLINE void set_position(dReal x, dReal y, dReal z); INLINE void set_position(const LVecBase3f &pos); @@ -71,11 +69,10 @@ PUBLISHED: INLINE int get_auto_disable_steps() const; INLINE dReal get_auto_disable_time() const; INLINE int get_auto_disable_flag() const; -#ifdef HAVE_PYTHON - PyObject* get_data() const; -#else - INLINE void* get_data() const; +#ifndef CPPPARSER + INLINE void *get_data() const; #endif + EXTENSION(PyObject *get_data() const); INLINE LVecBase3f get_position() const; INLINE LMatrix3f get_rotation() const; @@ -150,6 +147,10 @@ PUBLISHED: private: dBodyID _id; +public: + typedef void (*DestroyCallback)(OdeBody &body); + DestroyCallback _destroy_callback = nullptr; + public: static TypeHandle get_class_type() { return _type_handle; diff --git a/panda/src/ode/odeBody_ext.I b/panda/src/ode/odeBody_ext.I index a26eba5b41..5535b900dd 100644 --- a/panda/src/ode/odeBody_ext.I +++ b/panda/src/ode/odeBody_ext.I @@ -13,6 +13,19 @@ #include "odeJoint_ext.h" +/** + * Returns the custom data associated with the OdeBody. + */ +INLINE PyObject *Extension:: +get_data() const { + PyObject *data = (PyObject *)_this->get_data(); + if (data == nullptr) { + data = Py_None; + } + Py_INCREF(data); + return data; +} + /** * Equivalent to get_joint().convert() */ diff --git a/panda/src/ode/odeBody_ext.cxx b/panda/src/ode/odeBody_ext.cxx new file mode 100644 index 0000000000..59361c5fa1 --- /dev/null +++ b/panda/src/ode/odeBody_ext.cxx @@ -0,0 +1,37 @@ +/** + * PANDA 3D SOFTWARE + * Copyright (c) Carnegie Mellon University. All rights reserved. + * + * All use of this software is subject to the terms of the revised BSD + * license. You should have received a copy of this license along + * with this source code in a file named "LICENSE." + * + * @file odeBody_ext.cxx + * @author rdb + * @date 2018-11-06 + */ + +#include "odeBody_ext.h" + +static void destroy_callback(OdeBody &body) { + Py_XDECREF((PyObject *)body.get_data()); +} + +/** + * Sets custom data to be associated with the OdeBody. + */ +void Extension:: +set_data(PyObject *data) { + void *old_data = _this->get_data(); + + if (data != nullptr && data != Py_None) { + Py_INCREF(data); + _this->set_data((void *)data); + _this->_destroy_callback = &destroy_callback; + } else { + _this->set_data(nullptr); + _this->_destroy_callback = nullptr; + } + + Py_XDECREF((PyObject *)old_data); +} diff --git a/panda/src/ode/odeBody_ext.h b/panda/src/ode/odeBody_ext.h index 04c2244324..125c7fe509 100644 --- a/panda/src/ode/odeBody_ext.h +++ b/panda/src/ode/odeBody_ext.h @@ -30,6 +30,9 @@ template<> class Extension : public ExtensionBase { public: + void set_data(PyObject *); + INLINE PyObject *get_data() const; + INLINE PyObject *get_converted_joint(int i) const; }; diff --git a/panda/src/ode/p3ode_ext_composite.cxx b/panda/src/ode/p3ode_ext_composite.cxx index 02ebaec64c..ac054bdc49 100644 --- a/panda/src/ode/p3ode_ext_composite.cxx +++ b/panda/src/ode/p3ode_ext_composite.cxx @@ -1,3 +1,4 @@ +#include "odeBody_ext.cxx" #include "odeGeom_ext.cxx" #include "odeJoint_ext.cxx" #include "odeSpace_ext.cxx"