From 2e13c1fa4bc13f2956ccc719d2ea223713fb1e85 Mon Sep 17 00:00:00 2001 From: ThorstenB Date: Mon, 3 Oct 2011 11:57:48 +0200 Subject: [PATCH] Add convenience class for change listener callbacks. --- simgear/props/props.hxx | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/simgear/props/props.hxx b/simgear/props/props.hxx index 8cf38e6d..38c9c0a2 100644 --- a/simgear/props/props.hxx +++ b/simgear/props/props.hxx @@ -1938,6 +1938,36 @@ struct Hash }; } } + +/** Convenience class for change listener callbacks without + * creating a derived class implementing a "valueChanged" method. + * Also removes listener on destruction automatically. + */ +template +class SGPropertyChangeCallback + : public SGPropertyChangeListener +{ +public: + SGPropertyChangeCallback(T* obj, void (T::*method)(SGPropertyNode*), + SGPropertyNode_ptr property,bool initial=false) + : _obj(obj), _callback(method), _property(property) + { + _property->addChangeListener(this,initial); + } + virtual ~SGPropertyChangeCallback() + { + _property->removeChangeListener(this); + } + void valueChanged (SGPropertyNode * node) + { + (_obj->*_callback)(node); + } +private: + T* _obj; + void (T::*_callback)(SGPropertyNode*); + SGPropertyNode_ptr _property; +}; + #endif // __PROPS_HXX // end of props.hxx