TGUI  0.8.7
SignalManagerImpl.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2019 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 
26 #ifndef TGUI_SIGNAL_MANAGER_IMPL_HPP
27 #define TGUI_SIGNAL_MANAGER_IMPL_HPP
28 
29 #include <TGUI/SignalImpl.hpp>
30 #include <TGUI/SignalManager.hpp>
31 
33 
34 namespace tgui
35 {
37 
38 #if defined(__cpp_if_constexpr) && (__cpp_if_constexpr >= 201606L)
39  template <typename Func, typename... BoundArgs>
40  unsigned int SignalManager::connect(std::string widgetName, std::string signalName, Func&& handler, const BoundArgs&... args)
41  {
42  const unsigned int id = generateUniqueId();
43 
44  if constexpr (std::is_convertible_v<Func, std::function<void(const BoundArgs&...)>>
45  && std::is_invocable_v<decltype(&handler), BoundArgs...>
46  && !std::is_function_v<Func>)
47  {
48  // Reference to function
49  m_signals[id] = {widgetName, signalName, makeSignal([=, f=std::function<void(const BoundArgs&...)>(handler)]{ std::invoke(f, args...); })};
50  }
51  else if constexpr (std::is_convertible_v<Func, std::function<void(const BoundArgs&...)>>)
52  {
53  // Function pointer
54  m_signals[id] = {widgetName, signalName, makeSignal([=]{ std::invoke(handler, args...); })};
55  }
56  else if constexpr (std::is_convertible_v<Func, std::function<void(const BoundArgs&..., const std::shared_ptr<Widget>&, const std::string&)>>
57  && std::is_invocable_v<decltype(&handler), BoundArgs..., const std::shared_ptr<Widget>&, const std::string&>
58  && !std::is_function_v<Func>)
59  {
60  // Reference to function with caller arguments
61  m_signals[id] = {widgetName, signalName, makeSignal([=, f=std::function<void(const BoundArgs&..., const std::shared_ptr<Widget>& w, const std::string& s)>(handler)](const std::shared_ptr<Widget>& w, const std::string& s){ std::invoke(f, args..., w, s); })};
62  }
63  else
64  {
65  // Function pointer with caller arguments
66  m_signals[id] = {widgetName, signalName, makeSignal([=](const std::shared_ptr<Widget>& w, const std::string& s){ std::invoke(handler, args..., w, s); })};
67  }
68 
69  connect(id);
70  return id;
71  }
72 
73 #else
74  template <typename Func, typename... Args, typename std::enable_if<std::is_convertible<Func, std::function<void(const Args&...)>>::value>::type*>
75  unsigned int SignalManager::connect(std::string widgetName, std::string signalName, Func&& handler, const Args&... args)
76  {
77  const unsigned int id = generateUniqueId();
78  m_signals[id] = {widgetName, signalName, makeSignal([f=std::function<void(const Args&...)>(handler),args...](){ f(args...); })};
79 
80  connect(id);
81  return id;
82  }
83 
84  template <typename Func, typename... BoundArgs, typename std::enable_if<!std::is_convertible<Func, std::function<void(const BoundArgs&...)>>::value // Ambigious otherwise when passing bind expression
85  && std::is_convertible<Func, std::function<void(const BoundArgs&..., std::shared_ptr<Widget>, const std::string&)>>::value>::type*>
86  unsigned int SignalManager::connect(std::string widgetName, std::string signalName, Func&& handler, BoundArgs&&... args)
87  {
88  const unsigned int id = generateUniqueId();
89  m_signals[id] = {widgetName, signalName, makeSignal(
90  [f=std::function<void(const BoundArgs&..., const std::shared_ptr<Widget>&, const std::string&)>(handler), args...]
91  (const std::shared_ptr<Widget>& w, const std::string& s)
92  { f(args..., w, s); }
93  )};
94 
95  connect(id);
96  return id;
97  }
98 #endif
99 
101 
102  template <typename Func, typename... BoundArgs>
103  unsigned int SignalManager::connect(std::string widgetName, std::initializer_list<std::string> signalNames, Func&& handler, BoundArgs&&... args)
104  {
105  unsigned int lastId = 0;
106  for (auto& signalName : signalNames)
107  lastId = connect(widgetName, signalName, handler, args...);
108 
109  return lastId;
110  }
111 }
112 
114 
115 #endif // TGUI_SIGNAL_MANAGER_IMPL_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
unsigned int connect(std::string widgetName, std::string signalName, Func &&handler, const Args &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition: SignalManagerImpl.hpp:75