-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2010 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
#ifndef MSP_GL_BINDABLE_H_
#define MSP_GL_BINDABLE_H_
namespace Msp {
namespace GL {
+/**
+A helper class for single-point binding. Provides tracking of the currently
+bound object.
+*/
template<typename T>
class Bindable
{
const T *Bindable<T>::cur_obj;
+/**
+A helper class for Bindables that revert to a default object on unbind.
+*/
+template<typename T>
+class BindableWithDefault: protected Bindable<T>
+{
+protected:
+ BindableWithDefault() { }
+
+public:
+ static const T *current()
+ {
+ if(!Bindable<T>::cur_obj)
+ Bindable<T>::cur_obj = &default_object();
+ return Bindable<T>::cur_obj;
+ }
+
+ static void unbind()
+ {
+ default_object().bind();
+ }
+
+ static const T &default_object()
+ {
+ static T obj;
+ return obj;
+ }
+};
+
+
/**
RAII class for binding things. Binds the thing upon construction and unbinds
it upon destruction. If a null pointer is given, unbinds upon construction and
Base *binder;
public:
+ template<typename T>
+ Bind(const T &o, bool r = false):
+ binder(r ? create(&o, T::current()) : create(&o))
+ { }
+
template<typename T>
Bind(const T *o, bool r = false):
binder(r ? create(o, T::current()) : create(o))
{ }
+ template<typename T>
+ Bind(T *o, bool r = false):
+ binder(r ? create(o, T::current()) : create(o))
+ { }
+
private:
Bind(const Bind &);
Bind &operator=(const Bind &);