python extended object to native c++ pointer
Im toying around with the idea to use python as an embedded scripting language for a project im working on and have got most things working. However i cant seem to be able to convert a python extended object back into a native c++ pointer. So this is my class: class CGEGameModeBase { public: virtual void FunctionCall()=0; virtual const char* StringReturn()=0; }; class CGEPYGameMode : public CGEGameModeBase, public boost::python::wrapper<CGEPYGameMode> { public: virtual void FunctionCall() { if (override f = this->get_override("FunctionCall")) f(); } virtual const char* StringReturn() { if (override f = this->get_override("StringReturn")) return f(); return "FAILED TO CALL"; } }; Boost wrapping: BOOST_PYTHON_MODULE(GEGameMode) { class_<CGEGameModeBase, boost::noncopyable>("CGEGameModeBase", no_init); class_<CGEPYGameMode, bases<CGEGameModeBase> >("CGEPYGameMode", no_init) .def("FunctionCall", &CGEPYGameMode::FunctionCall) .def("StringReturn", &CGEPYGameMode::StringReturn); } and the python code: import GEGameMode def Ident(): return "Alpha" def NewGamePlay(): return "NewAlpha" def NewAlpha(): import GEGameMode import GEUtil class Alpha(GEGameMode.CGEPYGameMode): def __init__(self): print "Made new Alpha!" def FunctionCall(self): GEUtil.Msg("This is function test Alpha!") def StringReturn(self): return "This is return test Alpha!" return Alpha() Now i can call the first to functions fine by doing this: const char* ident = extract< const char* >( GetLocalDict()["Ident"]() ); const char* newgameplay = extract< const char* >( GetLocalDict()["NewGamePlay"]() ); printf("Loading Script: %s\n", ident); CGEPYGameMode* m_pGameMode = extract< CGEPYGameMode* >( GetLocalDict()[newgameplay]() ); However when i try and convert the Alpha class back to its base class (last line above) i get an boost error: TypeError: No registered converter was able to extract a C++ pointer to type class CGEPYGameMode from this Python object of type Alpha I have done alot of searching on the net but cant work out how to convert the Alpha object into its base class pointer. I could leave it as an object but rather have it as a pointer so some non python aware code can use it. Any ideas? Better Formatted version: http://stackoverflow.com/questions/1355187/python-object-to-native-c-pointer
On 08/31/2009 07:51 AM, Mark Chandler wrote:
Im toying around with the idea to use python as an embedded scripting language for a project im working on and have got most things working. However i cant seem to be able to convert a python extended object back into a native c++ pointer.
[...]
import GEGameMode def Ident(): return "Alpha" def NewGamePlay(): return "NewAlpha" def NewAlpha(): import GEGameMode import GEUtil class Alpha(GEGameMode.CGEPYGameMode): def __init__(self): print "Made new Alpha!"
super(Alpha, self).__init__() is missing here.
def FunctionCall(self): GEUtil.Msg("This is function test Alpha!") def StringReturn(self): return "This is return test Alpha!" return Alpha()
Without explicit initialization of the base class, your Alpha instance is in fact not a CGEPYGameMode instance. HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin...
Stefan i spent sooooo long on this thank you! I got another quick question, how do i define the class in the global namespace but still refer to it in functions? I want to do something like this but keep getting compile errors. ////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// import GEUtil import GEGameMode charlieTemp = 0 class Charlie(GEGameMode.CGEPYGameMode): def __init__(self): super(Alpha, self).__init__() def FunctionCall(self): GEUtil.Msg("This is function test Charlie!") def StringReturn(self): return "This is return test Charlie!" def Ident(): return "Charlie" def NewGamePlay(): return "NewCharlie" def NewCharlie(): global charlieTemp charlieTemp = Charlie() return charlieTemp ////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// Loading Script: Charlie Traceback (most recent call last): File "test_c.py", line 25, in NewCharlie charlieTemp = Charlie() NameError: global name 'Charlie' is not defined Stefan Seefeld wrote:
On 08/31/2009 07:51 AM, Mark Chandler wrote:
Im toying around with the idea to use python as an embedded scripting language for a project im working on and have got most things working. However i cant seem to be able to convert a python extended object back into a native c++ pointer.
[...]
import GEGameMode def Ident(): return "Alpha" def NewGamePlay(): return "NewAlpha" def NewAlpha(): import GEGameMode import GEUtil class Alpha(GEGameMode.CGEPYGameMode): def __init__(self): print "Made new Alpha!"
super(Alpha, self).__init__()
is missing here.
def FunctionCall(self): GEUtil.Msg("This is function test Alpha!") def StringReturn(self): return "This is return test Alpha!" return Alpha()
Without explicit initialization of the base class, your Alpha instance is in fact not a CGEPYGameMode instance.
HTH, Stefan
On 08/31/2009 08:57 AM, Mark Chandler wrote:
Stefan i spent sooooo long on this thank you!
I got another quick question, how do i define the class in the global namespace but still refer to it in functions? I want to do something like this but keep getting compile errors.
The script itself looks fine (apart from some indentation quirks, which may stem from your use of tabs). I seem to remember some issues with global and local namespaces in boost::python::exec(), which may relate to this. What version of boost are you using ? How do you call exec() from C++ ? Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin...
im using boost 1_36_0 as the latest has compile issues. im loading it via the boost call: return boost::python::exec_file(file, GetGloablDict(), GetLocalDict()); where GetGloablDict is the main dict and GetLocalDict is a copy for this instant Stefan Seefeld wrote:
On 08/31/2009 08:57 AM, Mark Chandler wrote:
Stefan i spent sooooo long on this thank you!
I got another quick question, how do i define the class in the global namespace but still refer to it in functions? I want to do something like this but keep getting compile errors.
The script itself looks fine (apart from some indentation quirks, which may stem from your use of tabs). I seem to remember some issues with global and local namespaces in boost::python::exec(), which may relate to this. What version of boost are you using ? How do you call exec() from C++ ?
Regards, Stefan
I worked out what was causing it. I was using a diff dict for global and local thus locals couldnt see globals and globals couldnt see locals. Thanks for your help On 31/08/2009, at 9:12 PM, Mark Chandler wrote:
im using boost 1_36_0 as the latest has compile issues.
im loading it via the boost call: return boost::python::exec_file(file, GetGloablDict(), GetLocalDict ());
where GetGloablDict is the main dict and GetLocalDict is a copy for this instant
Stefan Seefeld wrote:
On 08/31/2009 08:57 AM, Mark Chandler wrote:
Stefan i spent sooooo long on this thank you!
I got another quick question, how do i define the class in the global namespace but still refer to it in functions? I want to do something like this but keep getting compile errors.
The script itself looks fine (apart from some indentation quirks, which may stem from your use of tabs). I seem to remember some issues with global and local namespaces in boost::python::exec(), which may relate to this. What version of boost are you using ? How do you call exec() from C++ ?
Regards, Stefan
_______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
participants (2)
-
Mark Chandler -
Stefan Seefeld