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