File tree Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Original file line number Diff line number Diff line change 8
8
#include < algorithm>
9
9
#include < functional>
10
10
#include < list>
11
+ #include < memory>
11
12
12
13
namespace margelo {
13
14
14
- template <typename Callback> class ListenerManager {
15
+ template <typename Callback> class ListenerManager : public std ::enable_shared_from_this<ListenerManager<Callback>> {
15
16
private:
17
+ using TSelf = ListenerManager<Callback>;
16
18
std::list<Callback> _listeners;
17
19
20
+ private:
21
+ std::shared_ptr<ListenerManager<Callback>> shared () {
22
+ return this ->shared_from_this ();
23
+ }
24
+
18
25
public:
19
26
Listener add (Callback listener) {
20
27
_listeners.push_back (std::move (listener));
21
28
auto id = --_listeners.end ();
22
- return Listener ([id, this ]() { _listeners.erase (id); });
29
+ auto weakThis = std::weak_ptr<TSelf>(shared ());
30
+ return Listener ([id, weakThis]() {
31
+ auto sharedThis = weakThis.lock ();
32
+ if (sharedThis) {
33
+ sharedThis->_listeners .erase (id);
34
+ }
35
+ });
23
36
}
24
37
25
38
const std::list<Callback>& getListeners () {
You can’t perform that action at this time.
0 commit comments