X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fbindable.h;h=8a5821b957cf4dd1569e3e840d6ad2ee8207e5ed;hb=47abe7c9e1633ca65f910a4db340724117a6f6e5;hp=f534d7afd8e9788e412dd0d2ddd876eee0091c8e;hpb=f33a98b1a044c8ac7b12778cbca6c4a124875e4a;p=libs%2Fgl.git diff --git a/source/bindable.h b/source/bindable.h index f534d7af..8a5821b9 100644 --- a/source/bindable.h +++ b/source/bindable.h @@ -71,8 +71,9 @@ does nothing upon destruction. class Bind { private: - typedef void CleanupFunc(); + typedef void CleanupFunc(int); + int slot; CleanupFunc *cleanup; public: @@ -85,25 +86,50 @@ public: template Bind(const T &o) { init(&o); } + template + Bind(T *o, S s) { init(o, s); } + + template + Bind(const T *o, S s) { init(o, s); } + + template + Bind(const T &o, S s) { init(&o, s); } + private: template void init(const T *o) { - cleanup = (o ? &unbind : 0); + cleanup = (o ? static_cast(&unbind) : 0); + slot = 0; if(o) o->bind(); else T::unbind(); } + template + void init(const T *o, S s) + { + cleanup = (o ? static_cast(&unbind_from) : 0); + slot = s; + if(o) + o->bind_to(s); + else + T::unbind_from(s); + } + public: ~Bind() - { if(cleanup) cleanup(); } + { if(cleanup) cleanup(slot); } private: template - static void unbind() + static void unbind(int) { T::unbind(); } + + template + static void unbind_from(int s) + { T::unbind_from(static_cast(s)); } }; @@ -113,9 +139,10 @@ Similar to Bind, but restores previous binding upon destruction. class BindRestore { private: - typedef void CleanupFunc(const void *); + typedef void CleanupFunc(const void *, int); const void *old; + int slot; CleanupFunc *cleanup; public: @@ -128,31 +155,63 @@ public: template BindRestore(const T &o) { init(&o); } + template + BindRestore(T *o, S s) { init(o, s); } + + template + BindRestore(const T *o, S s) { init(o, s); } + + template + BindRestore(const T &o, S s) { init(&o, s); } + private: template 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) T::unbind(); } + template + void init(T *o, S s) + { + old = T::current(s); + slot = s; + cleanup = (o!=old ? static_cast(&restore_to) : 0); + if(o) + o->bind_to(s); + else if(old) + T::unbind_from(s); + } + public: ~BindRestore() - { if(cleanup) cleanup(old); } + { if(cleanup) cleanup(old, slot); } private: template - static void restore(const void *o) + static void restore(const void *o, int) { if(o) reinterpret_cast(o)->bind(); else T::unbind(); } + + template + static void restore_to(const void *o, int si) + { + S s = static_cast(si); + if(o) + reinterpret_cast(o)->bind_to(s); + else + T::unbind_from(s); + } }; } // namespace GL