sandbox
Loading...
Searching...
No Matches
slot_state.hpp
1#ifndef LIBSBX_SIGNAL_SLOT_STATE_HPP_
2#define LIBSBX_SIGNAL_SLOT_STATE_HPP_
3
4#include <mutex>
5#include <atomic>
6#include <cinttypes>
7
8#include <libsbx/signals/lockable.hpp>
9
10namespace sbx::signals {
11
12using group_id = std::int32_t;
13
15
16 template<lockable, typename...>
17 friend class signal_base;
18
19public:
20
21 explicit slot_state(group_id group) noexcept
22 : _index{0},
23 _group{group},
24 _is_connected{true},
25 _is_blocked{false} { }
26
27 virtual ~slot_state() = default;
28
29 virtual auto is_connected() const noexcept -> bool {
30 return _is_connected;
31 }
32
33 auto disconnect() noexcept -> bool {
34 auto result = _is_connected.exchange(false);
35
36 if (result) {
37 do_disconnect();
38 }
39
40 return result;
41 }
42
43 auto is_blocked() const noexcept {
44 return _is_blocked.load();
45 }
46
47 auto block() noexcept -> void {
48 _is_blocked.store(true);
49 }
50
51 auto unblock() noexcept -> void {
52 _is_blocked.store(false);
53 }
54
55protected:
56
57 virtual auto do_disconnect() -> void { }
58
59 auto index() const -> std::size_t {
60 return _index;
61 }
62
63 auto set_index(std::size_t index) -> void {
64 _index = index;
65 }
66
67 auto group() const -> group_id {
68 return _group;
69 }
70
71private:
72
73 std::size_t _index;
74 group_id _group;
75 std::atomic<bool> _is_connected;
76 std::atomic<bool> _is_blocked;
77
78}; // class slot_state
79
80} // namespace sbx::signals
81
82#endif // LIBSBX_SIGNAL_SLOT_STATE_HPP_
Definition: signal.hpp:16
Definition: slot_state.hpp:14