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
12namespace cutehmi {
13
28template <class C>
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
74
78 virtual ~Singleton();
79
87};
88
100CUTEHMI_API void destroySingletonInstances();
101
102
103template <class C>
105{
107}
108
109template <class C>
111{
113}
114
115template <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
128template <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
140template <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: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
153//(c)C: This file is a part of CuteHMI.
154//(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.
155//(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.
156//(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/>.
157//(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
158//(c)C: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
159//(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
160//(c)C: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Non-copyable object.
Definition: NonCopyable.hpp:10
Non-movable object.
Definition: NonMovable.hpp:10
Singleton template.
Definition: Singleton.hpp:32
virtual ~Singleton()
Destructor.
Definition: Singleton.hpp:110
static std::unique_ptr< C > & InstancePtr()
Get instance pointer.
Definition: Singleton.hpp:141
static void Destroy()
Destroy instance.
Definition: Singleton.hpp:129
Singleton()
Default constructor.
Definition: Singleton.hpp:104
static C & Instance()
Get instance.
Definition: Singleton.hpp:116
CUTEHMI_API void removeSingletonDestroyCallback(singletonDestroyCallback callback)
Definition: singleton.cpp:80
CUTEHMI_API void storeSingletonDestroyCallback(singletonDestroyCallback callback)
Definition: singleton.cpp:70
Definition: constants.hpp:6
CUTEHMI_API void destroySingletonInstances()
Destroy singleton instances.
Definition: Singleton.cpp:5