X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fbindable.h;h=b2634ea92bd4b1b43bb1f88b502af230f1e3c4bd;hp=8823de5c9773209a1642f5b763887e65ca7ea5d6;hb=HEAD;hpb=f14435e58bfa0fa697a06ba9a454bb30cd37d9d8 diff --git a/source/bindable.h b/source/bindable.h deleted file mode 100644 index 8823de5c..00000000 --- a/source/bindable.h +++ /dev/null @@ -1,129 +0,0 @@ -#ifndef MSP_GL_BINDABLE_H_ -#define MSP_GL_BINDABLE_H_ - -namespace Msp { -namespace GL { - -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; - - -/** -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. Optionally can restore the previous binding. -*/ -class Bind -{ -private: - struct Base - { - virtual ~Base() { } - }; - - template - struct Binder1: Base - { - const T *obj; - - Binder1(const T *o): - obj(o) - { - if(obj) - obj->bind(); - else - T::unbind(); - } - - ~Binder1() - { - if(obj) - obj->unbind(); - } - }; - - template - struct Binder2: Base - { - const T *obj; - const U *old; - - Binder2(const T *o, const U *l): - obj(o), - old(l) - { - if(obj) - obj->bind(); - else - T::unbind(); - } - - ~Binder2() - { - if(old) - old->bind(); - else if(obj) - obj->unbind(); - } - }; - - Base *binder; - -public: - template - Bind(const T &o, bool r = false): - binder(r ? create(&o, T::current()) : create(&o)) - { } - - template - Bind(const T *o, bool r = false): - binder(r ? create(o, T::current()) : create(o)) - { } - - template - Bind(T *o, bool r = false): - binder(r ? create(o, T::current()) : create(o)) - { } - -private: - Bind(const Bind &); - Bind &operator=(const Bind &); - -public: - ~Bind() { delete binder; } - -private: - template - Base *create(const T *o) - { return new Binder1(o); } - - template - Base *create(const T *o, const U *l) - { return new Binder2(o, l); } -}; - -} // namespace GL -} // namespace Msp - -#endif