views:

166

answers:

9

Hi,

I'm really new to C++ and I've come across a problem I've not been able to solve by reading documentations.

I want to embed a script language into my c++ application. That language could be javascript, lua or preferably python.

I'm not looking for something like Boost.Python / swig, something that is able to wrap my c++ functions / classes to a python interface, but rather a python_evaluate_and_return_result_as_variable("my_code"); function.

I have a whole bunch of structs containing a few integers:

struct my_integers {
    int a;
    int b;
    int c;
    int d;
    int e;
};

Now I want to do some math with these integers, for example:

i.a = i.c * i.e;

The math I want to do will be changing a lot in the future and I need people other then me be able to change the math without having access to the c++ code.

I'm thinking about a code structure like this:

  1. I initialize my struct and fill it with the starting values
  2. I load an external python function, lets say "my_python_function", that takes the struct as an argument and does so math with it before returning it.
  3. I get my struct like i = my_python_function_cppwrapper(i)

Is something like that possible? I googled a lot for this but the only thing I seem to find are wrappers that provide c++ -> python (or the other way around) functionallity without really interacting with variables.

I'd be really thankful for any help,
Robin.

+1  A: 

How about embedding a JavaScript engine, such as V8?

Matthijs Bierman
I think I'm sticking with python but I may use V8 in my next project. Thanks for the hint, the only javascript engine I could think about was spidermonkey.
Robin
+4  A: 

If you want to simply run Python scripts from C/C++, then use the Python C API. In your C/C++ code:

PyRun_SimpleString("import math; x = math.sqrt(2 * 2)");

For more complicated things, you will have to look at the API docs, but it's pretty straightforward.

Quartz
That doesn't quite work for me as I need to pass and extract variables, but thanks for your comment :)
Robin
+5  A: 

The Python documentation has a page on embedding Python in a C or C++ application.

Jon Purdy
+4  A: 

Why not use Boost.Python? You can expose your data classes to Python and execute a script/function as described here.

David Feurle
Thank you very much, that was what I've been looking for. I must've missed the section when I looked over Boost.Python.
Robin
A: 

dont forget the grand-daddy of embedded scripting language - tcl.

tcl has v nice c++ wrapper (modelled on boost.python) that makes it trivial to invoke and to wire up callbacks to your code

pm100
A: 

Lua works pretty well too, especially since its small, is ansi c compliant, has a low memory foot print along with a great wiki and messaging list. If you need even more speed there is a x86 32 and 64 bit jit version(luajit). Binding can be done with an array of tools/libraries, like swig or lunar(the wiki lists them all). The only problem that i can see is binding the struct members so they can be referenced directly(ie: struct.member = 4), though its possible to set this up with metatables that have get and set methods bound to variable names

Necrolis
A: 

You say that you're not looking for something to wrap your C++ functions / classes in a Python interface, but if you want Python code to be able to refer to members of your C++ my_integers structure, that is wrapping C++ classes in a Python interface. Of course, you're free to wrap as many or as few classes as you want - in this example, you'd wrap my_integers, then you'd embed a Python interpreter to do stuff with my_integers.

Josh Kelley
A: 

For something as simple as you describe, you could implement an interpreter for your own 'little language'. You could even call it the "Robin" language. ;-)

martineau
A: 

I advice using Lua as internal scripting engine. Implementation is just a few lines, and though light, the language has sufficient power. So no need for TCL. You might as well look at python, integration in C++ is rather easy, as there exists a Boost.Python implementation facilitating integration.

But depending on the application, I'd still recommend Lua.

polemon