-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigslot.hpp
200 lines (167 loc) · 4.24 KB
/
sigslot.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#pragma once
#include <memory>
#include <functional>
#include <list>
#include <mutex>
#include <algorithm>
namespace utils
{
namespace detail
{
template <typename SmartPtr>
struct smart_ptr_traits
{
};
template <typename T>
struct smart_ptr_traits<std::shared_ptr<T>>
{
typedef std::weak_ptr<T> weak_type;
};
template <typename T>
struct smart_ptr_traits<std::weak_ptr<T>>
{
typedef std::shared_ptr<T> share_type;
};
struct shared_ptr_continer
{
virtual operator bool() = 0;
};
template <typename SharedPtrType>
class shared_ptr_continer_imp : public shared_ptr_continer
{
public:
shared_ptr_continer_imp(SharedPtrType const &shared)
: m_shared(shared)
{
}
virtual ~shared_ptr_continer_imp(){}
operator bool()
{
return (bool)m_shared;
}
private:
SharedPtrType m_shared;
};
struct weak_ptr_continer
{
virtual std::shared_ptr<shared_ptr_continer> lock() const = 0;
};
template <typename WeakPtrType>
class weak_ptr_continer_imp : public weak_ptr_continer
{
public:
weak_ptr_continer_imp(WeakPtrType const &weak) : m_weak(weak)
{
}
virtual ~weak_ptr_continer_imp(){}
virtual std::shared_ptr<shared_ptr_continer> lock() const
{
using SharedPtrType = typename smart_ptr_traits<WeakPtrType>::share_type;
return std::make_shared<shared_ptr_continer_imp<SharedPtrType>>(m_weak.lock());
}
private:
WeakPtrType m_weak;
};
template <typename... signature>
class slot;
template <typename R, typename... ArgTypes>
class slot<R(ArgTypes...)>
{
public:
using SlotFunc = std::function<R(ArgTypes...)>;
template <typename T>
slot(T const &func) : m_func(func)
{
}
slot(slot const &other)
{
m_weak = other.m_weak;
m_tracked = other.m_tracked;
m_func = other.m_func;
}
template <typename SharedPtrType>
slot &track(SharedPtrType const &sharedptr)
{
using WeakPtrType = typename smart_ptr_traits<SharedPtrType>::weak_type;
m_weak = std::make_shared<weak_ptr_continer_imp<WeakPtrType>>(WeakPtrType(sharedptr));
m_tracked = true;
return *this;
}
template <typename... _Args>
bool operator()(_Args &&... args)
{
if (!m_func)
{
return false;
}
if (m_tracked)
{
auto shared = m_weak->lock();
if (shared->operator bool())
{
m_func(std::forward<_Args>(args)...);
}
else
{
return false;
}
}
else
{
m_func(std::forward<_Args>(args)...);
}
return true;
}
private:
SlotFunc m_func;
std::shared_ptr<weak_ptr_continer> m_weak;
bool m_tracked = false;
};
} // namespace detail
template <typename... signature>
class sigslot;
template <typename R, typename... ArgTypes>
class sigslot<R(ArgTypes...)>
{
public:
using SlotType = detail::slot<R(ArgTypes...)>;
using SlotID = std::weak_ptr<SlotType>;
SlotID connect(SlotType const &slot)
{
std::lock_guard<std::mutex> guard(m_mutex);
auto const s = std::make_shared<SlotType>(slot);
m_slots.push_back(s);
return s;
}
void disconnect(SlotID const &s)
{
std::lock_guard<std::mutex> guard(m_mutex);
m_slots.remove_if([&s](std::shared_ptr<SlotType> const &slot) {
return slot == s.lock();
});
}
template <typename... _Args>
void operator()(_Args &&... args)
{
m_mutex.lock();
auto slots = m_slots;
m_mutex.unlock();
decltype(slots) remove_slots;
std::copy_if(slots.begin(), slots.end(), std::back_inserter(remove_slots), [&args...](std::shared_ptr<SlotType> const &s) {
return !(*s)(std::forward<_Args>(args)...);
});
if (remove_slots.empty())
{
return;
}
m_mutex.lock();
m_slots.remove_if([&remove_slots](std::shared_ptr<SlotType> const &slot) {
return std::find(remove_slots.begin(), remove_slots.end(), slot) != remove_slots.end();
});
m_mutex.unlock();
}
private:
std::list<std::shared_ptr<SlotType>> m_slots;
std::mutex m_mutex;
};
} // namespace utils