From 002617a5577129dba175fc7c86043cc4493dde80 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Sun, 5 Nov 2017 18:44:14 +0800 Subject: [PATCH] Remove resource handler. We decide to use shared_ptr instead. And we will treat a resource class as a reference. (instead of object) --- SDLWrapper/Cursor.cpp | 4 +-- SDLWrapper/Cursor.h | 8 ++--- SDLWrapper/Font.cpp | 2 +- SDLWrapper/Font.h | 5 ++-- SDLWrapper/_handler.cpp | 1 - SDLWrapper/_handler.h | 66 ----------------------------------------- 6 files changed, 9 insertions(+), 77 deletions(-) delete mode 100644 SDLWrapper/_handler.cpp delete mode 100644 SDLWrapper/_handler.h diff --git a/SDLWrapper/Cursor.cpp b/SDLWrapper/Cursor.cpp index b41ff3f..52f71c4 100644 --- a/SDLWrapper/Cursor.cpp +++ b/SDLWrapper/Cursor.cpp @@ -23,7 +23,7 @@ SDL_Cursor* Cursor::_get() //private void Cursor::_clear() { - _cur.release(); + _cur.reset(); } Cursor::Cursor(Surface surf,Point hotspot) @@ -76,5 +76,5 @@ void Cursor::activate() SDL_SetCursor(_get()); } } -} /// End of namespace MiniEngine +} /// End of namespace MiniEngine diff --git a/SDLWrapper/Cursor.h b/SDLWrapper/Cursor.h index bab651d..2afae0a 100644 --- a/SDLWrapper/Cursor.h +++ b/SDLWrapper/Cursor.h @@ -1,14 +1,14 @@ #pragma once #include "include.h" #include "__Noncopyable.h" -#include "_handler.h" #include "_SystemCursorType.h" #include "Point.h" #include "Surface.h" #include "__Plugin.h" +#include namespace MiniEngine { -class Cursor : public NonCopyable +class Cursor { public: Cursor()=default; @@ -25,7 +25,7 @@ public: void release(); private: - res_ptr _cur; + std::shared_ptr _cur; void _set(SDL_Cursor*); void _set_no_delete(SDL_Cursor*); @@ -34,5 +34,5 @@ private: friend class _internal::Plugin; }; -} /// End of namespace MiniEngine +} /// End of namespace MiniEngine diff --git a/SDLWrapper/Font.cpp b/SDLWrapper/Font.cpp index 5a729c9..83d4394 100644 --- a/SDLWrapper/Font.cpp +++ b/SDLWrapper/Font.cpp @@ -10,7 +10,7 @@ void Font::_set(TTF_Font* p) // private void Font::_clear() { - _font.release(); + _font.reset(); } // private TTF_Font* Font::_get() const diff --git a/SDLWrapper/Font.h b/SDLWrapper/Font.h index cd9d753..e1152d4 100644 --- a/SDLWrapper/Font.h +++ b/SDLWrapper/Font.h @@ -9,10 +9,9 @@ #include #include "__Plugin.h" #include "__Noncopyable.h" -#include "_handler.h" namespace MiniEngine { -class Font : public NonCopyable +class Font { public: Font() = default; @@ -118,7 +117,7 @@ private: void _real_setFontStyle(int); int _style_caster(FontStyle); - res_ptr _font; + std::shared_ptr _font; void _set(TTF_Font*); void _clear(); diff --git a/SDLWrapper/_handler.cpp b/SDLWrapper/_handler.cpp deleted file mode 100644 index 8db72c7..0000000 --- a/SDLWrapper/_handler.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "_handler.h" diff --git a/SDLWrapper/_handler.h b/SDLWrapper/_handler.h deleted file mode 100644 index 0087541..0000000 --- a/SDLWrapper/_handler.h +++ /dev/null @@ -1,66 +0,0 @@ -#pragma once -#include - -namespace MiniEngine -{ - -template -class res_ptr -{ -public: - res_ptr() : _ptr(nullptr) {} - res_ptr(T* ptr,const std::function& delFunc) : _ptr(ptr), _delfunc(delFunc) {} - res_ptr(const res_ptr&)=delete; - res_ptr& operator = (const res_ptr&)=delete; - res_ptr(res_ptr&& t) - { - _ptr=t._ptr; - _delfunc=t._delfunc; - t._ptr=nullptr; - } - res_ptr& operator = (res_ptr&& t) - { - release(); - _ptr=t._ptr; - _delfunc=t._delfunc; - t._ptr=nullptr; - return *this; - } - ~res_ptr() - { - release(); - } - - void release() - { - if(_ptr) - { - _delfunc(_ptr); - _ptr=nullptr; - } - } - void reset(T* ptr,const std::function& delFunc) - { - release(); - _ptr=ptr; - _delfunc=delFunc; - } - T* get() - { - return _ptr; - } - const T* get() const - { - return _ptr; - } - T* operator -> () - { - return _ptr; - } - -private: - T* _ptr; - std::function _delfunc; -}; - -}/// End of namespace MiniEngine