Browse Source

Automatically coerce Python function to CallbackObject

rdb 10 years ago
parent
commit
52fec094bc

+ 2 - 0
panda/src/putil/Sources.pp

@@ -27,6 +27,7 @@
     cachedTypedWritableReferenceCount.h cachedTypedWritableReferenceCount.I \
     callbackData.h callbackData.I \
     callbackObject.h callbackObject.I \
+    callbackObject_ext.h callbackObject_ext.I \
     clockObject.h clockObject.I \
     collideMask.h \
     copyOnWriteObject.h copyOnWriteObject.I \
@@ -139,6 +140,7 @@
     cachedTypedWritableReferenceCount.h cachedTypedWritableReferenceCount.I \
     callbackData.h callbackData.I \
     callbackObject.h callbackObject.I \
+    callbackObject_ext.h callbackObject_ext.I \
     clockObject.h clockObject.I \
     collideMask.h \
     copyOnWriteObject.h copyOnWriteObject.I \

+ 2 - 0
panda/src/putil/callbackObject.h

@@ -38,6 +38,8 @@ public:
 PUBLISHED:
   virtual void output(ostream &out) const;
 
+  EXTENSION(static PT(CallbackObject) make(PyObject *function));
+
 public:
   virtual void do_callback(CallbackData *cbdata);
 

+ 32 - 0
panda/src/putil/callbackObject_ext.I

@@ -0,0 +1,32 @@
+// Filename: callbackObject_ext.I
+// Created by:  rdb (25Feb15)
+//
+////////////////////////////////////////////////////////////////////
+//
+// 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."
+//
+////////////////////////////////////////////////////////////////////
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: Extension<CallbackObject>::make
+//       Access: Published
+//  Description: This static constructor is merely provided so that
+//               interrogate can automatically coerce Python
+//               functions when passing them to a C++ function that
+//               accepts a CallbackObject.
+////////////////////////////////////////////////////////////////////
+INLINE PT(CallbackObject) Extension<CallbackObject>::
+make(PyObject *function) {
+  if (function != Py_None && !PyCallable_Check(function)) {
+    PyErr_SetString(PyExc_TypeError, "expected callable or None");
+    return NULL;
+  } else {
+    return new PythonCallbackObject(function);
+  }
+}

+ 47 - 0
panda/src/putil/callbackObject_ext.h

@@ -0,0 +1,47 @@
+// Filename: callbackObject_ext.h
+// Created by:  rdb (25Feb15)
+//
+////////////////////////////////////////////////////////////////////
+//
+// 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."
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef CALLBACKOBJECT_EXT_H
+#define CALLBACKOBJECT_EXT_H
+
+#include "dtoolbase.h"
+
+#ifdef HAVE_PYTHON
+
+#include "extension.h"
+#include "pythonCallbackObject.h"
+#include "py_panda.h"
+
+////////////////////////////////////////////////////////////////////
+//       Class : Extension<CallbackObject>
+// Description : This class defines the extension methods for
+//               CallbackObject, which are called instead of
+//               any C++ methods with the same prototype.
+//
+//               This just defines a static constructor, which makes
+//               it possible for Interrogate to automatically accept
+//               a Python function wherever a CallbackObject is
+//               accepted.
+////////////////////////////////////////////////////////////////////
+template<>
+class Extension<CallbackObject> : public ExtensionBase<CallbackObject> {
+public:
+  INLINE static PT(CallbackObject) make(PyObject *function);
+};
+
+#include "callbackObject_ext.I"
+
+#endif  // HAVE_PYTHON
+
+#endif  // CALLBACKOBJECT_EXT_H