signal.h
1 /*
2 ** UICore
3 ** Copyright (c) 1997-2015 The UICore Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** 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
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries UICore may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
29 #pragma once
30 
31 #include "bind_member.h"
32 #include <memory>
33 #include <functional>
34 #include <vector>
35 
36 namespace uicore
37 {
38  class SlotImpl;
39 
40  class Slot
41  {
42  public:
43  Slot() { }
44 
45  operator bool() const { return static_cast<bool>(impl); }
46 
47  template<typename T>
48  explicit Slot(T impl) : impl(impl) { }
49 
50  private:
51  std::shared_ptr<SlotImpl> impl;
52  };
53 
54  class SlotImpl
55  {
56  public:
57  SlotImpl() { }
58  SlotImpl(const SlotImpl &) = delete;
59  SlotImpl &operator=(const SlotImpl &) = delete;
60  virtual ~SlotImpl() { }
61  };
62 
63  template<typename SlotImplType>
64  class SignalImpl
65  {
66  public:
67  std::vector<std::weak_ptr<SlotImplType>> slots;
68  };
69 
70  template<typename FuncType>
71  class SlotImplT : public SlotImpl
72  {
73  public:
74  SlotImplT(const std::weak_ptr<SignalImpl<SlotImplT>> &signal, const std::function<FuncType> &callback) : signal(signal), callback(callback)
75  {
76  }
77 
79  {
80  std::shared_ptr<SignalImpl<SlotImplT>> sig = signal.lock();
81  if (sig)
82  {
83  for (auto it = sig->slots.begin(); it != sig->slots.end(); ++it)
84  {
85  // todo: investigate if "it->lock().get() == this" is required
86  if (it->expired() || it->lock().get() == this)
87  {
88  it = sig->slots.erase(it);
89  if (it == sig->slots.end())
90  break;
91  }
92  }
93  }
94  }
95 
96  std::weak_ptr<SignalImpl<SlotImplT>> signal;
97  std::function<FuncType> callback;
98  };
99 
100  template<typename FuncType>
101  class Signal
102  {
103  public:
104  Signal() : impl(std::make_shared<SignalImpl<SlotImplT<FuncType>>>()) { }
105 
106  template<typename... Args>
107  void operator()(Args&&... args)
108  {
109  std::vector<std::weak_ptr<SlotImplT<FuncType>>> slots = impl->slots;
110  for (std::weak_ptr<SlotImplT<FuncType>> &weak_slot : slots)
111  {
112  std::shared_ptr<SlotImplT<FuncType>> slot = weak_slot.lock();
113  if (slot)
114  {
115  slot->callback(std::forward<Args>(args)...);
116  }
117  }
118  }
119 
120  Slot connect(const std::function<FuncType> &func)
121  {
122  auto slot_impl = std::make_shared<SlotImplT<FuncType>>(impl, func);
123  impl->slots.push_back(slot_impl);
124  return Slot(slot_impl);
125  }
126 
127  template<typename InstanceType, typename MemberFuncType>
128  Slot connect(InstanceType instance, MemberFuncType func)
129  {
130  return connect(bind_member(instance, func));
131  }
132 
133  private:
134  std::shared_ptr<SignalImpl<SlotImplT<FuncType>>> impl;
135  };
136 
138  {
139  public:
140  template<typename FuncType, typename InstanceType, typename MemberFuncType>
141  void connect(Signal<FuncType> &signal, InstanceType instance, MemberFuncType func)
142  {
143  slots.push_back(signal.connect(instance, func));
144  }
145 
146  template<typename FuncType, typename CallbackType>
147  void connect(Signal<FuncType> &signal, CallbackType func)
148  {
149  slots.push_back(signal.connect(func));
150  }
151 
152  private:
153  std::vector<Slot> slots;
154  };
155 }
Definition: signal.h:40
std::weak_ptr< SignalImpl< SlotImplT > > signal
Definition: signal.h:96
Slot connect(InstanceType instance, MemberFuncType func)
Definition: signal.h:128
Slot connect(const std::function< FuncType > &func)
Definition: signal.h:120
SlotImplT(const std::weak_ptr< SignalImpl< SlotImplT >> &signal, const std::function< FuncType > &callback)
Definition: signal.h:74
SlotImpl()
Definition: signal.h:57
std::function< R(Args...)> bind_member(T *instance, R(T::*method)(Args...))
Definition: bind_member.h:37
STL namespace.
std::vector< std::weak_ptr< SlotImplType > > slots
Definition: signal.h:67
~SlotImplT()
Definition: signal.h:78
virtual ~SlotImpl()
Definition: signal.h:60
Definition: signal.h:64
Signal()
Definition: signal.h:104
void connect(Signal< FuncType > &signal, InstanceType instance, MemberFuncType func)
Definition: signal.h:141
Slot()
Definition: signal.h:43
void connect(Signal< FuncType > &signal, CallbackType func)
Definition: signal.h:147
std::function< FuncType > callback
Definition: signal.h:97
Definition: signal.h:101
Definition: signal.h:137
Definition: signal.h:54
void operator()(Args &&...args)
Definition: signal.h:107
Slot(T impl)
Definition: signal.h:48
Definition: Application/application.h:35
SlotImpl & operator=(const SlotImpl &)=delete
Definition: signal.h:71