connection.h
Go to the documentation of this file.
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 
41  class ConnectionData : public RefCounted {
42  public:
44  : RefCounted(1)
45  , _valid(false)
46  , _slot(0)
47  , _sender(0)
48  { }
49 
51  : RefCounted(1)
52  , _valid(true)
53  , _slot(slot)
54  , _sender(&sender)
55  { }
56 
58  { delete _slot; }
59 
60  bool valid() const
61  { return _valid; }
62 
63  void setValid(bool valid)
64  { _valid = valid; }
65 
67  { return *_sender; }
68 
69  const Connectable& sender() const
70  { return *_sender; }
71 
73  { return *_slot; }
74 
75  const Slot& slot() const
76  { return *_slot; }
77 
78  private:
79  bool _valid;
80  Slot* _slot;
81  Connectable* _sender;
82  };
83 
87  class Connection
88  {
90  : _data(data)
91  {
92  if (_data)
93  _data->addRef();
94  }
95 
96  public:
97  Connection();
98 
100 
102 
103  Connection& operator=(const Connection& connection);
104 
105  ~Connection();
106 
107  bool valid() const
108  { return _data && _data->valid(); }
109 
110  const Connectable& sender() const
111  { return _data->sender(); }
112 
113  const Slot& slot() const
114  { return _data->slot(); }
115 
116  bool operator!() const
117  { return !valid(); }
118 
119  void close();
120 
121  bool operator==(const Connection& connection) const
122  { return _data == connection._data; }
123 
124  private:
125  ConnectionData* _data;
126  };
127 
128 } // namespace cxxtools
129 
130 #endif