X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fbindable.h;h=b2634ea92bd4b1b43bb1f88b502af230f1e3c4bd;hp=8651c70ee066b6cbfa2e8f40cd967b0a92f148cd;hb=4b4d2a48048268d2ad48bafbce8647af8088573f;hpb=02feac06937ec4e8e122a833b4903ccf0bd6d030 diff --git a/source/bindable.h b/source/bindable.h index 8651c70e..b2634ea9 100644 --- a/source/bindable.h +++ b/source/bindable.h @@ -15,6 +15,7 @@ protected: static const T *cur_obj; Bindable() { } + ~Bindable() { if(cur_obj==this) T::unbind(); } static bool set_current(const T *obj) { @@ -39,8 +40,11 @@ A helper class for Bindables that revert to a default object on unbind. template class BindableWithDefault: protected Bindable { + friend class Bindable; + protected: BindableWithDefault() { } + ~BindableWithDefault() { if(this==&default_object()) Bindable::set_current(0); } public: static const T *current() @@ -52,7 +56,8 @@ public: static void unbind() { - default_object().bind(); + if(Bindable::cur_obj) + default_object().bind(); } static const T &default_object() @@ -99,7 +104,8 @@ private: template void init(const T *o) { - cleanup = (o ? &unbind : 0); + cleanup = (o ? static_cast(&unbind) : 0); + slot = 0; if(o) o->bind(); else @@ -109,7 +115,7 @@ private: template void init(const T *o, S s) { - cleanup = (o ? &unbind_from : 0); + cleanup = (o ? static_cast(&unbind_from) : 0); slot = s; if(o) o->bind_to(s); @@ -168,7 +174,8 @@ private: void init(T *o) { old = T::current(); - cleanup = (o!=old ? &restore : 0); + slot = 0; + cleanup = (o!=old ? static_cast(&restore) : 0); if(o) o->bind(); else if(old) @@ -180,7 +187,7 @@ private: { old = T::current(s); slot = s; - cleanup = (o!=old ? &restore_to : 0); + cleanup = (o!=old ? static_cast(&restore_to) : 0); if(o) o->bind_to(s); else if(old)