How should I embed Python in a C++ Builder / Delphi 2010 application?

社会主义新天地 提交于 2019-12-03 08:23:12

You should not be afraid of the P4D project at google groups. It seems inactive because, in part, it is very stable and full-featured already. Those components are used in the much more active PyScripter application which is one of the best python development editors currently available. PyScripter is written in Delphi and uses the P4D components. As such, it also presents a very comprehensive example of how to use the P4D components, although the examples provided with the P4D source checkout are already good enough to get started.

Is exposing internal classes or instantiated objects to Python as objects easy, or is the API truly C-style or flat / non-OO, and if so what's the best approach to mimic an underlying OO layer through such an API?

You have already answered yourself. The latter part of the sentence is correct.

Objects and classes do not exist in C++ as soon as you compile, only a few structures (vtables), and also another ones explaining some OO data, provided that RTTI is activated. That's why it is not possible to bridge the gap between Python and C++ using classes and objects.

You can build that surely by yourself, creating a set of C functions along with some data structures, and then an OO-layer. But you cannot do that out of the box.

For instance, class Car:

class Car {
public:
  int getDoors()
      { return this->doors; }
protected:
  int doors;
};

Is translated to:

struct Car {
    int doors;
};

int Car_getDoors(Car * this)
{
    return this->doors;
}

And a call to getDoors:

 c->getDoors()

Is translated as:

Car_getDoors( c )

You can generate C++ to $SCRIPTLANG wrappers with swig.

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