X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fbindable.h;h=b2634ea92bd4b1b43bb1f88b502af230f1e3c4bd;hp=8a5821b957cf4dd1569e3e840d6ad2ee8207e5ed;hb=HEAD;hpb=2c9a7979a41def3ee4c7fea7163718af25a26e61 diff --git a/source/bindable.h b/source/bindable.h deleted file mode 100644 index 8a5821b9..00000000 --- a/source/bindable.h +++ /dev/null @@ -1,220 +0,0 @@ -#ifndef MSP_GL_BINDABLE_H_ -#define MSP_GL_BINDABLE_H_ - -namespace Msp { -namespace GL { - -/** -A helper class for single-point binding. Provides tracking of the currently -bound object. -*/ -template -class Bindable -{ -protected: - static const T *cur_obj; - - Bindable() { } - - static bool set_current(const T *obj) - { - if(obj==cur_obj) - return false; - - cur_obj = obj; - return true; - } - -public: - static const T *current() { return cur_obj; } -}; - -template -const T *Bindable::cur_obj; - - -/** -A helper class for Bindables that revert to a default object on unbind. -*/ -template -class BindableWithDefault: protected Bindable -{ -protected: - BindableWithDefault() { } - -public: - static const T *current() - { - if(!Bindable::cur_obj) - Bindable::cur_obj = &default_object(); - return Bindable::cur_obj; - } - - static void unbind() - { - default_object().bind(); - } - - static const T &default_object() - { - static T obj; - return obj; - } -}; - - -/** -RAII class for binding things. Binds the thing upon construction and unbinds -it upon destruction. If a null pointer is given, unbinds upon construction and -does nothing upon destruction. -*/ -class Bind -{ -private: - typedef void CleanupFunc(int); - - int slot; - CleanupFunc *cleanup; - -public: - template - Bind(T *o) { init(o); } - - template - Bind(const T *o) { init(o); } - - 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 ? 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(slot); } - -private: - template - static void unbind(int) - { T::unbind(); } - - template - static void unbind_from(int s) - { T::unbind_from(static_cast(s)); } -}; - - -/** -Similar to Bind, but restores previous binding upon destruction. -*/ -class BindRestore -{ -private: - typedef void CleanupFunc(const void *, int); - - const void *old; - int slot; - CleanupFunc *cleanup; - -public: - template - BindRestore(T *o) { init(o); } - - template - BindRestore(const T *o) { init(o); } - - 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(); - 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, slot); } - -private: - template - 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 -} // namespace Msp - -#endif