From: Martin G. <mar...@gm...> - 2009-01-28 11:06:24
|
Hi, I'm entirely new to SWIG, and to the python c/c++ interfaces and I'm trying to use it to integrate python with an application I need to write. My objective is to invoke a python script from my C++ program and pass the python script a pointer to a class that will allow the python script to obtain data it needs and call any methods it must in the hosting program. Currently I'm calling this class "ScriptCompanion". Defining the class, and creating the wrapper for it seems to be working, certainly exploring the generated ScriptCompanion.py shows it has declared all the methods I'm exposing. My class header looks like this: class ScriptCompanion { public: typedef std::map<std::string, std::string> ArgumentMap; ScriptCompanion(void); ~ScriptCompanion(void); std::string GetArgument(std::string name); void SetResultArgument(std::string name, std::string value); private: ArgumentMap m_inputArgs; ArgumentMap m_outputArgs; }; My interface file looks like this. %module ScriptCompanion %include "std_string.i" %include "typemaps.i" %{ #include "script_companion.h" %} class ScriptCompanion { public: std::string GetArgument(std::string name); void SetResultArgument(std::string name, std::string value); }; My C++ looks like this (ref counting and other boilerplate has been removed for brevity): PyObject* pName = PyString_FromString("TestModule"); PyObject* pModule = PyImport_Import(pName); pFunc = PyObject_GetAttrString(m_rModule.GetHandle(), "TestFunction"); ScriptCompanion* pCompanion = new ScriptCompanion(); PyObject* pArgs = PyTuple_New(1); This is where I come unstuck. How can I turn my ScriptCompanion pointer into a PyObject* which I can pass in as an argument? Is this documented anywhere? I've not been able to find it. Thanks, Martin |