From 81b8c138ba824a3f498dc3fc2a1800c6c488a4c1 Mon Sep 17 00:00:00 2001 From: Jan Haller Date: Sun, 5 Jul 2020 15:54:12 +0200 Subject: [PATCH 1/2] 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; } From 40ec0bf37dee713c6fc0e7f56a81f7b4c112a0af Mon Sep 17 00:00:00 2001 From: Jan Haller Date: Sun, 5 Jul 2020 16:02:12 +0200 Subject: [PATCH 2/2] Separate sf::Texture copy/move assignment operator --- include/SFML/Graphics/Texture.hpp | 17 ++++++++++++++--- src/SFML/Graphics/Texture.cpp | 15 +++++++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/include/SFML/Graphics/Texture.hpp b/include/SFML/Graphics/Texture.hpp index 8f12edc6..3ad55188 100644 --- a/include/SFML/Graphics/Texture.hpp +++ b/include/SFML/Graphics/Texture.hpp @@ -518,14 +518,25 @@ public: bool generateMipmap(); //////////////////////////////////////////////////////////// - /// \brief Combined copy/move assignment operator + /// \brief Copy assignment operator /// - /// \param right Instance to assign (copied or moved) + /// \param copied Instance to assign /// /// \return Reference to self /// //////////////////////////////////////////////////////////// - Texture& operator =(Texture right); + Texture& operator =(const Texture& copied); + + //////////////////////////////////////////////////////////// + /// \brief Move assignment operator + /// + /// \param moved instance to move from. Behaves like a + /// default-constructed object after the move. + /// + /// \return Reference to self + /// + //////////////////////////////////////////////////////////// + Texture& operator =(Texture&& moved); //////////////////////////////////////////////////////////// /// \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 ce52ec80..bfd71473 100644 --- a/src/SFML/Graphics/Texture.cpp +++ b/src/SFML/Graphics/Texture.cpp @@ -817,9 +817,20 @@ unsigned int Texture::getMaximumSize() //////////////////////////////////////////////////////////// -Texture& Texture::operator =(Texture right) +Texture& Texture::operator =(const Texture& copied) { - swap(right); + Texture temp(copied); + swap(temp); + + return *this; +} + + +//////////////////////////////////////////////////////////// +Texture& Texture::operator =(Texture&& moved) +{ + Texture temp(std::move(moved)); + swap(temp); return *this; }