-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathConnection.h
200 lines (173 loc) · 8.28 KB
/
Connection.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#ifndef Corrade_Interconnect_Connection_h
#define Corrade_Interconnect_Connection_h
/*
This file is part of Corrade.
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025
Vladimír Vondruš <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
/** @file
* @brief Class @ref Corrade::Interconnect::Connection
*/
#include <cstddef>
#include "Corrade/Containers/Reference.h"
#include "Corrade/Interconnect/Interconnect.h"
#include "Corrade/Interconnect/visibility.h"
namespace Corrade { namespace Interconnect {
namespace Implementation {
struct SignalDataHash;
enum: std::size_t { FunctionPointerSize =
#ifndef CORRADE_TARGET_WINDOWS
2*sizeof(void*)/sizeof(std::size_t)
#else
/* On MSVC, pointers to members with a virtual base class
are the biggest and have 16 bytes both on 32bit and 64bit. */
16/sizeof(std::size_t)
#endif
};
class SignalData {
public:
/* Still broken even on MSVC 2022. Maybe 2025 will be the year when
MSVC can finally do plain C++11? */
#if !defined(CORRADE_TARGET_MSVC) || defined(CORRADE_TARGET_CLANG_CL) || _MSC_VER >= 1940
template<class Emitter, class ...Args> SignalData(typename Emitter::Signal(Emitter::*signal)(Args...)): data() {
typedef typename Emitter::Signal(Emitter::*BaseSignal)(Args...);
*reinterpret_cast<BaseSignal*>(data) = signal;
}
#else
/* MSVC is not able to detect template parameters, so I need to
shovel these in explicitly using "static constructor" */
template<class Emitter, class ...Args> static SignalData create(typename Emitter::Signal(Emitter::*signal)(Args...)) {
/* Member function pointers on Windows have different size
based on whether the class has no/single inheritance,
multiple inheritance or virtual inheritance. Casting to
(Emitter::*) which has no inheritance (like done for other
platforms) would thus lose information and cause problems
when classes with multiple or virtual inheritance are used,
so we create a new type, virtually inherited from emitter,
cast the pointer to that and then save its representation. */
SignalData d;
struct VirtuallyInheritedEmitter: virtual Emitter {};
typedef typename Emitter::Signal(VirtuallyInheritedEmitter::*VirtuallyDerivedSignal)(Args...);
*reinterpret_cast<VirtuallyDerivedSignal*>(d.data) = signal;
return d;
}
#endif
bool operator==(const SignalData& other) const {
for(std::size_t i = 0; i != FunctionPointerSize; ++i)
if(data[i] != other.data[i]) return false;
return true;
}
bool operator!=(const SignalData& other) const {
return !operator==(other);
}
private:
/* https://bugzilla.gnome.org/show_bug.cgi?id=776986 */
#ifndef DOXYGEN_GENERATING_OUTPUT
friend Interconnect::Emitter;
friend SignalDataHash;
#endif
/* Still broken even on MSVC 2022. Maybe 2025 will be the year when
MSVC can finally do plain C++11? */
#if defined(CORRADE_TARGET_MSVC) && !defined(CORRADE_TARGET_CLANG_CL) && _MSC_VER < 1940
SignalData(): data() {}
#endif
std::size_t data[FunctionPointerSize];
};
}
/**
@brief Connection
Returned by @ref Interconnect::connect(), allows to remove the connection
later using @ref Interconnect::disconnect(). Destruction of the @ref Connection
object does not remove the connection, after that the only possibility to
remove the connection is to disconnect the whole emitter or receiver or
disconnect everything connected to given signal using
@ref Emitter::disconnectSignal(), @ref Emitter::disconnectAllSignals() or
@ref Receiver::disconnectAllSlots(), or destroy either the emitter or receiver
object.
@see @ref interconnect, @ref Emitter, @ref Receiver
*/
class CORRADE_INTERCONNECT_EXPORT Connection {
public:
#ifdef CORRADE_BUILD_DEPRECATED
/**
* @brief Whether the connection exists
* @m_deprecated_since{2019,10} This function is dangerous as it has no
* way to check that the original @ref Emitter object still
* exists, use @ref Emitter::isConnected() instead.
*/
CORRADE_DEPRECATED("dangerous, use Emitter::isConnected() instead") bool isConnected() const;
/**
* @brief Remove the connection
* @m_deprecated_since{2019,10} This function is dangerous as it has no
* way to check that the original @ref Emitter object still
* exists, use @ref Interconnect::disconnect() instead.
*/
CORRADE_DEPRECATED("dangerous, use Interconnect::disconnect() instead") void disconnect();
/**
* @brief Whether connection is possible
* @m_deprecated_since{2019,10} Re-connecting a disconnected signal is
* not possible anymore in order to make the library more
* efficient. This function now just returns the value of (also
* deprecated) @ref isConnected().
*/
CORRADE_DEPRECATED("re-connecting a disconnected signal is not possible anymore") bool isConnectionPossible() const {
CORRADE_IGNORE_DEPRECATED_PUSH
return isConnected();
CORRADE_IGNORE_DEPRECATED_POP
}
/**
* @brief Re-establish the connection
* @m_deprecated_since{2019,10} Re-connecting a disconnected signal is
* not possible anymore in order to make the library more
* efficient. This function now just returns the value of (also
* deprecated) @ref isConnected().
*/
CORRADE_DEPRECATED("re-connecting a disconnected signal is not possible anymore") bool connect() {
CORRADE_IGNORE_DEPRECATED_PUSH
return isConnected();
CORRADE_IGNORE_DEPRECATED_POP
}
#endif
#ifdef DOXYGEN_GENERATING_OUTPUT
private:
#endif
explicit Connection(
#ifdef CORRADE_BUILD_DEPRECATED
Emitter& emitter,
#endif
Implementation::SignalData signal, Implementation::ConnectionData& data);
private:
/* https://bugzilla.gnome.org/show_bug.cgi?id=776986 */
#ifndef DOXYGEN_GENERATING_OUTPUT
friend Emitter;
friend Receiver;
/* Interestingly enough, unlike in Emitter.h, here MinGW GCC doesn't
warn about disconnect() being redeclared without a dllimport
attribute. */
friend CORRADE_INTERCONNECT_EXPORT bool disconnect(Emitter&, const Connection&);
#endif
#ifdef CORRADE_BUILD_DEPRECATED
Containers::Reference<Emitter> _emitter;
#endif
Implementation::SignalData _signal;
/* Note: this might become dangling at some point */
Implementation::ConnectionData* _data;
};
}}
#endif