CuteHMI - CuteHMI (CuteHMI.2)
Singleton.hpp
1 #ifndef H_EXTENSIONS_CUTEHMI_2_INCLUDE_CUTEHMI_SINGLETON_HPP
2 #define H_EXTENSIONS_CUTEHMI_2_INCLUDE_CUTEHMI_SINGLETON_HPP
3 
4 #include "NonCopyable.hpp"
5 #include "NonMovable.hpp"
6 #include "internal/common.hpp"
7 #include "internal/singleton.hpp"
8 
9 #include <functional>
10 #include <memory>
11 
12 namespace cutehmi {
13 
24 template <class C>
25 class Singleton:
26  public NonCopyable,
27  public NonMovable
28 {
29  public:
43  static C & Instance();
44 
60  static void Destroy();
61 
62  protected:
63  // shield the constructor and destructor to prevent outside sources
64  // from creating or destroying a Singleton instance.
65 
69  Singleton();
70 
74  virtual ~Singleton();
75 
83 };
84 
96 CUTEHMI_API void destroySingletonInstances();
97 
98 
99 template <class C>
101 {
103 }
104 
105 template <class C>
107 {
109 }
110 
111 template <class C>
113 {
114  //<CuteHMI-2.workaround target="std" cause="design">
115  // Function std::unique_ptr::reset() sets internal pointer to nullptr and only after that it will delete its contents. This
116  // causes error, when managed object still needs to be accessed through std::unique_ptr::get() function by members of managed
117  // object during their destruction. Workaround is to use additional raw pointer. In general this prevents std::unique_ptr from
118  // being used in inconsistent state (calling std::unique_ptr::get() while inside std::unique_ptr::reset()).
119  static C * instancePtr = InstancePtr().get();
120  return *instancePtr;
121  //</CuteHMI-2.workaround>
122 }
123 
124 template <class C>
126 {
127  //<CuteHMI-2.workaround>
128  // A bit paranoic, but if Instance() wasn't called before, it would point to nullptr. What if it is used by a dtor of one of the
129  // members of managed object? After InstancePtr().reset(), std::unique_ptr::reset() first sets internal pointer to nullptr, then
130  // calls destructor (hence Instance() would point to nullptr during destruction).
131  Instance();
132  //</CuteHMI-2.workaround>
133  InstancePtr().reset();
134 }
135 
136 template <class C>
138 {
139  static std::unique_ptr<C> instance(new C);
140  return instance;
141 }
142 
143 }
144 
145 #endif
146 
147 //(c)C: Copyright © 2018-2020, Michał Policht <michal@policht.pl>. All rights reserved.
148 //(c)C: This file is a part of CuteHMI.
149 //(c)C: CuteHMI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
150 //(c)C: CuteHMI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
151 //(c)C: You should have received a copy of the GNU Lesser General Public License along with CuteHMI. If not, see <https://www.gnu.org/licenses/>.
cutehmi::internal::removeSingletonDestroyCallback
CUTEHMI_PRIVATE void removeSingletonDestroyCallback(singletonDestroyCallback callback)
Definition: singleton.cpp:77
cutehmi::Singleton::~Singleton
virtual ~Singleton()
Destructor.
Definition: Singleton.hpp:106
cutehmi::destroySingletonInstances
CUTEHMI_API void destroySingletonInstances()
Destroy singleton instances.
Definition: Singleton.cpp:5
cutehmi::NonMovable
Non-movable object.
Definition: NonMovable.hpp:9
cutehmi
Definition: constants.hpp:6
cutehmi::internal::storeSingletonDestroyCallback
CUTEHMI_PRIVATE void storeSingletonDestroyCallback(singletonDestroyCallback callback)
Definition: singleton.cpp:67
cutehmi::NonCopyable
Non-copyable object.
Definition: NonCopyable.hpp:9
cutehmi::Singleton::Destroy
static void Destroy()
Destroy instance.
Definition: Singleton.hpp:125
cutehmi::Singleton::InstancePtr
static std::unique_ptr< C > & InstancePtr()
Get instance pointer.
Definition: Singleton.hpp:137
cutehmi::Singleton::Singleton
Singleton()
Default constructor.
Definition: Singleton.hpp:100
cutehmi::Singleton::Instance
static C & Instance()
Get instance.
Definition: Singleton.hpp:112
std::unique_ptr
cutehmi::Singleton
Singleton template.
Definition: Singleton.hpp:25