X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fbindable.h;h=bde5e85a11752b3a95a07de5187ad174b3c4a50a;hb=055f553b1a75f44e72f3c2b5a1c98c1e1e8f3f30;hp=d85ec78daac037710390a7949c1deb164c3dbb51;hpb=42ace9ac1350d3ae009bdd2fb335ac1e57d1b36b;p=libs%2Fgl.git diff --git a/source/bindable.h b/source/bindable.h index d85ec78d..bde5e85a 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,6 +33,36 @@ 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 @@ -98,11 +125,21 @@ private: 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 &);