Gaia-ECS v0.9.3
A simple and powerful entity component system
Loading...
Searching...
No Matches
gaia::core::dyn_singleton< T > Class Template Referencefinal

Gaia-ECS is a header-only library which means we want to avoid using global static variables because they would get copied to each translation units. At the same time the goal is for users to not see any memory allocation used by the library. Therefore, the only solution is a static variable with local scope. More...

#include <dyn_singleton.h>

Public Member Functions

 dyn_singleton (dyn_singleton &&world)=delete
 
 dyn_singleton (const dyn_singleton &world)=delete
 
dyn_singletonoperator= (dyn_singleton &&)=delete
 
dyn_singletonoperator= (const dyn_singleton &)=delete
 

Static Public Member Functions

static T & get () noexcept
 

Detailed Description

template<typename T>
class gaia::core::dyn_singleton< T >

Gaia-ECS is a header-only library which means we want to avoid using global static variables because they would get copied to each translation units. At the same time the goal is for users to not see any memory allocation used by the library. Therefore, the only solution is a static variable with local scope.

Being a static variable with local scope which means the singleton is guaranteed to be younger than its caller. Because static variables are released in the reverse order in which they are created, if used with a static World it would mean we first release the singleton and only then proceed with the world itself. As a result, in its destructor the world could access memory that has already been released.

Instead, we let the singleton allocate the object on the heap and once singleton's destructor is called we tell the internal object it should destroy itself. This way there are no memory leaks or access-after-freed issues on app exit reported.


The documentation for this class was generated from the following file: