1 | // Observable Library |
2 | // Copyright (c) 2016-2021 David Capello |
3 | // |
4 | // This file is released under the terms of the MIT license. |
5 | // Read LICENSE.txt for more information. |
6 | |
7 | #ifndef OBS_CONNETION_H_INCLUDED |
8 | #define OBS_CONNETION_H_INCLUDED |
9 | #pragma once |
10 | |
11 | namespace obs { |
12 | |
13 | class signal_base; |
14 | class slot_base; |
15 | |
16 | class connection { |
17 | public: |
18 | connection() : m_signal(nullptr), |
19 | m_slot(nullptr) { |
20 | } |
21 | |
22 | connection(signal_base* sig, |
23 | slot_base* slot) : |
24 | m_signal(sig), |
25 | m_slot(slot) { |
26 | } |
27 | |
28 | void disconnect(); |
29 | |
30 | operator bool() { |
31 | return (m_slot != nullptr); |
32 | } |
33 | |
34 | private: |
35 | signal_base* m_signal; |
36 | slot_base* m_slot; |
37 | }; |
38 | |
39 | class scoped_connection { |
40 | public: |
41 | scoped_connection() { |
42 | } |
43 | |
44 | scoped_connection(const connection& conn) : m_conn(conn) { |
45 | } |
46 | |
47 | scoped_connection& operator=(const connection& conn) { |
48 | m_conn.disconnect(); |
49 | m_conn = conn; |
50 | return *this; |
51 | } |
52 | |
53 | ~scoped_connection() { |
54 | m_conn.disconnect(); |
55 | } |
56 | |
57 | // Just in case that we want to disconnect the signal in the middle |
58 | // of the scope. |
59 | void disconnect() { |
60 | m_conn.disconnect(); |
61 | } |
62 | |
63 | private: |
64 | connection m_conn; |
65 | }; |
66 | |
67 | } // namespace obs |
68 | |
69 | #endif |
70 | |