From d25ad10a039514516460f8e446ea734baf69fed1 Mon Sep 17 00:00:00 2001 From: Andre Hartmann Date: Wed, 4 Sep 2019 21:01:43 +0200 Subject: QCanBusDevice: Improve callback register functions Change-Id: I9c2b985768be34308580940c4013448d027fc75b Reviewed-by: Marc Mutz --- src/serialbus/qcanbusdevice.cpp | 8 ++++---- src/serialbus/qcanbusdevice.h | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/serialbus/qcanbusdevice.cpp b/src/serialbus/qcanbusdevice.cpp index 9256bc8..8eb45c1 100644 --- a/src/serialbus/qcanbusdevice.cpp +++ b/src/serialbus/qcanbusdevice.cpp @@ -347,11 +347,11 @@ bool QCanBusDevice::hasOutgoingFrames() const * Called from the derived plugin to register a function that performs the * CAN controller hardware reset when \a resetController() is called. */ -void QCanBusDevice::setResetControllerFunction(std::function &resetter) +void QCanBusDevice::setResetControllerFunction(std::function resetter) { Q_D(QCanBusDevice); - d->m_resetControllerFunction = resetter; + d->m_resetControllerFunction = std::move(resetter); } /*! @@ -359,11 +359,11 @@ void QCanBusDevice::setResetControllerFunction(std::function &resetter) * Called from the derived plugin to register a function that returns the * CAN controller bus status when \a busStatus() is called. */ -void QCanBusDevice::setCanBusStatusGetter(std::function &busStatusGetter) +void QCanBusDevice::setCanBusStatusGetter(std::function busStatusGetter) { Q_D(QCanBusDevice); - d->m_busStatusGetter = busStatusGetter; + d->m_busStatusGetter = std::move(busStatusGetter); } /*! diff --git a/src/serialbus/qcanbusdevice.h b/src/serialbus/qcanbusdevice.h index 1844f4b..476e77e 100644 --- a/src/serialbus/qcanbusdevice.h +++ b/src/serialbus/qcanbusdevice.h @@ -182,8 +182,8 @@ protected: virtual bool open() = 0; virtual void close() = 0; - void setResetControllerFunction(std::function &resetter); - void setCanBusStatusGetter(std::function &busStatusGetter); + void setResetControllerFunction(std::function resetter); + void setCanBusStatusGetter(std::function busStatusGetter); static QCanBusDeviceInfo createDeviceInfo(const QString &name, bool isVirtual = false, -- cgit v1.2.3 From 35e033232f8b19fcd3785baf04fec0db546d8015 Mon Sep 17 00:00:00 2001 From: Andre Hartmann Date: Wed, 4 Sep 2019 21:09:22 +0200 Subject: QCanBusDevice: Undo source-incompatible change Introduced in eca5edf45a8043 Change-Id: If1586d0b62b0544feee5b07289503966efa0f16d Reviewed-by: Alex Blasche --- src/serialbus/qcanbusdevice.h | 1 - tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/serialbus/qcanbusdevice.h b/src/serialbus/qcanbusdevice.h index 476e77e..9b505f9 100644 --- a/src/serialbus/qcanbusdevice.h +++ b/src/serialbus/qcanbusdevice.h @@ -204,7 +204,6 @@ Q_DECLARE_OPERATORS_FOR_FLAGS(QCanBusDevice::Directions) QT_END_NAMESPACE -Q_DECLARE_METATYPE(QCanBusDevice::Filter) Q_DECLARE_METATYPE(QCanBusDevice::Filter::FormatFilter) Q_DECLARE_METATYPE(QList) diff --git a/tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp b/tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp index e0b30f0..61d30c0 100644 --- a/tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp +++ b/tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp @@ -44,6 +44,8 @@ #include +Q_DECLARE_METATYPE(QCanBusDevice::Filter) + class tst_Backend : public QCanBusDevice { Q_OBJECT -- cgit v1.2.3 From 47eaa2b1a58db67643f32cb1d8779c5b199ccb10 Mon Sep 17 00:00:00 2001 From: Andre Hartmann Date: Wed, 4 Sep 2019 21:29:55 +0200 Subject: QCanBusDevice: Make Filter comparison functions non-member friends MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Symmetric operators, such as op== and op!=, shouldn't be members, because member functions do not allow for implicit conversions on the LHS argument, which introduces an asymmetry in how the RHS and LHS arguments are handled by the compiler. We could add the operators as non-member non-friend free functions, but since Filter is a nested class, there's a lot of code between the definiton of Filter and the point where we can lexically declare the operators for the first time (after the definition of the outer class). Code between these points does not see the operators and thus behaves different from code that follows the the definition of the outer class. This is subtle and while in the present case, there's no indication that Filter may get an implicit conversion to some other type which does have an equality operator, there's always the possibility that such a thing might be added later, in which case code using lhsfilter == rhsfilter in the body of QCanBusDevice would call a different operator== than code outside it. It's just safer to declare the operators as soon as possible, which means as non-member friends. Change-Id: Iadd097a9d64d0b1c912b750d6d89431b42def313 Reviewed-by: André Hartmann Reviewed-by: Alex Blasche Reviewed-by: Mårten Nordheim --- src/serialbus/qcanbusdevice.cpp | 10 ++++++---- src/serialbus/qcanbusdevice.h | 10 +++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/serialbus/qcanbusdevice.cpp b/src/serialbus/qcanbusdevice.cpp index 8eb45c1..222f19c 100644 --- a/src/serialbus/qcanbusdevice.cpp +++ b/src/serialbus/qcanbusdevice.cpp @@ -150,16 +150,18 @@ Q_LOGGING_CATEGORY(QT_CANBUS, "qt.canbus") */ /*! - \fn QCanBusDevice::Filter::operator==(const Filter &other) + \fn bool operator==(const QCanBusDevice::Filter &a, const QCanBusDevice::Filter &b) + \relates QCanBusDevice::Filter - Returns true, if the filter \a other is equal to this filter, + Returns true, if the filter \a a is equal to the filter \a b, otherwise returns false. */ /*! - \fn QCanBusDevice::Filter::operator!=(const Filter &other) + \fn bool operator!=(const QCanBusDevice::Filter &a, const QCanBusDevice::Filter &b) + \relates QCanBusDevice::Filter - Returns true, if the filter \a other is not equal to this filter, + Returns true, if the filter \a a is not equal to the filter \a b, otherwise returns false. */ diff --git a/src/serialbus/qcanbusdevice.h b/src/serialbus/qcanbusdevice.h index 9b505f9..5d2d976 100644 --- a/src/serialbus/qcanbusdevice.h +++ b/src/serialbus/qcanbusdevice.h @@ -98,15 +98,15 @@ public: struct Filter { - bool operator==(const Filter &other) const + friend constexpr bool operator==(const Filter &a, const Filter &b) noexcept { - return frameId == other.frameId && frameIdMask == other.frameIdMask - && type == other.type && format == other.format; + return a.frameId == b.frameId && a.frameIdMask == b.frameIdMask + && a.type == b.type && a.format == b.format; } - bool operator!=(const Filter &other) const + friend constexpr bool operator!=(const Filter &a, const Filter &b) noexcept { - return !operator==(other); + return !operator==(a, b); } enum FormatFilter { -- cgit v1.2.3