TGUI  1.6.1
Loading...
Searching...
No Matches
SignalManager.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_SIGNAL_MANAGER_HPP
26#define TGUI_SIGNAL_MANAGER_HPP
27
28#include <TGUI/Widget.hpp>
29
30#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
31 #include <unordered_map>
32 #include <memory>
33#endif
34
36
37TGUI_MODULE_EXPORT namespace tgui
38{
40
41 class TGUI_API SignalManager
42 {
43 public:
44
45 using Ptr = std::shared_ptr<SignalManager>;
46 using ConstPtr = std::shared_ptr<const SignalManager>;
47
48 using Delegate = std::function<void()>;
49 using DelegateEx = std::function<void(std::shared_ptr<Widget>, const String&)>;
50
51 using Weak = std::weak_ptr<Widget>;
52 using SignalID = unsigned int;
53
55 {
56 String widgetName;
57 String signalName;
58 std::pair<Delegate, DelegateEx> func;
59 };
60
62 {
63 SignalID signalId;
64 Weak widget;
65 unsigned int signalWidgetID;
66
67 ConnectedSignalTuple(SignalID sigId, Weak widgetPtr, unsigned int widgetId) :
68 signalId(sigId),
69 widget(std::move(widgetPtr)),
70 signalWidgetID(widgetId)
71 {
72 }
73 };
74
78 SignalManager() = default;
79
83 virtual ~SignalManager() = default;
84
90 static void setDefaultSignalManager(const SignalManager::Ptr& manager);
91
97 TGUI_NODISCARD static SignalManager::Ptr getSignalManager();
98
99#if defined(__cpp_if_constexpr) && (__cpp_if_constexpr >= 201606L)
110 template <typename Func, typename... BoundArgs>
111 unsigned int connect(String widgetName, String signalName, Func&& handler, const BoundArgs&... args);
112
113#else
124 template <typename Func, typename... Args, typename std::enable_if_t<std::is_convertible<Func, std::function<void(const Args&...)>>::value>* = nullptr>
125 unsigned int connect(String widgetName, String signalName, Func&& handler, const Args&... args);
126
138 template <typename Func, typename... BoundArgs, typename std::enable_if_t<!std::is_convertible<Func, std::function<void(const BoundArgs&...)>>::value // Ambigious otherwise when passing bind expression
139 && std::is_convertible<Func, std::function<void(const BoundArgs&..., std::shared_ptr<Widget>, const String&)>>::value>* = nullptr>
140 unsigned int connect(String widgetName, String signalName, Func&& handler, BoundArgs&&... args);
141#endif
142
150 virtual bool disconnect(unsigned int id);
151
155 virtual void disconnectAll();
156
160 virtual void add(const Widget::Ptr& widgetPtr);
161
165 virtual void remove(Widget* widget);
166
168 private:
172 unsigned int generateUniqueId();
173
174 unsigned int m_lastId = 0;
175
177 protected:
178
182 TGUI_NODISCARD std::pair<Delegate, DelegateEx> makeSignal(const Delegate&);
183
187 TGUI_NODISCARD std::pair<Delegate, DelegateEx> makeSignalEx(const DelegateEx&);
188
196 virtual void connect(SignalID id);
197
199
200 static SignalManager::Ptr m_manager;
201
202 std::vector<Weak> m_widgets;
203 std::unordered_map<SignalID, SignalTuple> m_signals;
204 std::vector<ConnectedSignalTuple> m_connectedSignals;
205 };
206
208
209#if defined(__cpp_if_constexpr) && (__cpp_if_constexpr >= 201606L)
210 template <typename Func, typename... BoundArgs>
211 unsigned int SignalManager::connect(String widgetName, String signalName, Func&& handler, const BoundArgs&... args)
212 {
213 const unsigned int id = generateUniqueId();
214
215 if constexpr (std::is_convertible_v<Func, std::function<void(const BoundArgs&...)>>
216 && std::is_invocable_v<decltype(&handler), BoundArgs...>
217 && !std::is_function_v<Func>)
218 {
219 // Reference to function
220 m_signals[id] = {widgetName, signalName, makeSignal([=, f=std::function<void(const BoundArgs&...)>(handler)]{ std::invoke(f, args...); })};
221 }
222 else if constexpr (std::is_convertible_v<Func, std::function<void(const BoundArgs&...)>>)
223 {
224 // Function pointer
225 m_signals[id] = {widgetName, signalName, makeSignal([=]{ std::invoke(handler, args...); })};
226 }
227 else if constexpr (std::is_convertible_v<Func, std::function<void(const BoundArgs&..., const std::shared_ptr<Widget>&, const String&)>>
228 && std::is_invocable_v<decltype(&handler), BoundArgs..., const std::shared_ptr<Widget>&, const String&>
229 && !std::is_function_v<Func>)
230 {
231 // Reference to function with caller arguments
232 m_signals[id] = {widgetName, signalName, makeSignalEx([=, f=std::function<void(const BoundArgs&..., const std::shared_ptr<Widget>& w, const String& s)>(handler)](const std::shared_ptr<Widget>& w, const String& s){ std::invoke(f, args..., w, s); })};
233 }
234 else
235 {
236 // Function pointer with caller arguments
237 m_signals[id] = {widgetName, signalName, makeSignalEx([=](const std::shared_ptr<Widget>& w, const String& s){ std::invoke(handler, args..., w, s); })};
238 }
239
240 connect(id);
241 return id;
242 }
243#else
244 template <typename Func, typename... Args, typename std::enable_if_t<std::is_convertible<Func, std::function<void(const Args&...)>>::value>*>
245 unsigned int SignalManager::connect(String widgetName, String signalName, Func&& handler, const Args&... args)
246 {
247 const unsigned int id = generateUniqueId();
248 m_signals[id] = {widgetName, signalName, makeSignal([f=std::function<void(const Args&...)>(handler),args...](){ f(args...); })};
249
250 connect(id);
251 return id;
252 }
253
254 template <typename Func, typename... BoundArgs, typename std::enable_if_t<!std::is_convertible<Func, std::function<void(const BoundArgs&...)>>::value // Ambigious otherwise when passing bind expression
255 && std::is_convertible<Func, std::function<void(const BoundArgs&..., std::shared_ptr<Widget>, const String&)>>::value>*>
256 unsigned int SignalManager::connect(String widgetName, String signalName, Func&& handler, BoundArgs&&... args)
257 {
258 const unsigned int id = generateUniqueId();
259 m_signals[id] = {widgetName, signalName, makeSignalEx(
260 [f=std::function<void(const BoundArgs&..., const std::shared_ptr<Widget>&, const String&)>(handler), args...]
261 (const std::shared_ptr<Widget>& w, const String& s)
262 { f(args..., w, s); }
263 )};
264
265 connect(id);
266 return id;
267 }
268#endif
269
271}
272
274
275#endif // TGUI_SIGNAL_MANAGER_HPP
Definition SignalManager.hpp:42
virtual void connect(SignalID id)
Function that is called each time new signal is created with it id.
virtual bool disconnect(unsigned int id)
Disconnect a signal handler.
std::shared_ptr< const SignalManager > ConstPtr
Shared constant widget pointer.
Definition SignalManager.hpp:46
virtual void disconnectAll()
Disconnect all signal handler.
unsigned int connect(String widgetName, String signalName, Func &&handler, const Args &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition SignalManager.hpp:245
SignalManager()=default
Default constructor.
virtual ~SignalManager()=default
Destructor.
static SignalManager::Ptr getSignalManager()
Retrieves Signal Manager that will be used to operate a signal.
static void setDefaultSignalManager(const SignalManager::Ptr &manager)
Sets Signal Manager that will be used to operate a signal.
std::shared_ptr< SignalManager > Ptr
Shared widget pointer.
Definition SignalManager.hpp:45
Wrapper class to store strings.
Definition String.hpp:96
The parent class for every widget.
Definition Widget.hpp:83
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:86
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
Definition SignalManager.hpp:62
Definition SignalManager.hpp:55