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(int);
81 Bind(T *o) { init(o); }
84 Bind(const T *o) { init(o); }
87 Bind(const T &o) { init(&o); }
89 template<typename T, typename S>
90 Bind(T *o, S s) { init(o, s); }
92 template<typename T, typename S>
93 Bind(const T *o, S s) { init(o, s); }
95 template<typename T, typename S>
96 Bind(const T &o, S s) { init(&o, s); }
100 void init(const T *o)
102 cleanup = (o ? &unbind<T> : 0);
109 template<typename T, typename S>
110 void init(const T *o, S s)
112 cleanup = (o ? &unbind_from<T, S> : 0);
122 { if(cleanup) cleanup(slot); }
126 static void unbind(int)
129 template<typename T, typename S>
130 static void unbind_from(int s)
131 { T::unbind_from(static_cast<S>(s)); }
136 Similar to Bind, but restores previous binding upon destruction.
141 typedef void CleanupFunc(const void *, int);
145 CleanupFunc *cleanup;
149 BindRestore(T *o) { init(o); }
152 BindRestore(const T *o) { init(o); }
155 BindRestore(const T &o) { init(&o); }
157 template<typename T, typename S>
158 BindRestore(T *o, S s) { init(o, s); }
160 template<typename T, typename S>
161 BindRestore(const T *o, S s) { init(o, s); }
163 template<typename T, typename S>
164 BindRestore(const T &o, S s) { init(&o, s); }
171 cleanup = (o!=old ? &restore<T> : 0);
178 template<typename T, typename S>
183 cleanup = (o!=old ? &restore_to<T, S> : 0);
192 { if(cleanup) cleanup(old, slot); }
196 static void restore(const void *o, int)
199 reinterpret_cast<const T *>(o)->bind();
204 template<typename T, typename S>
205 static void restore_to(const void *o, int si)
207 S s = static_cast<S>(si);
209 reinterpret_cast<const T *>(o)->bind_to(s);