SPARK  0.1.0
A general purpose game engine written in C++.
Loading...
Searching...
No Matches
Slot.h
1#pragma once
2
3namespace spark::patterns
4{
5 template <typename... Args>
6 Slot<Args...>::Slot()
7 : m_callback() {}
8
9 template <typename... Args>
10 Slot<Args...>::Slot(const std::function<void(Args...)>& f)
11 : m_callback(f) {}
12
13 template <typename... Args>
14 Slot<Args...>::Slot(std::function<void(Args...)>&& f)
15 : m_callback(f) {}
16
17 template <typename... Args>
18 template <class T>
19 Slot<Args...>::Slot(T* target, void (T::*method)(Args...))
20 {
21 m_callback = [target, method](Args... args)
22 {
23 (target->*method)(args...);
24 };
25 }
26
27 template <typename... Args>
28 template <class T>
29 Slot<Args...>::Slot(T* target, void (T::*method)(Args...) const)
30 {
31 m_callback = [target, method](Args... args)
32 {
33 (target->*method)(args...);
34 };
35 }
36
37 template <typename... Args>
39 {
40 disconnect();
41 }
42
43 template <typename... Args>
45 : m_callback(slot.m_callback) {}
46
47 template <typename... Args>
48 Slot<Args...>::Slot(Slot&& slot) noexcept
49 {
50 move(&slot);
51 }
52
53 template <typename... Args>
54 Slot<Args...>& Slot<Args...>::operator=(const Slot& slot)
55 {
56 if (this == &slot)
57 return *this;
58
59 m_callback = slot.m_callback;
60 return *this;
61 }
62
63 template <typename... Args>
64 Slot<Args...>& Slot<Args...>::operator=(Slot&& slot) noexcept
65 {
66 disconnect();
67 move(&slot);
68 return *this;
69 }
70
71 template <typename... Args>
73 {
74 return m_connection != nullptr;
75 }
76
77 template <typename... Args>
79 {
80 if (isConnected())
81 m_connection->m_signal->disconnect(this);
82 }
83
84 template <typename... Args>
85 void Slot<Args...>::move(Slot* slot)
86 {
87 m_callback = std::move(slot->m_callback);
88 m_connection = nullptr;
89
90 if (slot->isConnected())
91 {
92 m_connection = slot->m_connection;
93 slot->m_connection->releaseSlot();
94 m_connection->m_slot = this;
95 m_connection->m_managed = false;
96 }
97 }
98}
A slot is a connection between a signal and a callback.
Definition Slot.h:18
bool isConnected() const
Checks if this slot is connected to a signal.
Definition Slot.h:72
void disconnect()
Disconnects this slot from the signal.
Definition Slot.h:78