From 81b8c138ba824a3f498dc3fc2a1800c6c488a4c1 Mon Sep 17 00:00:00 2001 From: Jan Haller Date: Sun, 5 Jul 2020 15:54:12 +0200 Subject: [PATCH] Add move semantics to sf::Texture, using combined copy/move assignment operator --- include/SFML/Graphics/Texture.hpp | 21 +++++++++++++++------ src/SFML/Graphics/Texture.cpp | 14 ++++++++++---- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/include/SFML/Graphics/Texture.hpp b/include/SFML/Graphics/Texture.hpp index 66035b45..8f12edc6 100644 --- a/include/SFML/Graphics/Texture.hpp +++ b/include/SFML/Graphics/Texture.hpp @@ -72,10 +72,19 @@ public: //////////////////////////////////////////////////////////// /// \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 @@ -507,16 +516,16 @@ public: /// //////////////////////////////////////////////////////////// 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 /// //////////////////////////////////////////////////////////// - Texture& operator =(const Texture& right); + Texture& operator =(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 a139115d..ce52ec80 100644 --- a/src/SFML/Graphics/Texture.cpp +++ b/src/SFML/Graphics/Texture.cpp @@ -101,6 +101,14 @@ m_cacheId (getUniqueId()) } +//////////////////////////////////////////////////////////// +Texture::Texture(Texture&& moved) noexcept : +Texture() +{ + swap(moved); +} + + //////////////////////////////////////////////////////////// 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(temp); + swap(right); return *this; }