Add move semantics to sf::Texture, using combined copy/move assignment operator

This commit is contained in:
Jan Haller 2020-07-05 15:54:12 +02:00
parent 3557c46ae6
commit 81b8c138ba
2 changed files with 25 additions and 10 deletions

View file

@ -72,10 +72,19 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Copy constructor /// \brief Copy constructor
/// ///
/// \param copy instance to copy /// \param copied instance to copy
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Texture(const Texture& copy); Texture(const Texture& copied);
////////////////////////////////////////////////////////////
/// \brief Move constructor
///
/// \param moved instance to move from. Behaves like a
/// default-constructed object after the move.
///
////////////////////////////////////////////////////////////
Texture(Texture&& moved) noexcept;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Destructor /// \brief Destructor
@ -507,16 +516,16 @@ public:
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool generateMipmap(); bool generateMipmap();
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Overload of assignment operator /// \brief Combined copy/move assignment operator
/// ///
/// \param right Instance to assign /// \param right Instance to assign (copied or moved)
/// ///
/// \return Reference to self /// \return Reference to self
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Texture& operator =(const Texture& right); Texture& operator =(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

@ -101,6 +101,14 @@ m_cacheId (getUniqueId())
} }
////////////////////////////////////////////////////////////
Texture::Texture(Texture&& moved) noexcept :
Texture()
{
swap(moved);
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Texture::~Texture() Texture::~Texture()
{ {
@ -809,11 +817,9 @@ unsigned int Texture::getMaximumSize()
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Texture& Texture::operator =(const Texture& right) Texture& Texture::operator =(Texture right)
{ {
Texture temp(right); swap(right);
swap(temp);
return *this; return *this;
} }