QOpenGLBuffer: add move-SMFs and swap()s

- add move special member functions (docs copied from QHostInfo)

- add member swap

- use move-and-swap, not pure-swap, because these objects hold
  resources (handles) other than just memory

- Q_DECLARE_SHARED (it's not implicitly shared, but explicitly)
  - adds ADL swap and Q_DECLARE_TYPEINFO

[ChangeLog][QtOpenGL][QOpenGLBuffer] Added member-swap(), move
constructor, move assignment operator.

Change-Id: I22dc92108bdd393fff4361db23e94eaf3d7ea9cc
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Marc Mutz 2022-07-12 17:55:21 +02:00
parent 749b2df889
commit ccb2e4dbb1
3 changed files with 46 additions and 2 deletions

View File

@ -166,7 +166,7 @@ QOpenGLBuffer::QOpenGLBuffer(const QOpenGLBuffer &other)
*/
QOpenGLBuffer::~QOpenGLBuffer()
{
if (!d_ptr->ref.deref()) {
if (d_ptr && !d_ptr->ref.deref()) {
destroy();
delete d_ptr;
}
@ -182,7 +182,7 @@ QOpenGLBuffer &QOpenGLBuffer::operator=(const QOpenGLBuffer &other)
{
if (d_ptr != other.d_ptr) {
other.d_ptr->ref.ref();
if (!d_ptr->ref.deref()) {
if (d_ptr && !d_ptr->ref.deref()) {
destroy();
delete d_ptr;
}
@ -191,6 +191,36 @@ QOpenGLBuffer &QOpenGLBuffer::operator=(const QOpenGLBuffer &other)
return *this;
}
/*!
\fn QOpenGLBuffer::QOpenGLBuffer(QOpenGLBuffer &&other)
\since 6.5
Move-constructs a new QOpenGLBuffer from \a other.
\note The moved-from object \a other is placed in a partially-formed state,
in which the only valid operations are destruction and assignment of a new
value.
*/
/*!
\fn QOpenGLBuffer &QOpenGLBuffer::operator=(QOpenGLBuffer &&other)
\since 6.5
Move-assigns \a other to this QOpenGLBuffer instance.
\note The moved-from object \a other is placed in a partially-formed state,
in which the only valid operations are destruction and assignment of a new
value.
*/
/*!
\fn QOpenGLBuffer::swap(QOpenGLBuffer &other)
\since 6.5
Swaps buffer \a other with this buffer. This operation is very fast and
never fails.
*/
/*!
Returns the type of buffer represented by this object.
*/

View File

@ -29,9 +29,15 @@ public:
QOpenGLBuffer();
explicit QOpenGLBuffer(QOpenGLBuffer::Type type);
QOpenGLBuffer(const QOpenGLBuffer &other);
QOpenGLBuffer(QOpenGLBuffer &&other) noexcept
: d_ptr{std::exchange(other.d_ptr, nullptr)} {}
~QOpenGLBuffer();
QOpenGLBuffer &operator=(const QOpenGLBuffer &other);
QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QOpenGLBuffer)
void swap(QOpenGLBuffer &other) noexcept
{ return qt_ptr_swap(d_ptr, other.d_ptr); }
enum UsagePattern
{
@ -98,6 +104,7 @@ private:
Q_DECLARE_PRIVATE(QOpenGLBuffer)
};
Q_DECLARE_SHARED(QOpenGLBuffer)
Q_DECLARE_OPERATORS_FOR_FLAGS(QOpenGLBuffer::RangeAccessFlags)

View File

@ -1588,6 +1588,13 @@ void tst_QOpenGL::bufferCreate()
buf.allocate(128);
QCOMPARE(buf.size(), 128);
{
QOpenGLBuffer moved = std::move(buf);
QCOMPARE_EQ(moved.isCreated(), true);
QCOMPARE_EQ(moved.size(), 128);
buf = std::move(moved);
}
buf.release();
buf.destroy();