ImFusion SDK 4.3
InstanceManager< T > Class Template Reference

#include <ImFusion/Core/GL/InstanceManager.h>

Concrete CRTP class for context-aware caching of OpenGL object instances. More...

+ Inheritance diagram for InstanceManager< T >:

Detailed Description

template<typename T>
class ImFusion::GL::InstanceManager< T >

Concrete CRTP class for context-aware caching of OpenGL object instances.

Inherit from this class if you want your class to support instance caching. It provides the static function cachedInstance() that will manage a single global instance of this object for each OpenGL context. Instances are lazy-instantiated, which means they do not exist until the first time they are requested for a concrete context.

This powerful interface enables you to avoid constant reallocation of your class while making sure to avoid race conditions (given that there is a 1-to-1 mapping between OpenGL contexts and threads) or issues if an OpenGL object is not shareable between contexts (such as VAOs).

Requesting cached instances is thread-safe. However, the returned objects are not. A cached instance will remain valid until the OpenGL context in which it was requested is destroyed.

Example for providing a global per-context GL::Framebuffer instance:

// declare the GlobalFBO class inheriting from both GL::Framebuffer and GL::InstanceManager:
class GlobalFBO : public GL::Framebuffer, public GL::InstanceManager<GlobalFBO> {};
// usage at some other point:
// `fbo` behaves like any other GL::Framebuffer just that it's "magically" cached per-context
GL::Framebuffer& fbo = GlobalFBO::cachedInstance();
auto guard = fbo.guardedBind();
[...]
Wrapper class for OpenGL Framebuffer Objects (FBO).
Definition Framebuffer.h:60
StateGuard guardedBind()
Binds the FBO and returns a GlStateGuard to restore the FBO binding state before the call.
Concrete CRTP class for context-aware caching of OpenGL object instances.
Definition InstanceManager.h:72
Note
When using a cached instance of this object, be aware that the state of the instance may be any valid state and is not necessarily the same as when you last used it.

Static Public Member Functions

static T & cachedInstance ()
 Returns the cached instance of type T for the currently active OpenGL context.
 

Member Function Documentation

◆ cachedInstance()

template<typename T>
static T & cachedInstance ( )
inlinestatic

Returns the cached instance of type T for the currently active OpenGL context.

Note
This instance may be shared with many other callers. Thus, do not take any state for granted but always set the needed state yourself.

The documentation for this class was generated from the following file:
Search Tab / S to search, Esc to close