I have a simulation program which repeats a set of functions in a specific order a large number of times and then outputs the result of some calculations based on those functions. Each function will mutate a Simulator object in some way and will take a variable number of arguments of different types. For example:
class Simulator
{
Simulator();
void A(int x, double y, string z);
void B(int x, int y);
void C(double x);
};
I want the user to be able to specify the order in which these functions will be called as well as the arguments, and I need to be able to store this information in some sort of script for the program to follow. For example, a script could end up being translated to the following actions for the program:
Simulator mySim;
mySim.A(5, 6.0, "a string");
mySim.B(1, 1);
mySim.A(3, 4.0, "another string");
mySim.C(10.0);
Is there an efficient way of doing this? My initial thought is to have a linked list of objects, each of which stores the name of the function, as well as the arguments. The program would then traverse the list, calling each function in turn. However, this poses a couple of problems:
(a) How to store the arguments in the list, which will be different for each function (both different in quantity as well as type).
(b) It seems rather inefficient as my program will have to execute a series of if statements on every run of the simulation, in order to determine which functions to call.
Anyone have any suggestions?
My suggestion: don't invent your own scripting language/interpreter.
If you want to quickly get started supporting user-defined scripts in a single day, check out Lua. It's arguably one of the easiest languages to embed into a C/C++ program. With luabind, it's even easier:
module(L)
[
class<Simulator>("Simulator")
.def("A", &Simulator::A)
.def("B", &Simulator::B)
.def("C", &Simulator::C)
];
Done! Now I can run lua scripts that can create Simulator and call functions on it and get all the features of the full lua language and its base libraries, computing mathematical expressions with each call, creating user-defined functions, closures, lambdas, etc. And lua is one speed demon when it comes to scripting languages.
I actually managed to turn a fairly large scale, commercial C++ program into a fully scriptable one with Lua in a single day. It's so easy to embed since that's what it was designed to do.
There's also Python which is quite similar to luabind if we use Boost.Python though a lot more painful with the C API and a bit more tedious to get started (we can't just build a statically linked library and go and there's a lot of environment variables we have to deal with like the sys.path and making sure we use the correct Python implementation for debug/release versions).
You could also choose among other languages, but those two of the most popular ones I know of which provide C++ libraries to make binding a lot easier.
You could also go with SWIG and support multiple scripting languages out of the box but SWIG takes quite a long time to learn since it's got massive documentation with a lot of details you have to pay attention to since it works for many different languages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With