Connection.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * As a special exception, you may use this file as part of a free
8  * software library without restriction. Specifically, if other files
9  * instantiate templates or use macros or inline functions from this
10  * file, or you compile this file and link it with other files to
11  * produce an executable, this file does not by itself cause the
12  * resulting executable to be covered by the GNU General Public
13  * License. This exception does not however invalidate any other
14  * reasons why the executable file might be covered by the GNU Library
15  * General Public License.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  */
26 #ifndef Pt_Connection_h
27 #define Pt_Connection_h
28 
29 #include <Pt/Api.h>
30 #include <Pt/Slot.h>
31 #include <Pt/NonCopyable.h>
32 
33 namespace Pt {
34 
35 class Connectable;
36 class Slot;
37 
39 class ConnectionData
40 {
41  public:
42  ConnectionData(Connectable& sender, Slot* slot)
43  : _refs(1)
44  , _valid(true)
45  , _slot(slot)
46  , _sender(&sender)
47  { }
48 
49  ~ConnectionData()
50  { delete _slot; }
51 
52  unsigned ref()
53  { return ++_refs; }
54 
55  unsigned unref()
56  { return --_refs; }
57 
58  unsigned refs() const
59  { return _refs; }
60 
61  bool isValid() const
62  { return _valid; }
63 
64  void setInvalid()
65  { _valid = false; }
66 
67  Connectable* sender()
68  { return _sender; }
69 
70  const Connectable* sender() const
71  { return _sender; }
72 
73  Slot* slot()
74  { return _slot; }
75 
76  const Slot* slot() const
77  { return _slot; }
78 
79  private:
80  unsigned _refs;
81  bool _valid;
82  Slot* _slot;
83  Connectable* _sender;
84 };
85 
90 class PT_API Connection
91 {
92  public:
94  Connection();
95 
97  Connection(Connectable& sender, Slot* slot);
98 
100  Connection(const Connection& connection);
101 
103  ~Connection();
104 
106  bool isValid() const
107  { return _data && _data->isValid(); }
108 
110  const Connectable* sender() const
111  { return _data ? _data->sender() : 0; }
112 
114  const Slot* slot() const
115  { return _data ? _data->slot() : 0; }
116 
118  bool operator!() const
119  { return this->isValid() == false; }
120 
122  void close();
123 
125  Connection& operator=(const Connection& connection);
126 
128  bool operator==(const Connection& connection) const
129  { return _data == connection._data; }
130 
131  private:
132  ConnectionData* _data;
133 };
134 
135 } // namespace Pt
136 
137 #endif
const Slot * slot() const
Returns the slot.
Definition: Connection.h:114
bool isValid() const
Returns true if not closed.
Definition: Connection.h:106
Connection Management for Signal and Slot Objects.
Definition: Connectable.h:49
const Connectable * sender() const
Returns the sender.
Definition: Connection.h:110
Endpoint of a signal/slot connection.
Definition: Slot.h:21
Represents a connection between a Signal/Delegate and a slot.
Definition: Connection.h:90
bool operator!() const
Returns true if closed.
Definition: Connection.h:118
ConstMethodSlot< R, C, ARGS > slot(C &obj, R(BaseT::*memFunc)(ARGS) const )
Returns a slot object for the given object/member pair.