We have an expression language for one of our applications, that has variables. The expression is parsed once, a tree is built and it can be evaluated many times while the variables in the expression change. Variables have a callback, so the evaluator can request the current value.
Code:
namespace ExpressionLanguage
{
class CSAZVariable : public CSAZNamedType
{
public:
typedef CSAZValue (*GET_VALUE_CALLBACK)( const CSAZVariable&, void* );
private:
GET_VALUE_CALLBACK m_CallBack;
void* m_pUserData;
public:
CSAZValue GetValue() const;
CSAZVariable( const wstring& name, ESAZDataType datatype, GET_VALUE_CALLBACK callback, void* userdata = NULL );
};
}
As you can see, it's a simple function pointer with userdata, so basically everybody can have a static function, cast "userdata" to whatever neccessary and read the current value from there. It works fine, however it strikes me as somewhat old fashioned. More "C with classes" than C++.
Can anyone point me to a more "C++"y way of doing callbacks or post a small example? Preferably without the use of Boost, just plain STL.