swift
Loading...
Searching...
No Matches
slot.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2016 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#ifndef SWIFT_MISC_SLOT_H
7#define SWIFT_MISC_SLOT_H
8
9#include <atomic>
10#include <functional>
11#include <future>
12#include <memory>
13
14#include <QObject>
15#include <QPointer>
16#include <QTimer>
17#include <QtGlobal>
18
19#include "misc/invoke.h"
20
21namespace swift::misc
22{
26 template <typename T, typename U, typename F, typename G>
27 QMetaObject::Connection connectOnce(T *sender, F signal, U *receiver, G &&slot,
29 {
30 std::promise<QMetaObject::Connection> promise;
31 auto called = std::make_shared<std::atomic_flag>();
32 called->clear();
33 auto wrapper = [receiver, called, connection = promise.get_future().share(),
34 slot = std::forward<G>(slot)](auto &&...args) {
35 if (called->test_and_set())
36 {
37 return;
38 }
39 QObject::disconnect(connection.get());
40 private_ns::invokeSlot(slot, receiver, std::forward<decltype(args)>(args)...);
41 };
42 auto connection = QObject::connect(sender, signal, receiver, std::move(wrapper), type);
43 promise.set_value(connection);
44 return connection;
45 }
46
51 template <typename T, typename F, typename G>
52 QMetaObject::Connection connectOnce(T *sender, F signal, G &&slot)
53 {
54 static_assert(!std::is_member_pointer_v<std::decay_t<G>>,
55 "If slot is a pointer to member, a receiver must be supplied");
56 return connectOnce(sender, signal, sender, std::forward<G>(slot));
57 }
58
64 template <typename F>
65 class CSlot;
66
71 template <typename R, typename... Args>
72 class CSlot<R(Args...)>
73 {
74 public:
76 CSlot() = default;
77
79 template <typename T, typename U>
80 CSlot(T *object, R (U::*function)(Args...))
81 : m_object(object), m_function([=](Args... args) { return (object->*function)(args...); })
82 {
83 Q_ASSERT_X(object, Q_FUNC_INFO, "Need object");
84 }
85
87 template <typename T>
88 CSlot(T *object, std::function<R(Args...)> function) : m_object(object), m_function(function)
89 {}
90
92 R operator()(Args... args) const
93 {
94 Q_ASSERT_X(m_function, Q_FUNC_INFO, "Empty CSlot was called");
95 return m_function(args...);
96 }
97
99 bool singleShot(Args... args) const
100 {
101 // does NOT return the values of m_function!
102 if (!m_object || !m_function)
103 {
104 return false;
105 }
106 QTimer::singleShot(0, m_object.data(), std::bind(*this, std::forward<Args>(args)...));
107 return true;
108 }
109
112 QObject *object() const { return m_object.data(); }
113
116 void setObject(QObject *object)
117 {
118 Q_ASSERT_X(hasNullObject(), Q_FUNC_INFO, "Can only set, not change the object");
119 m_object = object;
120 }
121
123 operator bool() const { return !this->isEmpty() && !this->hasNullObject(); }
124
126 bool operator!() const { return this->isEmpty() || this->hasNullObject(); }
127
129 bool isEmpty() const { return !m_function; }
130
132 bool hasNullObject() const { return m_object.isNull(); }
133
134 private:
135 QPointer<QObject> m_object;
136 std::function<R(Args...)> m_function;
137 };
138} // namespace swift::misc
139
140#endif // SWIFT_MISC_SLOT_H
R operator()(Args... args) const
Call the slot. The behaviour is undefined if the slot is empty.
Definition slot.h:92
CSlot()=default
Construct an empty slot.
bool hasNullObject() const
True if the object is null.
Definition slot.h:132
CSlot(T *object, std::function< R(Args...)> function)
Construct a slot from the given object passing a function and a object.
Definition slot.h:88
bool operator!() const
True if the slot is empty or object is null, false if it can be called.
Definition slot.h:126
bool isEmpty() const
True if the slot is empty, false if it can be called.
Definition slot.h:129
CSlot(T *object, R(U::*function)(Args...))
Construct a slot from the given member function of the given object.
Definition slot.h:80
QObject * object() const
Returns the object which the slot belongs to. Use this as the third argument to QObject::connect to e...
Definition slot.h:112
void setObject(QObject *object)
Set the object which the slot belongs to. Use this as the third argument to QObject::connect to ensur...
Definition slot.h:116
bool singleShot(Args... args) const
Call function "de-coupled" in original thread.
Definition slot.h:99
Callable wrapper for a member function with function signature F.
Definition slot.h:65
Free functions in swift::misc.
QMetaObject::Connection connectOnce(T *sender, F signal, U *receiver, G &&slot, Qt::ConnectionType type=Qt::AutoConnection)
Wrapper around QObject::connect which disconnects after the signal has been emitted once.
Definition slot.h:27
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
ConnectionType