NoesisGUI

Singletons in Noesis

Static data is not allowed in Noesis Engine as a general rule. In the past, statics have been a source of problems (construction time, destruction time, interdependencies, etc) so we have decided to avoid them. Another problem with statics is memory leaks: the destruction of the static data occurs after Kernel shutdown so those objects will generate leaks.

Usually, singletons are implemented using static classes. But this is not allowed in Noesis. To create singletons you have to use a Kernel System. Kernel Systems have an explicit time creation, explicit order creation and explicit time destruction.

There are some situations where implementing a singleton as a static class is the only way than can be followed (for example the Kernel itself is a singleton). In those rare cases, the Static-Class Singleton Pattern should be followed.

Static-Class Singleton Pattern

This is the general template that must be followed when implementing a singleton with a static class. The Kernel is an example of a class following this pattern.

class Singleton
{
public:
    /// Singleton accessor
    static Kernel* GetInstance()
    {
        static Singleton singleton;
        return &singleton;
    }

private:
    /// This is a singleton
    //@{
    Singleton();
    Singleton(const Singleton&);
    ~Singleton();
    Singleton& operator=(const Singleton&);
    //@}
};

The copy constructor and assignment operator don't need to be implemented because you will never execute them.

© 2017 Noesis Technologies