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;
18 ~Bindable() { if(cur_obj==this) T::unbind(); }
20 static bool set_current(const T *obj)
30 static const T *current() { return cur_obj; }
34 const T *Bindable<T>::cur_obj;
38 A helper class for Bindables that revert to a default object on unbind.
41 class BindableWithDefault: protected Bindable<T>
43 friend class Bindable<T>;
46 BindableWithDefault() { }
47 ~BindableWithDefault() { if(this==&default_object()) Bindable<T>::set_current(0); }
50 static const T *current()
52 if(!Bindable<T>::cur_obj)
53 Bindable<T>::cur_obj = &default_object();
54 return Bindable<T>::cur_obj;
59 default_object().bind();
62 static const T &default_object()
71 RAII class for binding things. Binds the thing upon construction and unbinds
72 it upon destruction. If a null pointer is given, unbinds upon construction and
73 does nothing upon destruction.
78 typedef void CleanupFunc(int);
85 Bind(T *o) { init(o); }
88 Bind(const T *o) { init(o); }
91 Bind(const T &o) { init(&o); }
93 template<typename T, typename S>
94 Bind(T *o, S s) { init(o, s); }
96 template<typename T, typename S>
97 Bind(const T *o, S s) { init(o, s); }
99 template<typename T, typename S>
100 Bind(const T &o, S s) { init(&o, s); }
104 void init(const T *o)
106 cleanup = (o ? static_cast<CleanupFunc *>(&unbind<T>) : 0);
114 template<typename T, typename S>
115 void init(const T *o, S s)
117 cleanup = (o ? static_cast<CleanupFunc *>(&unbind_from<T, S>) : 0);
127 { if(cleanup) cleanup(slot); }
131 static void unbind(int)
134 template<typename T, typename S>
135 static void unbind_from(int s)
136 { T::unbind_from(static_cast<S>(s)); }
141 Similar to Bind, but restores previous binding upon destruction.
146 typedef void CleanupFunc(const void *, int);
150 CleanupFunc *cleanup;
154 BindRestore(T *o) { init(o); }
157 BindRestore(const T *o) { init(o); }
160 BindRestore(const T &o) { init(&o); }
162 template<typename T, typename S>
163 BindRestore(T *o, S s) { init(o, s); }
165 template<typename T, typename S>
166 BindRestore(const T *o, S s) { init(o, s); }
168 template<typename T, typename S>
169 BindRestore(const T &o, S s) { init(&o, s); }
177 cleanup = (o!=old ? static_cast<CleanupFunc *>(&restore<T>) : 0);
184 template<typename T, typename S>
189 cleanup = (o!=old ? static_cast<CleanupFunc *>(&restore_to<T, S>) : 0);
198 { if(cleanup) cleanup(old, slot); }
202 static void restore(const void *o, int)
205 reinterpret_cast<const T *>(o)->bind();
210 template<typename T, typename S>
211 static void restore_to(const void *o, int si)
213 S s = static_cast<S>(si);
215 reinterpret_cast<const T *>(o)->bind_to(s);