X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fbindable.h;h=f534d7afd8e9788e412dd0d2ddd876eee0091c8e;hb=f33a98b1a044c8ac7b12778cbca6c4a124875e4a;hp=d85ec78daac037710390a7949c1deb164c3dbb51;hpb=42ace9ac1350d3ae009bdd2fb335ac1e57d1b36b;p=libs%2Fgl.git diff --git a/source/bindable.h b/source/bindable.h index d85ec78d..f534d7af 100644 --- a/source/bindable.h +++ b/source/bindable.h @@ -1,16 +1,13 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2010 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #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 { @@ -36,88 +33,126 @@ 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. Optionally can restore the previous binding. +does nothing upon destruction. */ class Bind { private: - struct Base - { - virtual ~Base() { } - }; + typedef void CleanupFunc(); + + CleanupFunc *cleanup; +public: 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 + Bind(T *o) { init(o); } + + template + Bind(const T *o) { init(o); } + + template + Bind(const T &o) { init(&o); } + +private: + template + void init(const T *o) { - 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; + cleanup = (o ? &unbind : 0); + if(o) + o->bind(); + else + T::unbind(); + } public: + ~Bind() + { if(cleanup) cleanup(); } + +private: template - Bind(const T *o, bool r = false): - binder(r ? create(o, T::current()) : create(o)) - { } + static void unbind() + { T::unbind(); } +}; + +/** +Similar to Bind, but restores previous binding upon destruction. +*/ +class BindRestore +{ private: - Bind(const Bind &); - Bind &operator=(const Bind &); + typedef void CleanupFunc(const void *); + + const void *old; + CleanupFunc *cleanup; public: - ~Bind() { delete binder; } + template + BindRestore(T *o) { init(o); } + + template + BindRestore(const T *o) { init(o); } + + template + BindRestore(const T &o) { init(&o); } private: template - Base *create(const T *o) - { return new Binder1(o); } + void init(T *o) + { + old = T::current(); + cleanup = (o!=old ? &restore : 0); + if(o) + o->bind(); + else if(old) + T::unbind(); + } + +public: + ~BindRestore() + { if(cleanup) cleanup(old); } - template - Base *create(const T *o, const U *l) - { return new Binder2(o, l); } +private: + template + static void restore(const void *o) + { + if(o) + reinterpret_cast(o)->bind(); + else + T::unbind(); + } }; } // namespace GL