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.
74 typedef void CleanupFunc();
80 Bind(T *o) { init(o); }
83 Bind(const T *o) { init(o); }
86 Bind(const T &o) { init(&o); }
92 cleanup = (o ? &unbind<T> : 0);
101 { if(cleanup) cleanup(); }
111 Similar to Bind, but restores previous binding upon destruction.
116 typedef void CleanupFunc(const void *);
119 CleanupFunc *cleanup;
123 BindRestore(T *o) { init(o); }
126 BindRestore(const T *o) { init(o); }
129 BindRestore(const T &o) { init(&o); }
136 cleanup = (o!=old ? &restore<T> : 0);
145 { if(cleanup) cleanup(old); }
149 static void restore(const void *o)
152 reinterpret_cast<const T *>(o)->bind();