Skip to content Skip to sidebar Skip to footer

Python 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 conve

Solution 1:

Thanks to Stefan from the python c++ mailling list, i was missing

super(Alpha, self).__init__()

from the constructor call meaning it never made the parent class. Thought this would of been automatic :D

Only other issue i had was saving the new class instance as a global var otherwise it got cleaned up as it went out of scope.

So happy now

Solution 2:

May not be the answer you are looking for, but take a look at ChaiScript for embedding in your C++ application.

According to their website,

ChaiScript is the first and only scripting language designed from the ground up with C++ compatibility in mind. It is an ECMAScript-inspired, embedded functional-like language.

ChaiScript has no meta-compiler, no library dependencies, no build system requirements and no legacy baggage of any kind. At can work seamlessly with any C++ functions you expose to it. It does not have to be told explicitly about any type, it is function centric.

With ChaiScript you can literally begin scripting your application by adding three lines of code to your program and not modifying your build steps at all.

Solution 3:

Well, I am not sure whether it will help you, but I had the same problem with scripts in Lua. We created objects from Lua and wanted some c++ code to handle the objects via pointers. We did the following:

  • all object stuff was written in c++, including constructors, destructors and factory method;
  • lua code was calling a factory method to create an object. this factory method 1) gave the object a unique ID number and 2) registered it in the c++ map, that mapped ID numbers to native pointers;
  • so, whenever lua was going to pass a pointer to c++ code, it gave an object ID instead, and the c++ code looked up the map for finding the actual pointer by ID.

Post a Comment for "Python Object To Native C++ Pointer"