diff --git a/panda/src/putil/Sources.pp b/panda/src/putil/Sources.pp index 7dc82eb5f4..8c72159364 100644 --- a/panda/src/putil/Sources.pp +++ b/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 \ diff --git a/panda/src/putil/callbackObject.h b/panda/src/putil/callbackObject.h index a8680e32fa..bb4f8f1223 100644 --- a/panda/src/putil/callbackObject.h +++ b/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); diff --git a/panda/src/putil/callbackObject_ext.I b/panda/src/putil/callbackObject_ext.I new file mode 100644 index 0000000000..0bf73b2e01 --- /dev/null +++ b/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::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:: +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); + } +} diff --git a/panda/src/putil/callbackObject_ext.h b/panda/src/putil/callbackObject_ext.h new file mode 100644 index 0000000000..1e0ceac72f --- /dev/null +++ b/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 +// 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 : public ExtensionBase { +public: + INLINE static PT(CallbackObject) make(PyObject *function); +}; + +#include "callbackObject_ext.I" + +#endif // HAVE_PYTHON + +#endif // CALLBACKOBJECT_EXT_H