python object to native c++ pointer

人走茶凉 提交于 2019-12-01 00:43:09

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!