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 
28 template <class C>
29 class Singleton:
30  public NonCopyable,
31  public NonMovable
32 {
33  public:
47  static C & Instance();
48 
64  static void Destroy();
65 
66  protected:
67  // shield the constructor and destructor to prevent outside sources
68  // from creating or destroying a Singleton instance.
69 
73  Singleton();
74 
78  virtual ~Singleton();
79 
87 };
88 
100 CUTEHMI_API void destroySingletonInstances();
101 
102 
103 template <class C>
105 {
107 }
108 
109 template <class C>
111 {
113 }
114 
115 template <class C>
117 {
118  //<CuteHMI-2.workaround target="std" cause="design">
119  // Function std::unique_ptr::reset() sets internal pointer to nullptr and only after that it will delete its contents. This
120  // causes error, when managed object still needs to be accessed through std::unique_ptr::get() function by members of managed
121  // object during their destruction. Workaround is to use additional raw pointer. In general this prevents std::unique_ptr from
122  // being used in inconsistent state (calling std::unique_ptr::get() while inside std::unique_ptr::reset()).
123  static C * instancePtr = InstancePtr().get();
124  return *instancePtr;
125  //</CuteHMI-2.workaround>
126 }
127 
128 template <class C>
130 {
131  //<CuteHMI-2.workaround>
132  // 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
133  // members of managed object? After InstancePtr().reset(), std::unique_ptr::reset() first sets internal pointer to nullptr, then
134  // calls destructor (hence Instance() would point to nullptr during destruction).
135  Instance();
136  //</CuteHMI-2.workaround>
137  InstancePtr().reset();
138 }
139 
140 template <class C>
142 {
143  static std::unique_ptr<C> instance(new C);
144  return instance;
145 }
146 
147 }
148 
149 #endif
150 
151 //(c)C: Copyright © 2018-2020, Michał Policht <michal@policht.pl>. All rights reserved.
152 //(c)C: This file is a part of CuteHMI.
153 //(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.
154 //(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.
155 //(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:110
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:129
cutehmi::Singleton::InstancePtr
static std::unique_ptr< C > & InstancePtr()
Get instance pointer.
Definition: Singleton.hpp:141
cutehmi::Singleton::Singleton
Singleton()
Default constructor.
Definition: Singleton.hpp:104
cutehmi::Singleton::Instance
static C & Instance()
Get instance.
Definition: Singleton.hpp:116
std::unique_ptr
cutehmi::Singleton
Singleton template.
Definition: Singleton.hpp:29