Browse Source

ode: remove dependency on Python.h from odeBody.h

rdb 7 years ago
parent
commit
82459fa21b

+ 1 - 3
panda/src/ode/odeBody.I

@@ -89,12 +89,10 @@ set_data(void *data) {
   dBodySetData(_id, data);
   dBodySetData(_id, data);
 }
 }
 
 
-#ifndef HAVE_PYTHON
-INLINE void* OdeBody::
+INLINE void *OdeBody::
 get_data() const {
 get_data() const {
   return dBodyGetData(_id);
   return dBodyGetData(_id);
 }
 }
-#endif
 
 
 INLINE void OdeBody::
 INLINE void OdeBody::
 set_position(dReal x, dReal y, dReal z) {
 set_position(dReal x, dReal y, dReal z) {

+ 4 - 23
panda/src/ode/odeBody.cxx

@@ -15,10 +15,6 @@
 #include "odeBody.h"
 #include "odeBody.h"
 #include "odeJoint.h"
 #include "odeJoint.h"
 
 
-#ifdef HAVE_PYTHON
-#include "py_panda.h"
-#endif
-
 TypeHandle OdeBody::_type_handle;
 TypeHandle OdeBody::_type_handle;
 
 
 OdeBody::
 OdeBody::
@@ -38,29 +34,14 @@ OdeBody::
 
 
 void OdeBody::
 void OdeBody::
 destroy() {
 destroy() {
-#ifdef HAVE_PYTHON
-  Py_XDECREF((PyObject*) dBodyGetData(_id));
-#endif
+  if (_destroy_callback != nullptr) {
+    _destroy_callback(*this);
+    _destroy_callback = nullptr;
+  }
   nassertv(_id);
   nassertv(_id);
   dBodyDestroy(_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::
 OdeJoint OdeBody::
 get_joint(int index) const {
 get_joint(int index) const {
   nassertr(_id != nullptr, OdeJoint(nullptr));
   nassertr(_id != nullptr, OdeJoint(nullptr));

+ 8 - 7
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_flag(int do_auto_disable);
   INLINE void set_auto_disable_defaults();
   INLINE void set_auto_disable_defaults();
   INLINE void set_data(void *data);
   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(dReal x, dReal y, dReal z);
   INLINE void set_position(const LVecBase3f &pos);
   INLINE void set_position(const LVecBase3f &pos);
@@ -71,11 +69,10 @@ PUBLISHED:
   INLINE int   get_auto_disable_steps() const;
   INLINE int   get_auto_disable_steps() const;
   INLINE dReal get_auto_disable_time() const;
   INLINE dReal get_auto_disable_time() const;
   INLINE int   get_auto_disable_flag() 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
 #endif
+  EXTENSION(PyObject *get_data() const);
 
 
   INLINE LVecBase3f  get_position() const;
   INLINE LVecBase3f  get_position() const;
   INLINE LMatrix3f  get_rotation() const;
   INLINE LMatrix3f  get_rotation() const;
@@ -150,6 +147,10 @@ PUBLISHED:
 private:
 private:
   dBodyID _id;
   dBodyID _id;
 
 
+public:
+  typedef void (*DestroyCallback)(OdeBody &body);
+  DestroyCallback _destroy_callback = nullptr;
+
 public:
 public:
   static TypeHandle get_class_type() {
   static TypeHandle get_class_type() {
     return _type_handle;
     return _type_handle;

+ 13 - 0
panda/src/ode/odeBody_ext.I

@@ -13,6 +13,19 @@
 
 
 #include "odeJoint_ext.h"
 #include "odeJoint_ext.h"
 
 
+/**
+ * Returns the custom data associated with the OdeBody.
+ */
+INLINE PyObject *Extension<OdeBody>::
+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()
  * Equivalent to get_joint().convert()
  */
  */

+ 37 - 0
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<OdeBody>::
+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);
+}

+ 3 - 0
panda/src/ode/odeBody_ext.h

@@ -30,6 +30,9 @@
 template<>
 template<>
 class Extension<OdeBody> : public ExtensionBase<OdeBody> {
 class Extension<OdeBody> : public ExtensionBase<OdeBody> {
 public:
 public:
+  void set_data(PyObject *);
+  INLINE PyObject *get_data() const;
+
   INLINE PyObject *get_converted_joint(int i) const;
   INLINE PyObject *get_converted_joint(int i) const;
 };
 };
 
 

+ 1 - 0
panda/src/ode/p3ode_ext_composite.cxx

@@ -1,3 +1,4 @@
+#include "odeBody_ext.cxx"
 #include "odeGeom_ext.cxx"
 #include "odeGeom_ext.cxx"
 #include "odeJoint_ext.cxx"
 #include "odeJoint_ext.cxx"
 #include "odeSpace_ext.cxx"
 #include "odeSpace_ext.cxx"