TGUI  0.8.7
SignalManager.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_HPP
27 #define TGUI_SIGNAL_MANAGER_HPP
28 
29 
30 #include <TGUI/Widget.hpp>
31 #include <memory>
32 
34 
35 namespace tgui
36 {
38 
39  class TGUI_API SignalManager
40  {
41  public:
42 
43  typedef std::shared_ptr<SignalManager> Ptr;
44  typedef std::shared_ptr<const SignalManager> ConstPtr;
45 
46  using Delegate = std::function<void()>;
47  using DelegateEx = std::function<void(std::shared_ptr<Widget>, const std::string&)>;
48 
49  typedef std::weak_ptr<Widget> Weak;
50  typedef unsigned int SignalID;
51 
52  struct SignalTuple
53  {
54  std::string m_widgetName;
55  std::string m_signalName;
56  std::pair<Delegate, DelegateEx> m_func;
57  };
58 
60  {
61  SignalID m_signalId;
62  Weak m_widget;
63  unsigned int m_signalWidgetID;
64  };
65 
69  SignalManager();
70 
74  virtual ~SignalManager();
75 
81  static void setDefaultSignalManager(const SignalManager::Ptr& manager);
82 
88  static SignalManager::Ptr getSignalManager();
89 
90 #if defined(__cpp_if_constexpr) && (__cpp_if_constexpr >= 201606L)
91  template <typename Func, typename... BoundArgs>
102  unsigned int connect(std::string widgetName, std::string signalName, Func&& handler, const BoundArgs&... args);
103 
104 #else
105  template <typename Func, typename... Args, typename std::enable_if<std::is_convertible<Func, std::function<void(const Args&...)>>::value>::type* = nullptr>
116  unsigned int connect(std::string widgetName, std::string signalName, Func&& handler, const Args&... args);
117 
118 
130  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
131  && std::is_convertible<Func, std::function<void(const BoundArgs&..., std::shared_ptr<Widget>, const std::string&)>>::value>::type* = nullptr>
132  unsigned int connect(std::string widgetName, std::string signalName, Func&& handler, BoundArgs&&... args);
133 #endif
134 
145  template <typename Func, typename... BoundArgs>
146  unsigned int connect(std::string widgetName, std::initializer_list<std::string> signalNames, Func&& handler, BoundArgs&&... args);
147 
148 
156  virtual bool disconnect(unsigned int id);
157 
158 
162  virtual void disconnectAll();
163 
164 
168  virtual void add(const Widget::Ptr& widgetPtr);
169 
170 
174  virtual void remove(Widget* widget);
175 
177  private:
181  unsigned int generateUniqueId();
182 
183  unsigned int m_lastId;
184 
185 
187  protected:
188 
192  std::pair<Delegate, DelegateEx> makeSignal(const Delegate&);
193 
194 
198  std::pair<Delegate, DelegateEx> makeSignal(const DelegateEx&);
199 
200 
208  virtual void connect(SignalID id);
209 
210 
212 
213  static SignalManager::Ptr m_manager;
214 
215  std::vector<Weak> m_widgets;
216  std::map<SignalID, SignalTuple> m_signals;
217  std::vector<ConnectedSignalTuple> m_connectedSignals;
218  };
219 
221 }
222 
224 
225 #endif // TGUI_SIGNAL_MANAGER_HPP
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
std::shared_ptr< SignalManager > Ptr
Shared widget pointer.
Definition: SignalManager.hpp:43
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: SignalManager.hpp:59
Definition: SignalManager.hpp:39
The parent class for every widget.
Definition: Widget.hpp:73
Definition: SignalManager.hpp:52
std::shared_ptr< const SignalManager > ConstPtr
Shared constant widget pointer.
Definition: SignalManager.hpp:44