1 #include "hmbdc/Copyright.hpp" 4 #include "hmbdc/Exception.hpp" 9 namespace hmbdc {
namespace pattern {
19 template <
typename Singleton>
21 template <
typename...Args>
35 template <
typename Singleton>
37 template <
typename...Args>
52 template<
typename Singleton>
56 static Singleton& instance() {
return *pInstance_s;}
57 static bool initialized() {
return pInstance_s;}
58 using element_type = Singleton;
64 static Singleton* pInstance_s;
67 template <
typename Singleton> Singleton*
70 template <
typename Singleton>
71 template <
typename...Args>
74 if (GuardedSingleton<Singleton>::pInstance_s) {
75 HMBDC_THROW(std::runtime_error
76 ,
"Cannot reinitialize typeid=" <<
typeid(Singleton).name());
78 GuardedSingleton<Singleton>::pInstance_s
79 =
new Singleton(std::forward<Args>(args)...);
82 template <
typename Singleton>
85 delete GuardedSingleton<Singleton>::pInstance_s;
86 GuardedSingleton<Singleton>::pInstance_s =
nullptr;
89 template <
typename Singleton>
92 if (GuardedSingleton<Singleton>::pInstance_s) {
93 HMBDC_THROW(std::runtime_error
94 ,
"Cannot reinitialize typeid=" <<
typeid(Singleton).name());
96 GuardedSingleton<Singleton>::pInstance_s
97 =
new (addr) Singleton;
100 template <
typename Singleton>
101 template <
typename...Args>
104 if (GuardedSingleton<Singleton>::pInstance_s) {
105 HMBDC_THROW(std::runtime_error
106 ,
"Cannot reinitialize typeid=" <<
typeid(Singleton).name());
108 GuardedSingleton<Singleton>::pInstance_s
109 =
new (addr) Singleton{std::forward<Args>(args)...};
112 template <
typename Singleton>
115 GuardedSingleton<Singleton>::pInstance_s->~Singleton();
117 GuardedSingleton<Singleton>::pInstance_s =
nullptr;
base for the Singleton that works with SingletonGuardian
Definition: GuardedSingleton.hpp:53
RAII representing the lifespan of the underlying Singleton which also ganrantees the singularity of u...
Definition: GuardedSingleton.hpp:20
similar to SingletonGuardian, but supports placement new of the underlying Singleton ...
Definition: GuardedSingleton.hpp:36