Text archives Help
- From: bigler@sci.utah.edu
- To: manta@sci.utah.edu
- Subject: [MANTA] r1011 - trunk/SwigInterface
- Date: Mon, 24 Apr 2006 11:39:06 -0600 (MDT)
Author: bigler
Date: Mon Apr 24 11:39:05 2006
New Revision: 1011
Added:
trunk/SwigInterface/pycallback.cc
trunk/SwigInterface/pycallback.h
trunk/SwigInterface/pycallback.i
Modified:
trunk/SwigInterface/CMakeLists.txt
Log:
This will provide an extension that allows you to get pointers to
python functions and arguments from a C++ class called PyCallback.
Future version will use this as a stepping stone to make callbacks
from any thread in Manta.
Modified: trunk/SwigInterface/CMakeLists.txt
==============================================================================
--- trunk/SwigInterface/CMakeLists.txt (original)
+++ trunk/SwigInterface/CMakeLists.txt Mon Apr 24 11:39:05 2006
@@ -37,6 +37,14 @@
-lm)
############################################################
+# Python callback code
+SET_SOURCE_FILES_PROPERTIES(pycallback.i PROPERTIES CPLUSPLUS ON)
+SET_SOURCE_FILES_PROPERTIES(pycallback.i PROPERTIES SWIG_FLAGS "-Wall")
+SWIG_ADD_MODULE(pycallback python
+ pycallback.i pycallback.cc pycallback.h)
+SWIG_LINK_LIBRARIES(pycallback ${PYTHON_LIBRARIES})
+
+############################################################
# Load a scene from a python script.
SET(SCENE_PYTHON 0 CACHE BOOL "Load a scene from a python script..")
IF(SCENE_PYTHON)
Added: trunk/SwigInterface/pycallback.cc
==============================================================================
--- (empty file)
+++ trunk/SwigInterface/pycallback.cc Mon Apr 24 11:39:05 2006
@@ -0,0 +1,173 @@
+/*
+ For more information, please see:
http://software.sci.utah.edu
+
+ The MIT License
+
+ Copyright (c) 2005-2006
+ Scientific Computing and Imaging Institute, University of Utah
+
+ License for the specific language governing rights and limitations under
+ Permission is hereby granted, free of charge, to any person obtaining a
+ copy of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ DEALINGS IN THE SOFTWARE.
+*/
+
+#include <SwigInterface/pycallback.h>
+
+using namespace Manta;
+
+PyCallback::PyCallback()
+ : function(NULL),
+ args(NULL)
+{
+ fprintf(stderr, "Constructor called for %p\n", this);
+}
+
+PyCallback::PyCallback(PyObject* function, PyObject* args)
+ : function(function),
+ args(args)
+{
+ fprintf(stderr, "Constructor called for %p\n", this);
+ // Do reference counting
+ Py_XINCREF(function);
+ Py_XINCREF(args);
+}
+
+PyCallback::PyCallback(const PyCallback& copy)
+ : function(copy.function),
+ args(copy.args)
+{
+ fprintf(stderr, "Constructor called for %p\n", this);
+ // Do reference counting
+ Py_XINCREF(function);
+ Py_XINCREF(args);
+}
+
+PyCallback::~PyCallback() {
+ fprintf(stderr, "Destructor called for %p\n", this);
+ // Do reference counting
+ Py_XDECREF(function);
+ Py_XDECREF(args);
+}
+
+void
+PyCallback::destroy(void* obj) {
+ PyCallback* cb = reinterpret_cast<PyCallback*>(obj);
+ delete cb;
+}
+
+PyObject*
+PyCallback::call(PyObject* new_args)
+{
+ PyObject* result;
+
+ if (new_args == NULL) {
+ // Use our stored args
+ result = PyEval_CallObject(function, args);
+ } else {
+ // Use the incoming args
+ if (!PyTuple_Check(new_args)) {
+ PyErr_SetString(PyExc_TypeError, "argument needs to be a tuple");
+ return NULL;
+ }
+
+ result = PyEval_CallObject(function, new_args);
+ }
+
+ return result;
+}
+
+#if 0
+PyObject*
+Manta::create_python_callback(PyObject* self, PyObject* args)
+{
+ PyObject* result = NULL;
+ PyObject* funct;
+ PyObject* funct_args;
+
+ int num_args = PyTuple_Size(args);
+ if (num_args < 1) {
+ PyErr_SetString(PyExc_TypeError, "need at least 1 argument (None
given)");
+ return NULL;
+ }
+
+ funct = PyTuple_GetItem(args, 0);
+ if (!PyCallable_Check(funct)) {
+ PyErr_SetString(PyExc_TypeError, "first parameter must be callable");
+ return NULL;
+ }
+
+ // Now slice out the arguments
+ funct_args = PyTuple_GetSlice(args, 1, num_args);
+ PyCallback* cb = new PyCallback(funct, funct_args);
+ fprintf(stderr, "cb = %p\n", cb);
+
+ result = PyCObject_FromVoidPtr(cb, PyCallback::destroy);
+ return result;
+}
+
+PyObject*
+Manta::call_python_callback(PyObject* self, PyObject* args)
+{
+ PyObject* result;
+
+ int num_args = PyTuple_Size(args);
+ if (num_args < 1) {
+ PyErr_SetString(PyExc_TypeError, "need at least 1 argument (None
given)");
+ return NULL;
+ }
+
+ // Returns a "borrowed reference", so cleanup unneeded
+ PyObject* pcb = PyTuple_GetItem(args, 0);
+ if (!PyCObject_Check(pcb)) {
+ PyErr_SetString(PyExc_TypeError, "first parameter must be Callback");
+ return NULL;
+ }
+ PyCallback* cb = reinterpret_cast<PyCallback*>(PyCObject_AsVoidPtr(pcb));
+ // Check to see if we are to use our args or the ones passed in.
+ // Returns a "new reference", so cleanup is needed.
+ PyObject* new_args = PyTuple_GetSlice(args, 1, num_args);
+ if (PyTuple_Size(new_args) > 0) {
+ // Use the incoming args
+ result = PyEval_CallObject(cb->function, new_args);
+ } else {
+ // Use our stored args
+ result = PyEval_CallObject(cb->function,
+ cb->args);
+ }
+ // Cleanup
+ Py_DECREF(new_args);
+
+ return result;
+}
+
+static PyMethodDef MyCallbackMethods[] = {
+ {"call_callback", Manta::call_python_callback, METH_VARARGS,
+ "Call a callable function."},
+ {"create_callback", Manta::create_python_callback, METH_VARARGS,
+ "Create a callback pointer for later."},
+ {NULL, NULL, 0, NULL} /* Sentinel */
+};
+
+PyMODINIT_FUNC
+initmanta_python_callback(void)
+{
+ (void) Py_InitModule("manta_python_callback", MyCallbackMethods);
+}
+
+
+#endif
Added: trunk/SwigInterface/pycallback.h
==============================================================================
--- (empty file)
+++ trunk/SwigInterface/pycallback.h Mon Apr 24 11:39:05 2006
@@ -0,0 +1,61 @@
+/*
+ For more information, please see:
http://software.sci.utah.edu
+
+ The MIT License
+
+ Copyright (c) 2005-2006
+ Scientific Computing and Imaging Institute, University of Utah
+
+ License for the specific language governing rights and limitations under
+ Permission is hereby granted, free of charge, to any person obtaining a
+ copy of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ DEALINGS IN THE SOFTWARE.
+*/
+
+
+#ifndef Manta_SwigInterface_pycallback_h
+#define Manta_SwigInterface_pycallback_h
+
+#include <Python.h>
+
+#include <stdio.h>
+
+namespace Manta {
+ struct PyCallback {
+ PyCallback();
+ PyCallback(PyObject* function, PyObject* args=0);
+ PyCallback(const PyCallback& copy);
+
+ ~PyCallback();
+
+ PyObject* call(PyObject* args = 0);
+
+ static void destroy(void* obj);
+
+ PyObject* function;
+ PyObject* args;
+
+ };
+
+#if 0
+ PyObject* create_python_callback(PyObject* self, PyObject* args);
+ PyObject* call_python_callback(PyObject* self, PyObject* args);
+#endif
+
+} // end namespace Manta
+
+#endif // #ifndef Manta_SwigInterface_pycallback_h
Added: trunk/SwigInterface/pycallback.i
==============================================================================
--- (empty file)
+++ trunk/SwigInterface/pycallback.i Mon Apr 24 11:39:05 2006
@@ -0,0 +1,52 @@
+%module pycallback
+
+
+
+// %typemap(in) PyObjectArgs {
+// $1 = $input
+// }
+
+%typemap(in) int {
+ $1 = PyInt_AsLong($input);
+}
+
+%{
+#include <stdio.h>
+%}
+
+void printInt(int val);
+
+%{
+ void printInt(int val) {
+ fprintf(stdout, "int = %d\n", val);
+ }
+%}
+
+%typemap(in) int;
+
+%typemap(in) PyObject* {
+ $1 = $input;
+}
+
+%typemap(out) PyObject* {
+ $result = $1;
+}
+
+int countArgs(PyObject* args);
+
+%{
+ int countArgs(PyObject* args) {
+ if (!PyTuple_Check(args)) {
+ fprintf(stderr, "Not a Tuple\n");
+ return 0;
+ }
+ int num_args = PyTuple_Size(args);
+ return num_args;
+ }
+%}
+
+%{
+#include <SwigInterface/python_callback.h>
+%}
+
+%include <SwigInterface/python_callback.h>
- [MANTA] r1011 - trunk/SwigInterface, bigler, 04/24/2006
Archive powered by MHonArc 2.6.16.