]> git.mxchange.org Git - simgear.git/commitdiff
Add convenience class for change listener callbacks.
authorThorstenB <brehmt@gmail.com>
Mon, 3 Oct 2011 09:57:48 +0000 (11:57 +0200)
committerThorstenB <brehmt@gmail.com>
Mon, 3 Oct 2011 09:57:48 +0000 (11:57 +0200)
simgear/props/props.hxx

index 8cf38e6da7954becd375207277a69e56381e837f..38c9c0a2e2ea058cdd9eb684b047ef00970e789b 100644 (file)
@@ -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 T>
+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