From 411c62a3ccd551a96b898f9f32a298b8ccef14e9 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sat, 1 Feb 2014 00:18:15 +0400 Subject: [PATCH] Implement c++11 move for renderers, textures and windows --- SDL2pp/Renderer.cc | 10 ++++++++++ SDL2pp/Renderer.hh | 4 ++-- SDL2pp/Texture.cc | 14 ++++++++++---- SDL2pp/Texture.hh | 6 ++---- SDL2pp/Window.cc | 10 ++++++++++ SDL2pp/Window.hh | 4 ++-- demos/rendertarget.cc | 2 +- 7 files changed, 37 insertions(+), 13 deletions(-) diff --git a/SDL2pp/Renderer.cc b/SDL2pp/Renderer.cc index 312598b..c103881 100644 --- a/SDL2pp/Renderer.cc +++ b/SDL2pp/Renderer.cc @@ -41,6 +41,16 @@ Renderer::~Renderer() { SDL_DestroyRenderer(renderer_); } +Renderer::Renderer(Renderer&& other) noexcept : renderer_(other.renderer_) { + other.renderer_ = nullptr; +} + +Renderer& Renderer::operator=(Renderer&& other) noexcept { + renderer_ = other.renderer_; + other.renderer_ = nullptr; + return *this; +} + SDL_Renderer* Renderer::Get() const { return renderer_; } diff --git a/SDL2pp/Renderer.hh b/SDL2pp/Renderer.hh index ab4a8f8..5bba168 100644 --- a/SDL2pp/Renderer.hh +++ b/SDL2pp/Renderer.hh @@ -45,9 +45,9 @@ public: virtual ~Renderer(); Renderer(const Renderer& other) = delete; - Renderer(Renderer&& other) = delete; + Renderer(Renderer&& other) noexcept; Renderer& operator=(const Renderer& other) = delete; - Renderer& operator=(Renderer&& other) = delete; + Renderer& operator=(Renderer&& other) noexcept; SDL_Renderer* Get() const; diff --git a/SDL2pp/Texture.cc b/SDL2pp/Texture.cc index f7e2c41..31f2147 100644 --- a/SDL2pp/Texture.cc +++ b/SDL2pp/Texture.cc @@ -39,6 +39,16 @@ Texture::~Texture() { SDL_DestroyTexture(texture_); } +Texture::Texture(Texture&& other) noexcept : texture_(other.texture_) { + other.texture_ = nullptr; +} + +Texture& Texture::operator=(Texture&& other) noexcept { + texture_ = other.texture_; + other.texture_ = nullptr; + return *this; +} + SDL_Texture* Texture::Get() const { return texture_; } @@ -63,8 +73,4 @@ void Texture::SetColorMod(Uint8 r, Uint8 g, Uint8 b) { throw Exception("SDL_SetTextureColorMod failed"); } -void Texture::Swap(Texture& other) noexcept { - std::swap(texture_, other.texture_); -} - } diff --git a/SDL2pp/Texture.hh b/SDL2pp/Texture.hh index 905e036..89aa223 100644 --- a/SDL2pp/Texture.hh +++ b/SDL2pp/Texture.hh @@ -41,9 +41,9 @@ public: virtual ~Texture(); Texture(const Texture& other) = delete; - Texture(Texture&& other) = delete; + Texture(Texture&& other) noexcept; Texture& operator=(const Texture& other) = delete; - Texture& operator=(Texture&& other) = delete; + Texture& operator=(Texture&& other) noexcept; SDL_Texture* Get() const; @@ -52,8 +52,6 @@ public: void SetBlendMode(SDL_BlendMode blendMode); void SetAlphaMod(Uint8 alpha = 255); void SetColorMod(Uint8 r = 255, Uint8 g = 255, Uint8 b = 255); - - void Swap(Texture& other) noexcept; }; } diff --git a/SDL2pp/Window.cc b/SDL2pp/Window.cc index 065f553..232f960 100644 --- a/SDL2pp/Window.cc +++ b/SDL2pp/Window.cc @@ -35,6 +35,16 @@ Window::~Window() { SDL_DestroyWindow(window_); } +Window::Window(Window&& other) noexcept : window_(other.window_) { + other.window_ = nullptr; +} + +Window& Window::operator=(Window&& other) noexcept { + window_ = other.window_; + other.window_ = nullptr; + return *this; +} + SDL_Window* Window::Get() const { return window_; } diff --git a/SDL2pp/Window.hh b/SDL2pp/Window.hh index 86ee456..b3e7f66 100644 --- a/SDL2pp/Window.hh +++ b/SDL2pp/Window.hh @@ -37,9 +37,9 @@ public: virtual ~Window(); Window(const Window& other) = delete; - Window(Window&& other) = delete; + Window(Window&& other) noexcept; Window& operator=(const Window& other) = delete; - Window& operator=(Window&& other) = delete; + Window& operator=(Window&& other) noexcept; SDL_Window* Get() const; }; diff --git a/demos/rendertarget.cc b/demos/rendertarget.cc index ec59a2e..44cd01d 100644 --- a/demos/rendertarget.cc +++ b/demos/rendertarget.cc @@ -95,7 +95,7 @@ int Run() { render.Copy(target1, Rect::Null(), Rect(MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0); // Swap textures to copy recursively - target1.Swap(target2); + std::swap(target1, target2); } // Draw result to screen