From e149093007aa11617ff5b49803a3c0b533fe93d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 4 Jun 2025 23:07:54 +0200 Subject: [PATCH] Audio: make Buffer and Source nothrow movable. The reason it wasn't done is likely just neglect, that the code wasn't updated since it was made originally, before I knew what noexcept is useful for. --- src/Magnum/Audio/Buffer.h | 8 +++---- src/Magnum/Audio/Source.h | 8 +++---- src/Magnum/Audio/Test/BufferALTest.cpp | 29 ++++++++++++++++++++++++++ src/Magnum/Audio/Test/SourceALTest.cpp | 29 ++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 8 deletions(-) diff --git a/src/Magnum/Audio/Buffer.h b/src/Magnum/Audio/Buffer.h index d8ebfc23c..148cb6539 100644 --- a/src/Magnum/Audio/Buffer.h +++ b/src/Magnum/Audio/Buffer.h @@ -72,13 +72,13 @@ class MAGNUM_AUDIO_EXPORT Buffer { Buffer(const Buffer&) = delete; /** @brief Move constructor */ - Buffer(Buffer&& other); + Buffer(Buffer&& other) noexcept; /** @brief Copying is not allowed */ Buffer& operator=(const Buffer&) = delete; /** @brief Move assignment */ - Buffer& operator=(Buffer&& other); + Buffer& operator=(Buffer&& other) noexcept; /** @brief OpenAL buffer ID */ ALuint id() const { return _id; } @@ -228,11 +228,11 @@ class MAGNUM_AUDIO_EXPORT Buffer { ALuint _id; }; -inline Buffer::Buffer(Buffer&& other): _id(other._id) { +inline Buffer::Buffer(Buffer&& other) noexcept: _id(other._id) { other._id = 0; } -inline Buffer& Buffer::operator=(Buffer&& other) { +inline Buffer& Buffer::operator=(Buffer&& other) noexcept { using Utility::swap; swap(_id, other._id); return *this; diff --git a/src/Magnum/Audio/Source.h b/src/Magnum/Audio/Source.h index 564e38427..c07b4c169 100644 --- a/src/Magnum/Audio/Source.h +++ b/src/Magnum/Audio/Source.h @@ -78,13 +78,13 @@ class MAGNUM_AUDIO_EXPORT Source { Source(const Source&) = delete; /** @brief Move constructor */ - Source(Source&& other); + Source(Source&& other) noexcept; /** @brief Copying is not allowed */ Source& operator=(const Source&) = delete; /** @brief Move assignment */ - Source& operator=(Source&& other); + Source& operator=(Source&& other) noexcept; /** @brief OpenAL source ID */ ALuint id() const { return _id; } @@ -776,11 +776,11 @@ MAGNUM_AUDIO_EXPORT Debug& operator<<(Debug& debug, Source::State value); /** @debugoperatorclassenum{Source,Source::Type} */ MAGNUM_AUDIO_EXPORT Debug& operator<<(Debug& debug, Source::Type value); -inline Source::Source(Source&& other): _id(other._id) { +inline Source::Source(Source&& other) noexcept: _id(other._id) { other._id = 0; } -inline Source& Source::operator=(Source&& other) { +inline Source& Source::operator=(Source&& other) noexcept { using Utility::swap; swap(_id, other._id); return *this; diff --git a/src/Magnum/Audio/Test/BufferALTest.cpp b/src/Magnum/Audio/Test/BufferALTest.cpp index dbc83fea7..0179dd721 100644 --- a/src/Magnum/Audio/Test/BufferALTest.cpp +++ b/src/Magnum/Audio/Test/BufferALTest.cpp @@ -41,6 +41,8 @@ struct BufferALTest: TestSuite::Tester { explicit BufferALTest(); void construct(); + void constructCopy(); + void constructMove(); void properties(); @@ -58,6 +60,8 @@ BufferALTest::BufferALTest(): _context{arguments().first(), arguments().second()} { addTests({&BufferALTest::construct, + &BufferALTest::constructCopy, + &BufferALTest::constructMove, &BufferALTest::properties, @@ -73,6 +77,31 @@ void BufferALTest::construct() { CORRADE_VERIFY(buf.id() != 0); } +void BufferALTest::constructCopy() { + CORRADE_VERIFY(!std::is_copy_constructible{}); + CORRADE_VERIFY(!std::is_copy_assignable{}); +} + +void BufferALTest::constructMove() { + Buffer a; + const ALuint id = a.id(); + CORRADE_VERIFY(a.id() != 0); + + Buffer b{Utility::move(a)}; + CORRADE_COMPARE(a.id(), 0); + CORRADE_COMPARE(b.id(), id); + + Buffer c; + const ALuint cId = c.id(); + c = Utility::move(b); + CORRADE_VERIFY(cId != 0); + CORRADE_COMPARE(b.id(), cId); + CORRADE_COMPARE(c.id(), id); + + CORRADE_VERIFY(std::is_nothrow_move_constructible::value); + CORRADE_VERIFY(std::is_nothrow_move_assignable::value); +} + void BufferALTest::properties() { Buffer buf; constexpr char data[] { 25, 17, 24, 122, 67, 24, 48, 96 }; diff --git a/src/Magnum/Audio/Test/SourceALTest.cpp b/src/Magnum/Audio/Test/SourceALTest.cpp index 6297fac4f..01ad05065 100644 --- a/src/Magnum/Audio/Test/SourceALTest.cpp +++ b/src/Magnum/Audio/Test/SourceALTest.cpp @@ -38,6 +38,8 @@ struct SourceALTest: TestSuite::Tester { explicit SourceALTest(); void construct(); + void constructCopy(); + void constructMove(); void position(); void direction(); @@ -60,6 +62,8 @@ SourceALTest::SourceALTest(): _context{arguments().first(), arguments().second()} { addTests({&SourceALTest::construct, + &SourceALTest::constructCopy, + &SourceALTest::constructMove, &SourceALTest::position, &SourceALTest::direction, @@ -80,6 +84,31 @@ void SourceALTest::construct() { CORRADE_VERIFY(source.id() != 0); } +void SourceALTest::constructCopy() { + CORRADE_VERIFY(!std::is_copy_constructible{}); + CORRADE_VERIFY(!std::is_copy_assignable{}); +} + +void SourceALTest::constructMove() { + Source a; + const ALuint id = a.id(); + CORRADE_VERIFY(a.id() != 0); + + Source b{Utility::move(a)}; + CORRADE_COMPARE(a.id(), 0); + CORRADE_COMPARE(b.id(), id); + + Source c; + const ALuint cId = c.id(); + c = Utility::move(b); + CORRADE_VERIFY(cId != 0); + CORRADE_COMPARE(b.id(), cId); + CORRADE_COMPARE(c.id(), id); + + CORRADE_VERIFY(std::is_nothrow_move_constructible::value); + CORRADE_VERIFY(std::is_nothrow_move_assignable::value); +} + void SourceALTest::position() { Source source; constexpr Vector3 pos{3.0f, 5.0f, 6.0f};