From d0a052ddb22d6e274ab3832c65916944e9c11118 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 20 Mar 2014 20:21:59 +0100 Subject: [PATCH] Refactoring in SizeNotifiable. Class is a template now and accepts a registraton policy. --- src/character.cpp | 11 ++---- src/character.hpp | 4 +-- src/sdlmain.cpp | 4 +-- src/sdlmain.hpp | 4 +-- src/sizenotifiable.cpp | 32 ++++++++++++++++- src/sizenotifiable.hpp | 81 +++++++++++++++++++++++++++++++++++++++--- src/tiledwallpaper.hpp | 8 ++++- 7 files changed, 122 insertions(+), 22 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index 1dd6f29..aa3250e 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -28,13 +28,10 @@ namespace cloonel { Character::Character (const std::string& parPath, SDLMain* parMain, float2 parSize) : Placeable(float2(0.0f)), Drawable(parSize), - m_screenRatio(), - m_screenRatioID(0), - m_sdlmain(parMain), + m_screenRatio(parMain), m_texture(new Texture(parPath, parMain, false)) { assert(parMain); - m_screenRatioID = parMain->RegisterForResChange(&m_screenRatio); } ///------------------------------------------------------------------------- @@ -42,19 +39,15 @@ namespace cloonel { Character::Character (const std::string&& parPath, SDLMain* parMain, float2 parSize) : Placeable(float2(0.0f)), Drawable(parSize), - m_screenRatio(), - m_screenRatioID(0), - m_sdlmain(parMain), + m_screenRatio(parMain), m_texture(new Texture(parPath, parMain, false)) { assert(parMain); - m_screenRatioID = parMain->RegisterForResChange(&m_screenRatio); } ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- Character::~Character() noexcept { - m_sdlmain->UnregisterForResChange(m_screenRatioID); } ///------------------------------------------------------------------------- diff --git a/src/character.hpp b/src/character.hpp index 61da69d..ce78c7d 100644 --- a/src/character.hpp +++ b/src/character.hpp @@ -42,9 +42,7 @@ namespace cloonel { virtual void Draw ( void ) const; private: - SizeNotifiable m_screenRatio; - size_t m_screenRatioID; - SDLMain* const m_sdlmain; + SizeNotifiable m_screenRatio; const std::unique_ptr m_texture; }; } //unnamed namespace diff --git a/src/sdlmain.cpp b/src/sdlmain.cpp index 38cbc75..fb621b1 100644 --- a/src/sdlmain.cpp +++ b/src/sdlmain.cpp @@ -28,7 +28,7 @@ namespace cloonel { SDL_Window* window; SDL_Renderer* renderer; SizeRatio sizeratio; - ObserversManager resChangeNotifList; + ObserversManager resChangeNotifList; bool initialized; }; @@ -108,7 +108,7 @@ namespace cloonel { ///------------------------------------------------------------------------ ///------------------------------------------------------------------------ - size_t SDLMain::RegisterForResChange (SizeNotifiable* parNotif) { + size_t SDLMain::RegisterForResChange (SizeNotifiableBase* parNotif) { parNotif->NotifyResChanged(m_localData->sizeratio); return m_localData->resChangeNotifList.Add(parNotif); } diff --git a/src/sdlmain.hpp b/src/sdlmain.hpp index ae3bdea..2f4a55e 100644 --- a/src/sdlmain.hpp +++ b/src/sdlmain.hpp @@ -26,7 +26,7 @@ struct SDL_Renderer; namespace cloonel { - class SizeNotifiable; + class SizeNotifiableBase; class SDLMain { public: @@ -38,7 +38,7 @@ namespace cloonel { ushort2 WidthHeight ( void ) const noexcept; void SetResolution ( ushort2 parRes ); - size_t RegisterForResChange ( SizeNotifiable* parNotif ); + size_t RegisterForResChange ( SizeNotifiableBase* parNotif ); void UnregisterForResChange ( size_t parID ) noexcept; private: diff --git a/src/sizenotifiable.cpp b/src/sizenotifiable.cpp index 5f3b58e..88ebb7b 100644 --- a/src/sizenotifiable.cpp +++ b/src/sizenotifiable.cpp @@ -1,10 +1,40 @@ #include "sizenotifiable.hpp" #include "sizeratio.hpp" +#include "sdlmain.hpp" +#include +#include namespace cloonel { + namespace implem { + } //namespace implem + + namespace regbehaviuors { + ///---------------------------------------------------------------------- + ///---------------------------------------------------------------------- + void AutoRegister::Register (SizeNotifiableBase* parNotifiable) { + assert(m_sdlmain); +#if !defined(NDEBUG) + assert(not m_registered); + m_registered = true; +#endif + m_id = m_sdlmain->RegisterForResChange(parNotifiable); + } + + ///---------------------------------------------------------------------- + ///---------------------------------------------------------------------- + void AutoRegister::Unregister() noexcept { +#if !defined(NDEBUG) + assert(m_registered); + m_registered = false; +#endif + assert(m_sdlmain); + m_sdlmain->UnregisterForResChange(m_id); + } + } //namespace regbehaviuors + ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- - void SizeNotifiable::NotifyResChanged (const SizeRatio& parSize) { + void SizeNotifiableBase::NotifyResChanged (const SizeRatio& parSize) { m_scaleRatio = parSize.Ratio(); } } //namespace cloonel diff --git a/src/sizenotifiable.hpp b/src/sizenotifiable.hpp index c107840..a945f06 100644 --- a/src/sizenotifiable.hpp +++ b/src/sizenotifiable.hpp @@ -21,21 +21,94 @@ #define id78906DE4FB0D43219CD3F0D9C620FC06 #include "vector.hpp" +#include namespace cloonel { class SizeRatio; + class SDLMain; + class SizeNotifiableBase; + + namespace regbehaviuors { + class DontRegister { + public: + enum { SDLMAIN_NEEDED = false }; + + DontRegister ( void ) = default; + ~DontRegister ( void ) noexcept = default; + + void Register ( SizeNotifiableBase* ) const noexcept { return; } + void Unregister ( void ) const noexcept { return; } + }; + + class AutoRegister { + public: + enum { SDLMAIN_NEEDED = true }; + + explicit AutoRegister ( SDLMain* parMain ) : + m_sdlmain(parMain) +#if !defined(NDEBUG) + , m_registered(false) +#endif + { + } + ~AutoRegister ( void ) noexcept = default; + + void Register ( SizeNotifiableBase* parNotifiable ); + void Unregister ( void ) noexcept; + + private: + SDLMain* const m_sdlmain; + size_t m_id; +#if !defined(NDEBUG) + bool m_registered; +#endif + }; + } //namespace regbehaviuors + + namespace implem { + } //namespace implem + + class SizeNotifiableBase { + protected: + SizeNotifiableBase ( void ) = default; + virtual ~SizeNotifiableBase ( void ) noexcept = default; - class SizeNotifiable { public: - SizeNotifiable ( void ) = default; - virtual ~SizeNotifiable ( void ) noexcept = default; - virtual void NotifyResChanged ( const SizeRatio& parSize ); const float2& Ratio ( void ) const noexcept { return m_scaleRatio; } private: float2 m_scaleRatio; }; + + template + class SizeNotifiable; + + template + class SizeNotifiable : private RegisterBehaviour, public SizeNotifiableBase { + static_assert(RegisterBehaviour::SDLMAIN_NEEDED == false, "SdlMainNeeded mismatches expected value"); + public: + SizeNotifiable ( void ) { + this->Register(this); + } + virtual ~SizeNotifiable ( void ) noexcept { + this->Unregister(); + } + }; + + template + class SizeNotifiable : private RegisterBehaviour, public SizeNotifiableBase { + static_assert(RegisterBehaviour::SDLMAIN_NEEDED == true, "SdlMainNeeded mismatches expected value"); + public: + explicit SizeNotifiable ( SDLMain* parSdlMain ) : + RegisterBehaviour(parSdlMain) + { + this->Register(this); + } + virtual ~SizeNotifiable ( void ) noexcept { + this->Unregister(); + } + }; } //namespace cloonel #endif diff --git a/src/tiledwallpaper.hpp b/src/tiledwallpaper.hpp index 892e10c..a24638e 100644 --- a/src/tiledwallpaper.hpp +++ b/src/tiledwallpaper.hpp @@ -20,8 +20,9 @@ #ifndef idDDB250A761E9458899C0687B7C8C5B7D #define idDDB250A761E9458899C0687B7C8C5B7D -#include #include "drawable.hpp" +#include "sizenotifiable.hpp" +#include namespace cloonel { class Texture; @@ -37,7 +38,12 @@ namespace cloonel { virtual void Draw ( void ) const; private: + class TileCountNotifiable : public SizeNotifiable { + }; + + TileCountNotifiable m_tileCount; const std::unique_ptr m_tile; + size_t m_tileCountID; }; } //namespace cloonel