17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-17 19:09:25 +02:00
Files
maszyna/gl/bindable.h

35 lines
729 B
C++

#pragma once
namespace gl
{
template <typename T>
class bindable
{
protected:
thread_local static bindable<T>* active;
public:
~bindable()
{
// deleting an object leaves its address free for the next one, which must not be taken for the bound one
if (active == this)
active = nullptr;
}
void bind()
{
if (active == this)
return;
active = this;
T::bind(*static_cast<T*>(active));
}
static void unbind()
{
active = nullptr;
T::bind(0);
}
};
template <typename T> thread_local bindable<T>* bindable<T>::active;
}