Added move constructor, and move assignment operator.

This commit is contained in:
Jim-Marsden 2022-11-19 21:06:35 -08:00 committed by Lukas Dürrenberger
parent 2d2f684786
commit a8bc8cf889
3 changed files with 66 additions and 16 deletions

View File

@ -69,6 +69,12 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Texture(); Texture();
////////////////////////////////////////////////////////////
/// \brief Destructor
///
////////////////////////////////////////////////////////////
~Texture();
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Copy constructor /// \brief Copy constructor
/// ///
@ -78,10 +84,22 @@ public:
Texture(const Texture& copy); 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 /// \brief Create the texture
@ -502,16 +520,6 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
[[nodiscard]] bool generateMipmap(); [[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 /// \brief Swap the contents of this texture with those of another
/// ///

View File

@ -38,6 +38,7 @@
#include <climits> #include <climits>
#include <cstring> #include <cstring>
#include <ostream> #include <ostream>
#include <utility>
namespace 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) bool Texture::create(const Vector2u& size)

View File

@ -7,10 +7,8 @@
static_assert(std::is_copy_constructible_v<sf::Texture>); static_assert(std::is_copy_constructible_v<sf::Texture>);
static_assert(std::is_copy_assignable_v<sf::Texture>); static_assert(std::is_copy_assignable_v<sf::Texture>);
static_assert(std::is_move_constructible_v<sf::Texture>); static_assert(std::is_nothrow_move_constructible_v<sf::Texture>);
static_assert(!std::is_nothrow_move_constructible_v<sf::Texture>); static_assert(std::is_nothrow_move_assignable_v<sf::Texture>);
static_assert(std::is_move_assignable_v<sf::Texture>);
static_assert(!std::is_nothrow_move_assignable_v<sf::Texture>);
static_assert(std::is_nothrow_swappable_v<sf::Texture>); static_assert(std::is_nothrow_swappable_v<sf::Texture>);
TEST_CASE("[Graphics] sf::Texture" * doctest::skip(skipDisplayTests)) TEST_CASE("[Graphics] sf::Texture" * doctest::skip(skipDisplayTests))