20#include <glib-object.h>
21#include <QtCore/QHash>
22#include <QtCore/QMutex>
23#include <boost/multi_index_container.hpp>
25#include <boost/multi_index/sequenced_index.hpp>
26#include <boost/multi_index/ordered_index.hpp>
28#include <boost/multi_index/member.hpp>
35static void c_marshaller(GClosure *closure, GValue *returnValue, uint paramValuesCount,
36 const GValue *paramValues,
void *hint,
void *data)
40 ClosureDataBase *cdata =
static_cast<ClosureDataBase*
>(closure->data);
45 for(uint i = cdata->passSender ? 0 : 1; i<paramValuesCount; ++i) {
46 params.append(Value(¶mValues[i]));
50 Value result(returnValue);
51 cdata->marshaller(result, params);
53 if (returnValue && G_IS_VALUE(returnValue)) {
54 g_value_copy(result, returnValue);
56 }
catch (
const std::exception & e) {
59 GSignalInvocationHint *ihint =
static_cast<GSignalInvocationHint*
>(hint);
62 g_signal_query(ihint->signal_id, &query);
63 signalName = QString::fromUtf8(query.signal_name);
65 if (ihint->detail != 0) {
66 Quark q(ihint->detail);
67 signalName.append(QLatin1String(
"::"));
68 signalName.append(q.toString());
72 QString instanceName = params.at(0).get<QString>();
78 dynamic_cast<const InvalidTypeException &
>(e);
80 msg = QLatin1String(
"One or more of the arguments of the signal are of different "
81 "type than the type that the closure expects");
84 dynamic_cast<const InvalidValueException &
>(e);
88 if (returnValue == NULL) {
89 msg = QLatin1String(
"The signal is defined to return void but the "
90 "closure returns something non-void");
92 msg = QLatin1String(
"One of the arguments of the signal was not a valid GValue. "
93 "This is most likely a bug in the code that invoked the signal.");
96 msg = QString::fromLatin1(e.what());
100 qCritical() <<
"Error during invocation of closure connected to signal"
101 << signalName <<
"from object" << instanceName <<
":" << msg;
105static void closureDestroyNotify(
void *data, GClosure *closure)
108 delete static_cast<ClosureDataBase*
>(closure->data);
111static inline GClosure *createCppClosure(ClosureDataBase *closureData)
113 GClosure *closure = g_closure_new_simple(
sizeof(GClosure), closureData);
114 g_closure_set_marshal(closure, &c_marshaller);
115 g_closure_add_finalize_notifier(closure, NULL, &closureDestroyNotify);
116 g_closure_ref(closure);
117 g_closure_sink(closure);
124Q_GLOBAL_STATIC(QWeakPointer<DestroyNotifierIface>, s_qobjDestroyNotifier)
125Q_GLOBAL_STATIC(QMutex, s_qobjDestroyNotifierMutex)
127DestroyNotifierIfacePtr QObjectDestroyNotifier::instance()
129 QMutexLocker l(s_qobjDestroyNotifierMutex());
131 DestroyNotifierIfacePtr ptr = s_qobjDestroyNotifier()->toStrongRef();
133 ptr = DestroyNotifierIfacePtr(
new QObjectDestroyNotifier);
134 *s_qobjDestroyNotifier() = ptr;
139bool QObjectDestroyNotifier::connect(
void *receiver, QObject *notificationReceiver,
const char *slot)
141 QObject *qreceiver =
reinterpret_cast<QObject*
>(receiver);
142 return QObject::connect(qreceiver, SIGNAL(destroyed(QObject*)),
143 notificationReceiver, slot, Qt::DirectConnection);
146bool QObjectDestroyNotifier::disconnect(
void* receiver, QObject *notificationReceiver)
148 QObject *qreceiver =
reinterpret_cast<QObject*
>(receiver);
149 return QObject::disconnect(qreceiver, 0, notificationReceiver, 0);
155class ConnectionsStore :
public QObject
159 inline ConnectionsStore() : QObject(), m_handlerIdInRemoval(0) {}
161 ulong connect(
void *instance, uint signal, Quark detail,
162 void *receiver,
const DestroyNotifierIfacePtr & notifier,
163 uint slotHash, ClosureDataBase *closureData, ConnectFlags flags);
165 bool disconnect(
void *instance, uint signal, Quark detail,
166 void *receiver, uint slotHash, ulong handlerId);
171 inline Connection(uint signal, Quark detail,
void *receiver,
172 uint slotHash, ulong handlerId)
188 bool lookupAndExec(
void *instance, uint signal, Quark detail,
void *receiver, uint slotHash,
189 ulong handlerId,
void (ConnectionsStore::*func)(void*, const Connection &));
191 void disconnectHandler(
void *instance,
const Connection & c);
192 void disconnectAndDestroyRcvrWatch(
void *instance,
const Connection & c);
194 void setupClosureWatch(
void *instance, ulong handlerId, GClosure *closure);
195 void onClosureDestroyedAction(
void *instance, ulong handlerId);
196 static void onClosureDestroyed(
void *data, GClosure *closure);
198 void setupReceiverWatch(
void *instance,
void *receiver,
const DestroyNotifierIfacePtr & notifier);
199 void destroyReceiverWatch(
void *instance,
const Connection & c);
202 void onReceiverDestroyed(
void *receiver);
203 void onReceiverDestroyed(QObject *receiver);
207 struct sequential {};
208 struct by_handlerId {};
210 struct by_receiver {};
212 typedef boost::multi_index_container<
214 boost::multi_index::indexed_by<
215 boost::multi_index::sequenced<
216 boost::multi_index::tag<sequential>
218 boost::multi_index::ordered_non_unique<
219 boost::multi_index::tag<by_signal>,
220 boost::multi_index::member<Connection, uint, &Connection::signal>
222 boost::multi_index::ordered_non_unique<
223 boost::multi_index::tag<by_receiver>,
224 boost::multi_index::member<Connection, void*, &Connection::receiver>
226 boost::multi_index::ordered_unique<
227 boost::multi_index::tag<by_handlerId>,
228 boost::multi_index::member<Connection, ulong, &Connection::handlerId>
231 > ConnectionsContainer;
233 typedef ConnectionsContainer::index<sequential>::type::iterator SequentialIterator;
234 typedef ConnectionsContainer::index<by_signal>::type::iterator BySignalIterator;
235 typedef ConnectionsContainer::index<by_receiver>::type::iterator ByReceiverIterator;
236 typedef ConnectionsContainer::index<by_handlerId>::type::iterator ByHandlerIterator;
237 typedef std::pair<BySignalIterator, BySignalIterator> BySignalIterators;
238 typedef std::pair<ByReceiverIterator, ByReceiverIterator> ByReceiverIterators;
242 DestroyNotifierIfacePtr notifier;
243 QHash<void*, int> senders;
247 QHash<void*, ConnectionsContainer> m_connections;
248 QHash<void*, ReceiverData> m_receivers;
250 QMutex m_handlerIdInRemovalMutex;
251 ulong m_handlerIdInRemoval;
254Q_GLOBAL_STATIC(ConnectionsStore, s_connectionsStore)
256ulong ConnectionsStore::connect(
void *instance, uint signal, Quark detail,
257 void *receiver,
const DestroyNotifierIfacePtr & notifier,
258 uint slotHash, ClosureDataBase *closureData, ConnectFlags flags)
260 QMutexLocker l(&m_mutex);
261 GClosure *closure = createCppClosure(closureData);
263 ulong handlerId = g_signal_connect_closure_by_id(instance, signal, detail, closure,
267 m_connections[instance].get<sequential>().push_back(
268 Connection(signal, detail, receiver, slotHash, handlerId)
271 setupClosureWatch(instance, handlerId, closure);
272 setupReceiverWatch(instance, receiver, notifier);
275 g_closure_unref(closure);
279bool ConnectionsStore::disconnect(
void *instance, uint signal, Quark detail,
280 void *receiver, uint slotHash, ulong handlerId)
282 QMutexLocker l(&m_mutex);
283 return lookupAndExec(instance, signal, detail, receiver, slotHash, handlerId,
284 &ConnectionsStore::disconnectAndDestroyRcvrWatch);
287bool ConnectionsStore::lookupAndExec(
void *instance, uint signal, Quark detail,
288 void *receiver, uint slotHash, ulong handlerId,
289 void (ConnectionsStore::*func)(void*, const Connection &))
291 bool executed =
false;
293 if (m_connections.contains(instance)) {
294 ConnectionsContainer & container = m_connections[instance];
297 ByHandlerIterator it = container.get<by_handlerId>().find(handlerId);
299 if (it != container.get<by_handlerId>().end()) {
300 (this->*func)(instance, *it);
303 container.get<by_handlerId>().erase(it);
306 BySignalIterators iterators = container.get<by_signal>().equal_range(signal);
308 while (iterators.first != iterators.second) {
310 (detail == iterators.first->detail &&
312 (receiver == iterators.first->receiver &&
313 (!slotHash || slotHash == iterators.first->slotHash)
319 (this->*func)(instance, *iterators.first);
322 iterators.first = container.get<by_signal>().erase(iterators.first);
327 }
else if (receiver) {
328 ByReceiverIterators iterators = container.get<by_receiver>().equal_range(receiver);
330 while (iterators.first != iterators.second) {
331 if (!slotHash || slotHash == iterators.first->slotHash) {
332 (this->*func)(instance, *iterators.first);
335 iterators.first = container.get<by_receiver>().erase(iterators.first);
341 for (SequentialIterator it = container.get<sequential>().begin();
342 it != container.get<sequential>().end(); ++it)
344 (this->*func)(instance, *it);
347 container.get<sequential>().clear();
350 if (container.get<sequential>().empty()) {
351 m_connections.remove(instance);
358void ConnectionsStore::disconnectHandler(
void *instance,
const Connection & c)
360 m_handlerIdInRemovalMutex.lock();
361 m_handlerIdInRemoval = c.handlerId;
362 m_handlerIdInRemovalMutex.unlock();
365 g_signal_handler_disconnect(instance, c.handlerId);
367 m_handlerIdInRemovalMutex.lock();
368 m_handlerIdInRemoval = 0;
369 m_handlerIdInRemovalMutex.unlock();
372void ConnectionsStore::disconnectAndDestroyRcvrWatch(
void *instance,
const Connection & c)
374 disconnectHandler(instance, c);
375 destroyReceiverWatch(instance, c);
378void ConnectionsStore::setupClosureWatch(
void *instance, ulong handlerId, GClosure *closure)
380 void *data =
new QPair<void*, ulong>(instance, handlerId);
381 g_closure_add_finalize_notifier(closure, data, &ConnectionsStore::onClosureDestroyed);
385void ConnectionsStore::onClosureDestroyed(
void *data, GClosure *closure)
388 QPair<void*, ulong> *pair =
static_cast< QPair<void*, ulong>*
>(data);
389 s_connectionsStore()->onClosureDestroyedAction(pair->first, pair->second);
393void ConnectionsStore::onClosureDestroyedAction(
void *instance, ulong handlerId)
396 m_handlerIdInRemovalMutex.lock();
397 register bool ok = (m_handlerIdInRemoval != handlerId);
398 m_handlerIdInRemovalMutex.unlock();
401 QMutexLocker l(&m_mutex);
402 lookupAndExec(instance, 0, Quark(), 0, 0, handlerId, &ConnectionsStore::destroyReceiverWatch);
406void ConnectionsStore::setupReceiverWatch(
void *instance,
void *receiver,
407 const DestroyNotifierIfacePtr & notifier)
409 if (!m_receivers.contains(receiver)) {
411 data.notifier = notifier;
412 if (!notifier->connect(receiver,
this, SLOT(onReceiverDestroyed(QObject*)))) {
413 notifier->connect(receiver,
this, SLOT(onReceiverDestroyed(
void*)));
415 m_receivers.insert(receiver, data);
418 m_receivers[receiver].senders[instance]++;
421void ConnectionsStore::destroyReceiverWatch(
void *instance,
const Connection & c)
423 if (--m_receivers[c.receiver].senders[instance] == 0) {
424 m_receivers[c.receiver].senders.remove(instance);
425 if (m_receivers[c.receiver].senders.isEmpty()) {
426 m_receivers[c.receiver].notifier->disconnect(c.receiver,
this);
427 m_receivers.remove(c.receiver);
432void ConnectionsStore::onReceiverDestroyed(
void *receiver)
434 QMutexLocker l(&m_mutex);
435 QHashIterator<void*, int> it(m_receivers[receiver].senders);
436 while (it.hasNext()) {
438 lookupAndExec(it.key(), 0, Quark(), receiver, 0, 0, &ConnectionsStore::disconnectHandler);
440 m_receivers.remove(receiver);
445void ConnectionsStore::onReceiverDestroyed(QObject *receiver)
447 onReceiverDestroyed(
static_cast<void*
>(receiver));
453ulong connect(
void *instance,
const char *signal, Quark detail,
454 void *receiver,
const DestroyNotifierIfacePtr & notifier,
455 uint slotHash, ClosureDataBase *closureData, ConnectFlags flags)
460 if (g_signal_parse_name(signal, Type::fromInstance(instance),
461 &signalId, &detailQuark, FALSE))
463 if (!detail && detailQuark) {
464 detail = detailQuark;
466 return s_connectionsStore()->connect(instance, signalId, detail, receiver,
467 notifier, slotHash, closureData, flags);
469 qWarning() <<
"QGlib::connect: Could not parse signal:" << signal
470 <<
"- Either it does not exist on this instance, or a detail "
471 "was specified but the signal is not detailed";
481bool disconnect(
void *instance,
const char *signal, Quark detail,
482 void *receiver, uint slotHash, ulong handlerId)
485 GQuark detailQuark = 0;
488 if (g_signal_parse_name(signal, Type::fromInstance(instance),
489 &signalId, &detailQuark, FALSE))
491 if (!detail && detailQuark) {
492 detail = detailQuark;
495 qWarning() <<
"QGlib::disconnect: Could not parse signal:" << signal
496 <<
"- Either it does not exist on this instance, or a detail "
497 "was specified but the signal is not detailed";
502 return s_connectionsStore()->disconnect(instance, signalId, detail,
503 receiver, slotHash, handlerId);
511#include "connect.moc"
Wrappers for Glib and GObject classes.