#pragma once namespace gl { template class bindable { protected: thread_local static bindable* 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(active)); } static void unbind() { active = nullptr; T::bind(0); } }; template thread_local bindable* bindable::active; }