Created
December 4, 2019 05:29
-
-
Save Jacob-Tate/ceab63053a41485ca443f87dc7fd8aff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* @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; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment