hmbdc
simplify-high-performance-messaging-programming
GuardedSingleton.hpp
1 #include "hmbdc/Copyright.hpp"
2 #pragma once
3 
4 #include "hmbdc/Exception.hpp"
5 #include <stdexcept>
6 #include <type_traits>
7 #include <typeinfo>
8 
9 namespace hmbdc { namespace pattern {
10 
11 template <typename Singleton>
13  template <typename...Args>
14  SingletonGuardian(Args&&...);
15  virtual ~SingletonGuardian();
16 };
17 
18 template<typename Singleton>
20  friend class SingletonGuardian<Singleton>;
21  static Singleton& instance() {return *pInstance_s;}
22  static bool initialized() {return pInstance_s;}
23 
24 private:
25  static Singleton* pInstance_s;
26 };
27 
28 template <typename Singleton> Singleton*
30 
31 template <typename Singleton>
32 template <typename...Args>
34 SingletonGuardian(Args&&...args) {
35  if (GuardedSingleton<Singleton>::pInstance_s) {
36  HMBDC_THROW(std::runtime_error
37  , "Cannot reinitialize typeid=" << typeid(Singleton).name());
38  }
39  GuardedSingleton<Singleton>::pInstance_s
40  = new Singleton(std::forward<Args>(args)...);
41 }
42 
43 template <typename Singleton>
46  delete GuardedSingleton<Singleton>::pInstance_s;
47  GuardedSingleton<Singleton>::pInstance_s = 0;
48 }
49 
50 }}
51 
52 
Definition: GuardedSingleton.hpp:19
Definition: GuardedSingleton.hpp:12
Definition: Client.hpp:11