YPC  0.2.0
singleton.h
1 #pragma once
2 #include <functional>
3 #include <memory>
4 #include <mutex>
5 
6 namespace ypc {
7 
8 template <typename T> class singleton {
9 public:
10  static T &instance() {
11  std::call_once(s_init_once, std::bind(singleton<T>::init));
12  if (!s_pInstance) {
13  throw std::runtime_error("already deallocated!");
14  }
15  return *s_pInstance;
16  }
17 
18  void release() {
19  std::call_once(s_dealloc_once, std::bind(singleton<T>::dealloc));
20  }
21 
22 protected:
23  singleton() = default;
24 
25 private:
26  static void init() { s_pInstance = std::shared_ptr<T>(new T()); }
27  static void dealloc() { s_pInstance.reset(); }
28 
29 protected:
30  static std::shared_ptr<T> s_pInstance;
31  static std::once_flag s_init_once;
32  static std::once_flag s_dealloc_once;
33 };
34 template <typename T> std::shared_ptr<T> singleton<T>::s_pInstance;
35 template <typename T> std::once_flag singleton<T>::s_init_once;
36 template <typename T> std::once_flag singleton<T>::s_dealloc_once;
37 
38 template <typename T> class singleton_guard {
39 public:
40  singleton_guard() = default;
41  singleton_guard(const singleton_guard &) = delete;
42  singleton_guard &operator=(const singleton_guard &) = delete;
43  singleton_guard(singleton_guard &&) = delete;
44  singleton_guard &operator=(singleton_guard &&) = delete;
45 
46  ~singleton_guard() { singleton<T>::instance().release(); }
47 }; // end class singleton_guard
48 } // namespace ypc
ypc::singleton_guard
Definition: singleton.h:38
ypc::singleton
Definition: singleton.h:8