Is there a way to create a C ++ class from a python class and associate it with compilation time?

Is there a way to create a relatively clean C ++ class from a python class and link it at compile time?

For example, if I have this python class:

class CarDef: acceleration = 1000.0 brake = 1500.0 inertia = acceleration * 0.1 * brake def __init__(self): pass 

I would like to have an appropriate C ++ class:

 class CarDef { public: double acceleration; double brake; double inertia; CarDef() : acceleration( 1000.0 ) , brake( 1500.0 ) , inertia ( 150000.0 ) {}; }; 

The resulting C ++ class may be different, as well as the original python class: I can use the "getter methods" paradigm instead of the class attributes.

I am trying to create resource files in python that I can use in my C ++ application. The goal is to minimize the amount of code that the end user will need to write to add and use parameters; and he should avoid string comparisons during the "startup phase" (this is allowed during the "initialization phase").

I would like the user to enter the name of the resource only twice: once in the python class and once in the place where the resource will be used in C ++, assuming that the β€œmagic” will bind two elements (or at runtime (which , I doubt it can be done without comparing strings), or at compile time (an intermediate step generates a C ++ class before compiling the project)). This is why I am moving from python to C ++; I believe that the transition from C ++ to python will require at least two python files: one that is generated and one that inherits from the latter (so as not to overwrite the resources already specified).

End-user usage will look like this:

 // A singleton definition manager class DefManager { CarDef mCarDef; public: static DefManager& GetReference() { static DefManager instance; return instance; } CarDef& getCarDef() { return mCarDef; } }; // One would use the generated CarDef class like this: mCar.setSpeed( mCar.getSpeed() + DefManager.GetReference().getCarDef().acceleration ); 

With this in mind, python code is strictly outside of C ++ code.

One obvious problem I see is finding out what type the python attribute or method returns. I saw a few examples of Cython , and it looks like it can use types (that's great!), But I have not seen any examples where it could do what I need. In addition, the c generated code seems to still need Python.h and thus the cpython api library when compiling.

Are there any ways to achieve this? Is there a better way to do this?

  • I am using python 3.2+.
  • I am using MS Visual Studio 2010 (we plan to switch to 2013 soon).
  • I am on a 32-bit system (but we plan to switch to 64 bit, OS and developed software soon).
+7
c ++ python
source share
3 answers

There is a way to go from C ++ to Python, but I don't know how to go from Python to C ++. If you don't mind writing your C ++ code, you can use the SWIG tool to automatically create Python classes for you.

Note that there are several limitations to handling exceptions. You can configure your Python code to throw C ++ exceptions, but the type of exception may be lost during translation. You also need to pay attention to the processing of reference counted objects. SWIG will generate a reference count for Python, which can sometimes delete objects unexpectedly.

If you don't like using a tool like SWIG, there is also Boost.Python for C ++. Again, this is C ++ for Python bindings and does not generate C ++ from Python.

+1
source share

You can embed python in your C ++ code or vice versa. There are tons of helper functions, albeit a bit ugly, that can be very powerful and able to accomplish what you want, although I'm not sure I fully understand your question. It does not require cython api, but still requires Python.h.

0
source share

There is a logical problem with what you ask.
Python is weakly typed.
In python, you can even change the type of a specific data item at runtime.
So let's say you have two types of objects

 CarDef 

Lets call them obj1 and obj2 . Let's say you have a setter:

 setIntX(self): self.x = 5 

and let's say you also have a setter:

 setStringX(self): self.x = "5" 

Then what type will the x member in your C ++ class have?
This can only be solved at run time, and more than one C ++ class may be required to model one python class.
However, a template class from python may be possible, and quite interesting in fact.
It is also possible that a general solution is not possible, but if you assume that the member is not of an ambiguous type, it is possible.

0
source share

All Articles