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  , _callable(slot->callable())
48  { }
49 
50  ~ConnectionData()
51  { delete _slot; }
52 
53  unsigned ref()
54  { return ++_refs; }
55 
56  unsigned unref()
57  { return --_refs; }
58 
59  unsigned refs() const
60  { return _refs; }
61 
62  bool isValid() const
63  { return _valid; }
64 
65  void setInvalid()
66  { _valid = false; }
67 
68  Connectable* sender()
69  { return _sender; }
70 
71  const Connectable* sender() const
72  { return _sender; }
73 
74  Slot* slot()
75  { return _slot; }
76 
77  const Slot* slot() const
78  { return _slot; }
79 
80  const Callback* callable() const
81  { return _callable; }
82 
83  private:
84  unsigned _refs;
85  bool _valid;
86  Slot* _slot;
87  Connectable* _sender;
88  const Callback* _callable;
89 };
90 
95 class PT_API Connection
96 {
97  public:
100 
102  Connection(Connectable& sender, Slot* slot);
103 
105  Connection(const Connection& connection);
106 
109 
111  bool isValid() const
112  { return _data && _data->isValid(); }
113 
115  const Connectable* sender() const
116  { return _data ? _data->sender() : 0; }
117 
119  const Slot* slot() const
120  { return _data ? _data->slot() : 0; }
121 
124  const Callback* callable() const
125  { return _data ? _data->callable() : 0; }
126 
128  bool operator!() const
129  { return this->isValid() == false; }
130 
132  void close();
133 
135  Connection& operator=(const Connection& connection);
136 
138  bool operator==(const Connection& connection) const
139  { return _data == connection._data; }
140 
141  private:
142  ConnectionData* _data;
143 };
144 
145 } // namespace Pt
146 
147 #endif
const Connectable * sender() const
Returns the sender.
Definition: Connection.h:115
Core module.
Definition: Allocator.h:33
Connection(const Connection &connection)
Copy Constructor.
Connection()
Default Constructor.
ConstMethodSlot< R, ClassT, As... > slot(ClassT &object, R(BaseT::*method)(As...) const)
Returns a slot object for the given object/member pair.
Definition: ConstMethod.h:172
bool isValid() const
Returns true if not closed.
Definition: Connection.h:111
Connection Management for Signal and Slot Objects.
Definition: Connectable.h:50
const Slot * slot() const
Returns the slot.
Definition: Connection.h:119
Connection & operator=(const Connection &connection)
Assignment operator.
const Callback * callable() const
Returns the slot's Callback, cached at connection time.
Definition: Connection.h:124
Endpoint of a signal/slot connection.
Definition: Slot.h:55
void close()
Closes the connection.
~Connection()
Destructor.
Connection(Connectable &sender, Slot *slot)
Construct with sender and slot.
Represents a connection between a Signal/Delegate and a slot.
Definition: Connection.h:96
bool operator!() const
Returns true if closed.
Definition: Connection.h:128