Don't allow to copy QBasicTimer in Qt 6, it would stop the timer

Fixing this in Qt 5 would be BIC. Although it could be okayish, there
are many more classes that shouldn't be copyable. I need to go through
clazy's output and fix them, and would rather do this noise for Qt 6 and
leave Qt 5 alone and purely BIC free.

Added a move-ctor and move-assign, as well as swap(), deprecated
copy-ctor and copy-assign.

The new copy special member functions warn at runtime if they are called.
In order to not pollute client code with the warning strings, lock them
away by defining the functions out-of-line.

[ChangeLog][QtCore][QBasicTimer] QBasicTimer is now a move-only class.
Copying is now deprecated and will be removed in Qt 6.

[ChangeLog][QtCore][QBasicTimer] Added swap() member and free function.

Change-Id: Ic3e6a26f3989d4c8d125c06e8b0b825411c6e106
Done-with: Marc Mutz <marc.mutz@kdab.com>
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
This commit is contained in:
Sérgio Martins 2016-03-05 17:45:44 +00:00 committed by Giuseppe D'Angelo
parent d2c4782432
commit 4965b0ed77
2 changed files with 81 additions and 0 deletions

View File

@ -65,6 +65,10 @@ QT_BEGIN_NAMESPACE
has not been stopped. The timer's ID can be retrieved using
timerId().
Objects of this class cannot be copied, but can be moved, so you
can maintain a list of basic timers by holding them in container
that supports move-only types, e.g. std::vector.
The \l{widgets/wiggly}{Wiggly} example uses QBasicTimer to repaint
a widget at regular intervals.
@ -79,6 +83,49 @@ QT_BEGIN_NAMESPACE
\sa start()
*/
/*!
\fn QBasicTimer::QBasicTimer(QBasicTimer &&other)
\since 5.14
Move-constructs a basic timer from \a other, which is left
\l{isActive()}{inactive}.
\sa isActive(), swap()
*/
/*!
\fn QBasicTimer &QBasicTimer::operator=(QBasicTimer &&other)
\since 5.14
Move-assigns \a other to this basic timer. The timer
previously represented by this basic timer is stopped.
\a other is left as \l{isActive()}{inactive}.
\sa stop(), isActive(), swap()
*/
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
/*!
\internal
*/
QBasicTimer::QBasicTimer(const QBasicTimer &other)
: id{other.id}
{
qWarning("QBasicTimer can't be copied");
}
/*!
\internal
*/
QBasicTimer &QBasicTimer::operator=(const QBasicTimer &other)
{
id = other.id;
qWarning("QBasicTimer can't be assigned to");
return *this;
}
#endif
/*!
\fn QBasicTimer::~QBasicTimer()
@ -94,6 +141,15 @@ QT_BEGIN_NAMESPACE
\sa start(), stop()
*/
/*!
\fn QBasicTimer::swap(QBasicTimer &other)
\fn swap(QBasicTimer &lhs, QBasicTimer &rhs)
\since 5.14
Swaps string \a other with this string, or \a lhs with \a rhs.
This operation is very fast and never fails.
*/
/*!
\fn int QBasicTimer::timerId() const

View File

@ -51,10 +51,33 @@ class QObject;
class Q_CORE_EXPORT QBasicTimer
{
int id;
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
Q_DISABLE_COPY(QBasicTimer)
#elif QT_DEPRECATED_SINCE(5, 14)
public:
// Just here to preserve BC, we can't remove them yet
QT_DEPRECATED_X("copy-construction is unsupported; use move-construction instead")
QBasicTimer(const QBasicTimer &);
QT_DEPRECATED_X("copy-assignment is unsupported; use move-assignment instead")
QBasicTimer &operator=(const QBasicTimer &);
#endif
public:
inline QBasicTimer() : id(0) {}
inline ~QBasicTimer() { if (id) stop(); }
QBasicTimer(QBasicTimer &&other) noexcept
: id{qExchange(other.id, 0)}
{}
QBasicTimer& operator=(QBasicTimer &&other) noexcept
{
QBasicTimer{std::move(other)}.swap(*this);
return *this;
}
void swap(QBasicTimer &other) noexcept { qSwap(id, other.id); }
inline bool isActive() const { return id != 0; }
inline int timerId() const { return id; }
@ -64,6 +87,8 @@ public:
};
Q_DECLARE_TYPEINFO(QBasicTimer, Q_MOVABLE_TYPE);
inline void swap(QBasicTimer &lhs, QBasicTimer &rhs) noexcept { lhs.swap(rhs); }
QT_END_NAMESPACE
#endif // QBASICTIMER_H