1 /*
2  * Copyright (C) 2006-2008 by Marc Boris Duerner
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  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
27  */
28 
29 #ifndef cxxtools_Connection_h
30 #define cxxtools_Connection_h
31 
32 #include <cxxtools/slot.h>
33 #include <cxxtools/refcounted.h>
34 
35 namespace cxxtools {
36 
37     class Connectable;
38 
39     /** @internal
40     */
41     class ConnectionData : public RefCounted {
42         public:
ConnectionData()43             ConnectionData()
44             : _refs(1)
45             , _valid(false)
46             , _slot(0)
47             , _sender(0)
48             { }
49 
ConnectionData(Connectable & sender,Slot * slot)50             ConnectionData(Connectable& sender, Slot* slot)
51             : _refs(1)
52             , _valid(true)
53             , _slot(slot)
54             , _sender(&sender)
55             { }
56 
~ConnectionData()57             ~ConnectionData()
58             { delete _slot; }
59 
ref()60             unsigned ref()
61             { return ++_refs; }
62 
unref()63             unsigned unref()
64             { return --_refs; }
65 
refs()66             unsigned refs() const
67             { return _refs; }
68 
valid()69             bool valid() const
70             { return _valid; }
71 
setValid(bool valid)72             void setValid(bool valid)
73             { _valid = valid; }
74 
sender()75             Connectable& sender()
76             { return *_sender; }
77 
sender()78             const Connectable& sender() const
79             { return *_sender; }
80 
slot()81             Slot& slot()
82             { return *_slot; }
83 
slot()84             const Slot& slot() const
85             { return *_slot; }
86 
87         private:
88             unsigned _refs;
89             bool _valid;
90             Slot* _slot;
91             Connectable* _sender;
92     };
93 
94     /** @brief Represents a connection between a Signal/Delegate and a slot
95         @ingroup sigslot
96     */
97     class Connection
98     {
99         public:
100             Connection();
101 
102             Connection(Connectable& sender, Slot* slot);
103 
104             Connection(const Connection& connection);
105 
106             ~Connection();
107 
valid()108             bool valid() const
109             { return _data->valid(); }
110 
sender()111             const Connectable& sender() const
112             { return _data->sender(); }
113 
slot()114             const Slot& slot() const
115             { return _data->slot(); }
116 
117             bool operator!() const
118             { return this->valid() == false; }
119 
120             void close();
121 
122             Connection& operator=(const Connection& connection);
123 
124             bool operator==(const Connection& connection) const;
125 
126         private:
127             ConnectionData* _data;
128     };
129 
130 } // namespace cxxtools
131 
132 #endif
133