1 #ifndef MSP_GL_BINDABLE_H_
2 #define MSP_GL_BINDABLE_H_
8 A helper class for single-point binding. Provides tracking of the currently
15 static const T *cur_obj;
19 static bool set_current(const T *obj)
29 static const T *current() { return cur_obj; }
33 const T *Bindable<T>::cur_obj;
37 A helper class for Bindables that revert to a default object on unbind.
40 class BindableWithDefault: protected Bindable<T>
43 BindableWithDefault() { }
46 static const T *current()
48 if(!Bindable<T>::cur_obj)
49 Bindable<T>::cur_obj = &default_object();
50 return Bindable<T>::cur_obj;
55 default_object().bind();
58 static const T &default_object()
67 RAII class for binding things. Binds the thing upon construction and unbinds
68 it upon destruction. If a null pointer is given, unbinds upon construction and
69 does nothing upon destruction. Optionally can restore the previous binding.
100 template<typename T, typename U>
106 Binder2(const T *o, const U *l):
129 Bind(const T &o, bool r = false):
130 binder(r ? create(&o, T::current()) : create(&o))
134 Bind(const T *o, bool r = false):
135 binder(r ? create(o, T::current()) : create(o))
139 Bind(T *o, bool r = false):
140 binder(r ? create(o, T::current()) : create(o))
145 Bind &operator=(const Bind &);
148 ~Bind() { delete binder; }
152 Base *create(const T *o)
153 { return new Binder1<T>(o); }
155 template<typename T, typename U>
156 Base *create(const T *o, const U *l)
157 { return new Binder2<T, U>(o, l); }