Skip to content

Commit

Permalink
Added move constructor, and move assignment operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim-Marsden authored and eXpl0it3r committed Mar 29, 2023
1 parent 2d2f684 commit a8bc8cf
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 16 deletions.
32 changes: 20 additions & 12 deletions include/SFML/Graphics/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class SFML_GRAPHICS_API Texture : GlResource
////////////////////////////////////////////////////////////
Texture();

////////////////////////////////////////////////////////////
/// \brief Destructor
///
////////////////////////////////////////////////////////////
~Texture();

////////////////////////////////////////////////////////////
/// \brief Copy constructor
///
Expand All @@ -78,10 +84,22 @@ class SFML_GRAPHICS_API Texture : GlResource
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
Expand Down Expand Up @@ -502,16 +520,6 @@ class SFML_GRAPHICS_API Texture : GlResource
////////////////////////////////////////////////////////////
[[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
///
Expand Down
44 changes: 44 additions & 0 deletions src/SFML/Graphics/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <climits>
#include <cstring>
#include <ostream>
#include <utility>


namespace
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions test/Graphics/Texture.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

static_assert(std::is_copy_constructible_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_move_assignable_v<sf::Texture>);
static_assert(!std::is_nothrow_move_assignable_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_nothrow_swappable_v<sf::Texture>);

TEST_CASE("[Graphics] sf::Texture" * doctest::skip(skipDisplayTests))
Expand Down

0 comments on commit a8bc8cf

Please sign in to comment.