25#ifndef TGUI_SIGNAL_MANAGER_HPP
26#define TGUI_SIGNAL_MANAGER_HPP
28#include <TGUI/Widget.hpp>
30#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
31 #include <unordered_map>
37TGUI_MODULE_EXPORT
namespace tgui
45 using Ptr = std::shared_ptr<SignalManager>;
46 using ConstPtr = std::shared_ptr<const SignalManager>;
48 using Delegate = std::function<void()>;
49 using DelegateEx = std::function<void(std::shared_ptr<Widget>,
const String&)>;
51 using Weak = std::weak_ptr<Widget>;
52 using SignalID =
unsigned int;
58 std::pair<Delegate, DelegateEx> func;
65 unsigned int signalWidgetID;
69 widget(std::move(widgetPtr)),
70 signalWidgetID(widgetId)
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);
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);
138 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<!std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value
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);
165 virtual void remove(
Widget* widget);
172 unsigned int generateUniqueId();
174 unsigned int m_lastId = 0;
182 TGUI_NODISCARD std::pair<Delegate, DelegateEx> makeSignal(
const Delegate&);
187 TGUI_NODISCARD std::pair<Delegate, DelegateEx> makeSignalEx(
const DelegateEx&);
202 std::vector<Weak> m_widgets;
203 std::unordered_map<SignalID, SignalTuple> m_signals;
204 std::vector<ConnectedSignalTuple> m_connectedSignals;
209#if defined(__cpp_if_constexpr) && (__cpp_if_constexpr >= 201606L)
210 template <
typename Func,
typename... BoundArgs>
213 const unsigned int id = generateUniqueId();
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>)
220 m_signals[id] = {widgetName, signalName, makeSignal([=, f=std::function<
void(
const BoundArgs&...)>(handler)]{ std::invoke(f, args...); })};
222 else if constexpr (std::is_convertible_v<Func, std::function<void(
const BoundArgs&...)>>)
225 m_signals[id] = {widgetName, signalName, makeSignal([=]{ std::invoke(handler, args...); })};
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>)
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); })};
237 m_signals[id] = {widgetName, signalName, makeSignalEx([=](
const std::shared_ptr<Widget>& w,
const String& s){ std::invoke(handler, args..., w, s); })};
244 template <
typename Func,
typename... Args,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const Args&...)>>::value>*>
247 const unsigned int id = generateUniqueId();
248 m_signals[id] = {widgetName, signalName, makeSignal([f=std::function<
void(
const Args&...)>(handler),args...](){ f(args...); })};
254 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<!std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value
255 && std::is_convertible<Func, std::function<void(
const BoundArgs&..., std::shared_ptr<Widget>,
const String&)>>::value>*>
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); }
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
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
Definition SignalManager.hpp:62
Definition SignalManager.hpp:55