From bac583d09025755402e9703133b5d9b1cf48bb6c Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Tue, 6 Aug 2024 23:03:16 +0200 Subject: [PATCH] Long live Q_DISABLE_COPY(_MOVE)_X Just like the _X-less versions, but with the possibility of supplying a message, for instance: class Obj : public QObject { Q_DISABLE_COPY_MOVE_X("This class has identity semantics"); }; class Manager { Q_DISABLE_COPY_X("This class manages a resource, so copy is not supported, use a move instead"); }; [ChangeLog][QtCore][QtGlobal] Added the Q_DISABLE_COPY_X and Q_DISABLE_COPY_MOVE_X macros. Change-Id: I9eda2356ea37ead6764eafcb6a13800dbfdf7721 Reviewed-by: Thiago Macieira --- src/corelib/global/qtclasshelpermacros.h | 9 ++++++ src/corelib/global/qtclasshelpermacros.qdoc | 36 +++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/corelib/global/qtclasshelpermacros.h b/src/corelib/global/qtclasshelpermacros.h index 952d3827507..a608b11fb99 100644 --- a/src/corelib/global/qtclasshelpermacros.h +++ b/src/corelib/global/qtclasshelpermacros.h @@ -29,6 +29,15 @@ QT_BEGIN_NAMESPACE Class(Class &&) = delete; \ Class &operator=(Class &&) = delete; +#define Q_DISABLE_COPY_X(Class, reason) \ + Class(const Class &) Q_DECL_EQ_DELETE_X(reason);\ + Class &operator=(const Class &) Q_DECL_EQ_DELETE_X(reason); + +#define Q_DISABLE_COPY_MOVE_X(Class, reason) \ + Q_DISABLE_COPY_X(Class, reason) \ + Class(Class &&) Q_DECL_EQ_DELETE_X(reason); \ + Class &operator=(Class &&) Q_DECL_EQ_DELETE_X(reason); + /* Implementing a move assignment operator using an established technique (move-and-swap, pure swap) is just boilerplate. diff --git a/src/corelib/global/qtclasshelpermacros.qdoc b/src/corelib/global/qtclasshelpermacros.qdoc index 8eaee7e5d2d..13745ad393a 100644 --- a/src/corelib/global/qtclasshelpermacros.qdoc +++ b/src/corelib/global/qtclasshelpermacros.qdoc @@ -43,7 +43,7 @@ application would probably crash when you called a member function of \c{w}. - \sa Q_DISABLE_COPY_MOVE + \sa Q_DISABLE_COPY_MOVE, Q_DISABLE_COPY_X */ /*! @@ -54,6 +54,38 @@ operators, move constructors and move assignment operators for the given \a Class. - \sa Q_DISABLE_COPY + \sa Q_DISABLE_COPY, Q_DISABLE_COPY_MOVE_X \since 5.13 */ + +/*! + \macro Q_DISABLE_COPY_X(Class, reason) + \relates + \since 6.9 + + Like Q_DISABLE_COPY, this macro disables copy operations for the + class \a Class. + + In addition, this documents the \a reason why this class does not support + copy operations. In C++26 this will cause the compiler to report that + reason in its error message against any code that attempts these + unsupported operations. + + \sa Q_DISABLE_COPY, Q_DISABLE_COPY_MOVE_X, Q_DECL_EQ_DELETE_X +*/ + +/*! + \macro Q_DISABLE_COPY_MOVE_X(Class, reason) + \relates + \since 6.9 + + Like Q_DISABLE_COPY_MOVE, this macro disables copy and move operations for the + class \a Class. + + In addition, this documents the \a reason why this class does not support + copy/move operations. In C++26 this will cause the compiler to report that + reason in its error message against any code that attempts these + unsupported operations. + + \sa Q_DISABLE_COPY_X, Q_DECL_EQ_DELETE_X +*/