1// SuperTux
2// Copyright (C) 2009 Ingo Ruhnke <grumbel@gmail.com>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17#ifndef HEADER_SUPERTUX_VIDEO_SDL_SURFACE_PTR_HPP
18#define HEADER_SUPERTUX_VIDEO_SDL_SURFACE_PTR_HPP
19
20#include <SDL.h>
21
22/** Simple Wrapper class around SDL_Surface that provides execption
23 safety */
24class SDLSurfacePtr final
25{
26private:
27 SDL_Surface* m_surface;
28
29public:
30 SDLSurfacePtr() :
31 m_surface(nullptr)
32 {}
33
34 explicit SDLSurfacePtr(SDL_Surface* surface) :
35 m_surface(surface)
36 {}
37
38 SDLSurfacePtr(SDLSurfacePtr&& other) noexcept :
39 m_surface(other.m_surface)
40 {
41 other.m_surface = nullptr;
42 }
43
44 SDLSurfacePtr& operator=(SDLSurfacePtr&& other) noexcept
45 {
46 if (this != &other)
47 {
48 m_surface = other.m_surface;
49 other.m_surface = nullptr;
50 }
51 return *this;
52 }
53
54 ~SDLSurfacePtr()
55 {
56 SDL_FreeSurface(m_surface);
57 }
58
59 SDL_Surface& operator*()
60 {
61 return *m_surface;
62 }
63
64 const SDL_Surface& operator*() const
65 {
66 return *m_surface;
67 }
68
69 SDL_Surface* operator->()
70 {
71 return m_surface;
72 }
73
74 const SDL_Surface* operator->() const
75 {
76 return m_surface;
77 }
78
79 void reset(SDL_Surface* surface)
80 {
81 SDL_FreeSurface(m_surface);
82 m_surface = surface;
83 }
84
85 SDL_Surface* get() const
86 {
87 return m_surface;
88 }
89
90 operator void*() {
91 return m_surface;
92 }
93
94private:
95 SDLSurfacePtr(const SDLSurfacePtr&) = delete;
96 SDLSurfacePtr& operator=(const SDLSurfacePtr&) = delete;
97};
98
99#endif
100
101/* EOF */
102