1 /* 2 * Copyright 2002, The libsigc++ Development Team 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2.1 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this library; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 * 18 */ 19 #ifndef _SIGC_CONNECTION_HPP_ 20 #define _SIGC_CONNECTION_HPP_ 21 #include <sigc++config.h> 22 #include <sigc++/signal.h> 23 24 namespace sigc { 25 26 /** Convinience class for safe disconnection. 27 * Iterators must not be used beyond the lifetime of the list 28 * they work on. A connection object can be created from a 29 * slot list iterator and may safely be used to disconnect 30 * the referred slot at any time (disconnect()). If the slot 31 * has already been destroyed, disconnect() does nothing. empty() or 32 * operator bool() can be used to test whether the connection is 33 * still active. The connection can be blocked (block(), unblock()). 34 * 35 * This is possible because the connection object gets notified 36 * when the referred slot dies (notify()). 37 * 38 * @ingroup signal 39 */ 40 struct SIGC_API connection 41 { 42 /** Constructs an empty connection object. */ 43 connection(); 44 45 /** Constructs a connection object copying an existing one. 46 * @param c The connection object to make a copy from. 47 */ 48 connection(const connection& c); 49 50 /** Constructs a connection object from a slot list iterator. 51 * @param it The slot list iterator to take the slot from. 52 */ 53 template <typename T_slot> connectionconnection54 connection(const slot_iterator<T_slot>& it) : slot_(&(*it)) 55 { if (slot_) slot_->add_destroy_notify_callback(this, ¬ify); } 56 57 /** Constructs a connection object from a slot object. 58 * This is only useful if you create your own slot list. 59 * @param sl The slot to operate on. 60 */ 61 explicit connection(slot_base& sl); 62 63 /** Overrides this connection object copying another one. 64 * @param c The connection object to make a copy from. 65 */ 66 connection& operator=(const connection& c); 67 68 /** Overrides this connection object with another slot list iterator. 69 * @param it The new slot list iterator to take the slot from. 70 */ 71 template <typename T_slot> 72 connection& operator=(const slot_iterator<T_slot>& it) 73 { set_slot(&(*it)); return *this; } 74 75 ~connection(); 76 77 /** Returns whether the connection is still active. 78 * @return @p false if the connection is still active. 79 */ 80 bool empty() const; 81 82 /** Returns whether the connection is still active. 83 * @return @p true if the connection is still active. 84 */ 85 bool connected() const; 86 87 /** Returns whether the connection is blocked. 88 * @return @p true if the connection is blocked. 89 */ 90 bool blocked() const; 91 92 /** Sets or unsets the blocking state of this connection. 93 * See slot_base::block() for details. 94 * @param should_block Indicates whether the blocking state should be set or unset. 95 * @return @p true if the connection has been in blocking state before. 96 */ 97 bool block(bool should_block = true); 98 99 /** Unsets the blocking state of this connection. 100 * @return @p true if the connection has been in blocking state before. 101 */ 102 bool unblock(); 103 104 /// Disconnects the referred slot. 105 void disconnect(); 106 107 /** Returns whether the connection is still active. 108 * @return @p true if the connection is still active. 109 */ 110 operator bool(); 111 112 /** Callback that is executed when the referred slot is destroyed. 113 * @param d The connection object notified (@p this). 114 */ 115 static void* notify(void* data); 116 117 private: 118 void set_slot(slot_base* sl); 119 120 /* Referred slot. Set to zero from notify(). 121 * A value of zero indicates an "empty" connection. 122 */ 123 slot_base* slot_; 124 }; 125 126 } /* namespace sigc */ 127 128 129 #ifndef LIBSIGC_DISABLE_DEPRECATED 130 131 namespace SigC { 132 133 /** Convinience class for safe disconnection. 134 * Iterators must not be used beyond the lifetime of the list 135 * they work on. A connection object can be created from a 136 * slot list iterator and may safely be used to disconnect 137 * the referred slot at any time (disconnect()). If the slot 138 * has already been destroyed, disconnect() does nothing. empty() or 139 * operator bool() can be used to test whether the connection is 140 * still active. The connection can be blocked (block(), unblock()). 141 * 142 * This is possible because the connection object gets notified 143 * when the referred slot dies (notify()). 144 * 145 * @deprecated Use sigc::connection instead. 146 * @ingroup compat 147 */ 148 typedef ::sigc::connection Connection; 149 150 } 151 152 #endif /* LIBSIGC_DISABLE_DEPRECATED */ 153 154 #endif /* _SIGC_TRACKABLE_HPP_ */ 155