Thrill  0.1
singleton.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * foxxll/singleton.hpp
3  *
4  * Part of FOXXLL. See http://foxxll.org
5  *
6  * Copyright (C) 2008, 2011 Andreas Beckmann <[email protected]>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #ifndef FOXXLL_SINGLETON_HEADER
14 #define FOXXLL_SINGLETON_HEADER
15 
16 #include <cstdlib>
17 #include <mutex>
18 
20 #include <foxxll/common/types.hpp>
21 
22 namespace foxxll {
23 
24 template <typename InstanceType, bool destroy_on_exit = true>
25 class singleton
26 {
27 public:
28  using instance_type = InstanceType;
31 
32 public:
33  singleton() = default;
34 
35  //! non-copyable: delete copy-constructor
36  singleton(const singleton&) = delete;
37  //! non-copyable: delete assignment operator
38  singleton& operator = (const singleton&) = delete;
39 
40  //! return instance or create base instance if empty
42  {
43  if (!instance_)
44  return create_instance<InstanceType>();
45 
46  return instance_;
47  }
48 
50  {
51  if (!instance_)
52  create_instance<InstanceType>();
53 
54  return *instance_;
55  }
56 
57  //! create instance of SubInstanceType and move into singleton
58  template <typename SubInstanceType>
60 
61  //! destroy singleton and mark as invalid
62  static void destroy_instance();
63 
64 private:
65  //! singleton object instance
67 
68  //! mutex to protect instance_
69  static std::mutex singleton_mutex_;
70 };
71 
72 template <typename InstanceType, bool destroy_on_exit>
73 template <typename SubInstanceType>
76 {
77  std::unique_lock<std::mutex> instance_lock(singleton_mutex_);
78  if (!instance_) {
79  instance_ = new SubInstanceType();
80  if (destroy_on_exit)
82  }
83  return instance_;
84 }
85 
86 template <typename InstanceType, bool destroy_on_exit>
88 {
89  std::unique_lock<std::mutex> instance_lock(singleton_mutex_);
90  instance_pointer old_instance = instance_;
91  instance_ = reinterpret_cast<instance_pointer>(size_t(-1)); // bomb if used again
92  delete old_instance;
93 }
94 
95 template <typename InstanceType, bool destroy_on_exit>
98 
99 template <typename InstanceType, bool destroy_on_exit>
101 
102 } // namespace foxxll
103 
104 #endif // !FOXXLL_SINGLETON_HEADER
105 
106 /**************************************************************************/
static std::mutex singleton_mutex_
mutex to protect instance_
Definition: singleton.hpp:69
singleton()=default
static void destroy_instance()
destroy singleton and mark as invalid
Definition: singleton.hpp:87
volatile instance_pointer volatile_instance_pointer
Definition: singleton.hpp:30
instance_type * instance_pointer
Definition: singleton.hpp:29
FOXXLL library namespace
static instance_pointer get_instance()
return instance or create base instance if empty
Definition: singleton.hpp:41
singleton & operator=(const singleton &)=delete
non-copyable: delete assignment operator
static instance_type & get_ref()
Definition: singleton.hpp:49
int register_exit_handler(void(*function)(void))
Definition: exithandler.cpp:35
static volatile_instance_pointer instance_
singleton object instance
Definition: singleton.hpp:66
static instance_pointer create_instance()
create instance of SubInstanceType and move into singleton
Definition: singleton.hpp:75