3 This file is part of libmspgl
4 Copyright © 2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #ifndef MSP_GL_BINDABLE_H_
9 #define MSP_GL_BINDABLE_H_
18 static const T *cur_obj;
22 static bool set_current(const T *obj)
32 static const T *current() { return cur_obj; }
36 const T *Bindable<T>::cur_obj;
40 RAII class for binding things. Binds the thing upon construction and unbinds
41 it upon destruction. If a null pointer is given, unbinds upon construction and
42 does nothing upon destruction. Optionally can restore the previous binding.
73 template<typename T, typename U>
79 Binder2(const T *o, const U *l):
102 Bind(const T &o, bool r = false):
103 binder(r ? create(&o, T::current()) : create(&o))
107 Bind(const T *o, bool r = false):
108 binder(r ? create(o, T::current()) : create(o))
112 Bind(T *o, bool r = false):
113 binder(r ? create(o, T::current()) : create(o))
118 Bind &operator=(const Bind &);
121 ~Bind() { delete binder; }
125 Base *create(const T *o)
126 { return new Binder1<T>(o); }
128 template<typename T, typename U>
129 Base *create(const T *o, const U *l)
130 { return new Binder2<T, U>(o, l); }