Skip to content

Commit

Permalink
Long live Q_DISABLE_COPY(_MOVE)_X
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
dangelog committed Aug 11, 2024
1 parent 0ed5d4d commit bac583d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/corelib/global/qtclasshelpermacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 34 additions & 2 deletions src/corelib/global/qtclasshelpermacros.qdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

/*!
Expand All @@ -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 <QtClassHelperMacros>
\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 <QtClassHelperMacros>
\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
*/

0 comments on commit bac583d

Please sign in to comment.