easy-gl › ScopedBind
ScopedBind
A RAII helper that calls a bind function on construction and an unbind function on destruction. Useful for ensuring a resource is unbound when leaving a scope, even on exceptions.
ScopedBind is non-copyable and non-movable. It is templated on the bind and unbind callable types so there is no virtual dispatch overhead.
Usage with a Framebuffer
// Bind the FBO for the duration of this block, then unbind automatically:
{
easygl::ScopedBind fboBind(
[&]{ fbo.bind(); },
[]{ easygl::Framebuffer::unbind(); }
);
// Render to fbo here ...
device.draw_arrays(easygl::PrimitiveType::Triangles, 0, 3);
} // FBO is unbound here, even if an exception was thrown above
Usage with a VAO
{
easygl::ScopedBind vaoBind(
[&]{ vao.bind(); },
[&]{ vao.unbind(); }
);
// Modify vertex attribute state ...
vao.set_attribute_pointer(0, 3, easygl::DataType::Float, false, 0, nullptr);
vao.enable_attribute(0);
}
ScopedBind is non-copyable and non-movable. Construct it at the top of a scope and let it run to the closing brace.Constructor
| Parameter | Description |
|---|---|
bind_fn | Callable invoked immediately on construction. |
unbind_fn | Callable stored and invoked in the destructor. |