[SOLVED] Programm internal scripting language
I would like to implement a scripting method for faster user input. This should be like programm specific internal programming, oder a kind of Macro.
- AutoCAD: the user can draw a line by clicking two point or by typing "line, x1, y1, x2, y2"
- gnuplot: changing the label by clicking in the menu "Axis / X Label" or typing "set xlabel 'abc'".
I guess there is a smarter way than do thomething like this:
case "abc": methodABC(); break;
case "def": mathodDEF(); break;
because, this structure will blow up, if there are a lot of methods available.
What's your question?
I want to know if there is a better way to find the function to call than a giant switch case statement.
Maybe there is a way to parse the input line (string) to the functions name, and call the function or something like that.
One way is to use a map to store a list of allowed input strings, and map it to function pointers: http://stackoverflow.com/questions/2136998/using-a-stl-map-of-function-pointers (the examples use std::map, but you can use a "QMap":http://qt-project.org/doc/qt-5.0/qtcore/qmap.html ). Then, use the input string as the key to retrieve the function to call.
However, the map technique can't easily handle functions that have different parameter types. A more flexible way is to use Qt's meta-object system -- this also lets you look up function names using strings. Read the documentation for "QMetaMethod":http://qt-project.org/doc/qt-5.0/qtcore/qmetamethod.html and "QMetaObject":http://qt-project.org/doc/qt-5.0/qtcore/qmetaobject.html. Your functions need to be member functions of a QObject.
it looks like QMap do what I need. The map example is also very nice. I guess I can use it for my purpose.
Thanks for your help JKSH!
You're welcome :) Please edit your original post an add "[SOLVED]" to the title!