jacob-tate
12/4/2019 - 5:29 AM

Singleton base class

/*! 
 *  @file      crtp_singleton.hpp
 *  @brief     Singleton base class
 *  @author    Jacob I. Tate
 *  @version   0.0.1
 *  @date      2019
 */

#pragma once

#include <utility>

template<typename T>
class singleton
{
public:
    /**
     * @brief Create an instance of the singleton object
     * 
     * @note This passes any variables to the ctor
     * @tparam Args The type of the variables to pass to the ctor
     * @param args The variables to pass to the ctor
     * @return T* We return a pointer rather than a reference to ensure ABI stability when systems are replaced
     */
    template<typename ... Args>
    static T* instance(Args&&... args)
    {
        static T instance { std::forward<Args>(args)... };

        return &instance;
    }

protected:
    singleton() = default;
    singleton(const singleton&) = delete;
    singleton& operator=(const singleton&) = delete;
    virtual ~singleton() = default;
};