From a8bc8cf889ea77d9071fcfceb19f2c0b3dea30d7 Mon Sep 17 00:00:00 2001 From: Jim-Marsden <47166310+Jim-Marsden@users.noreply.github.com> Date: Sat, 19 Nov 2022 21:06:35 -0800 Subject: [PATCH] Added move constructor, and move assignment operator. --- include/SFML/Graphics/Texture.hpp | 32 +++++++++++++--------- src/SFML/Graphics/Texture.cpp | 44 +++++++++++++++++++++++++++++++ test/Graphics/Texture.test.cpp | 6 ++--- 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/include/SFML/Graphics/Texture.hpp b/include/SFML/Graphics/Texture.hpp index 1ecc1e71..f49763bb 100644 --- a/include/SFML/Graphics/Texture.hpp +++ b/include/SFML/Graphics/Texture.hpp @@ -69,6 +69,12 @@ public: //////////////////////////////////////////////////////////// Texture(); + //////////////////////////////////////////////////////////// + /// \brief Destructor + /// + //////////////////////////////////////////////////////////// + ~Texture(); + //////////////////////////////////////////////////////////// /// \brief Copy constructor /// @@ -78,10 +84,22 @@ public: Texture(const Texture& copy); //////////////////////////////////////////////////////////// - /// \brief Destructor + /// \brief Copy assignment operator /// //////////////////////////////////////////////////////////// - ~Texture(); + Texture& operator=(const Texture&); + + //////////////////////////////////////////////////////////// + /// \brief Move constructor + /// + //////////////////////////////////////////////////////////// + Texture(Texture&&) noexcept; + + //////////////////////////////////////////////////////////// + /// \brief Move assignment operator + /// + //////////////////////////////////////////////////////////// + Texture& operator=(Texture&&) noexcept; //////////////////////////////////////////////////////////// /// \brief Create the texture @@ -502,16 +520,6 @@ public: //////////////////////////////////////////////////////////// [[nodiscard]] bool generateMipmap(); - //////////////////////////////////////////////////////////// - /// \brief Overload of assignment operator - /// - /// \param right Instance to assign - /// - /// \return Reference to self - /// - //////////////////////////////////////////////////////////// - Texture& operator=(const Texture& right); - //////////////////////////////////////////////////////////// /// \brief Swap the contents of this texture with those of another /// diff --git a/src/SFML/Graphics/Texture.cpp b/src/SFML/Graphics/Texture.cpp index ed134601..89e31461 100644 --- a/src/SFML/Graphics/Texture.cpp +++ b/src/SFML/Graphics/Texture.cpp @@ -38,6 +38,7 @@ #include #include #include +#include namespace @@ -99,6 +100,49 @@ Texture::~Texture() } } +//////////////////////////////////////////////////////////// +Texture::Texture(Texture&& right) noexcept : +m_size(std::exchange(right.m_size, {})), +m_actualSize(std::exchange(right.m_actualSize, {})), +m_texture(std::exchange(right.m_texture, 0)), +m_isSmooth(std::exchange(right.m_isSmooth, false)), +m_sRgb(std::exchange(right.m_sRgb, false)), +m_isRepeated(std::exchange(right.m_isRepeated, false)), +m_fboAttachment(std::exchange(right.m_fboAttachment, false)), +m_cacheId(std::exchange(right.m_cacheId, 0)) +{ +} + +//////////////////////////////////////////////////////////// +Texture& Texture::operator=(Texture&& right) noexcept +{ + // Catch self-moving. + if (&right == this) + { + return *this; + } + + // Destroy the OpenGL texture + if (m_texture) + { + TransientContextLock lock; + + GLuint texture = m_texture; + glCheck(glDeleteTextures(1, &texture)); + } + + // Move old to new. + m_size = std::exchange(right.m_size, {}); + m_actualSize = std::exchange(right.m_actualSize, {}); + m_texture = std::exchange(right.m_texture, 0); + m_isSmooth = std::exchange(right.m_isSmooth, false); + m_sRgb = std::exchange(right.m_sRgb, false); + m_isRepeated = std::exchange(right.m_isRepeated, false); + m_fboAttachment = std::exchange(right.m_fboAttachment, false); + m_cacheId = std::exchange(right.m_cacheId, 0); + return *this; +} + //////////////////////////////////////////////////////////// bool Texture::create(const Vector2u& size) diff --git a/test/Graphics/Texture.test.cpp b/test/Graphics/Texture.test.cpp index 729aebe6..168b1bac 100644 --- a/test/Graphics/Texture.test.cpp +++ b/test/Graphics/Texture.test.cpp @@ -7,10 +7,8 @@ static_assert(std::is_copy_constructible_v); static_assert(std::is_copy_assignable_v); -static_assert(std::is_move_constructible_v); -static_assert(!std::is_nothrow_move_constructible_v); -static_assert(std::is_move_assignable_v); -static_assert(!std::is_nothrow_move_assignable_v); +static_assert(std::is_nothrow_move_constructible_v); +static_assert(std::is_nothrow_move_assignable_v); static_assert(std::is_nothrow_swappable_v); TEST_CASE("[Graphics] sf::Texture" * doctest::skip(skipDisplayTests))