1 // Observable Library 2 // Copyright (c) 2016 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: connection()18 connection() : m_signal(nullptr), 19 m_slot(nullptr) { 20 } 21 connection(signal_base * sig,slot_base * slot)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: scoped_connection()41 scoped_connection() { 42 } 43 scoped_connection(const connection & conn)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 ~scoped_connection()53 ~scoped_connection() { 54 m_conn.disconnect(); 55 } 56 57 private: 58 connection m_conn; 59 }; 60 61 } // namespace obs 62 63 #endif 64