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 ? static_cast<CleanupFunc *>(&unbind<T>) : 0);
110 template<typename T, typename S>
111 void init(const T *o, S s)
113 cleanup = (o ? static_cast<CleanupFunc *>(&unbind_from<T, S>) : 0);
123 { if(cleanup) cleanup(slot); }
127 static void unbind(int)
130 template<typename T, typename S>
131 static void unbind_from(int s)
132 { T::unbind_from(static_cast<S>(s)); }
137 Similar to Bind, but restores previous binding upon destruction.
142 typedef void CleanupFunc(const void *, int);
146 CleanupFunc *cleanup;
150 BindRestore(T *o) { init(o); }
153 BindRestore(const T *o) { init(o); }
156 BindRestore(const T &o) { init(&o); }
158 template<typename T, typename S>
159 BindRestore(T *o, S s) { init(o, s); }
161 template<typename T, typename S>
162 BindRestore(const T *o, S s) { init(o, s); }
164 template<typename T, typename S>
165 BindRestore(const T &o, S s) { init(&o, s); }
173 cleanup = (o!=old ? static_cast<CleanupFunc *>(&restore<T>) : 0);
180 template<typename T, typename S>
185 cleanup = (o!=old ? static_cast<CleanupFunc *>(&restore_to<T, S>) : 0);
194 { if(cleanup) cleanup(old, slot); }
198 static void restore(const void *o, int)
201 reinterpret_cast<const T *>(o)->bind();
206 template<typename T, typename S>
207 static void restore_to(const void *o, int si)
209 S s = static_cast<S>(si);
211 reinterpret_cast<const T *>(o)->bind_to(s);