Refactoring in SizeNotifiable.

Class is a template now and accepts a registraton policy.
This commit is contained in:
King_DuckZ 2014-03-20 20:21:59 +01:00
parent dbf29f13d1
commit d0a052ddb2
7 changed files with 122 additions and 22 deletions

View file

@ -28,13 +28,10 @@ namespace cloonel {
Character::Character (const std::string& parPath, SDLMain* parMain, float2 parSize) : Character::Character (const std::string& parPath, SDLMain* parMain, float2 parSize) :
Placeable(float2(0.0f)), Placeable(float2(0.0f)),
Drawable(parSize), Drawable(parSize),
m_screenRatio(), m_screenRatio(parMain),
m_screenRatioID(0),
m_sdlmain(parMain),
m_texture(new Texture(parPath, parMain, false)) m_texture(new Texture(parPath, parMain, false))
{ {
assert(parMain); assert(parMain);
m_screenRatioID = parMain->RegisterForResChange(&m_screenRatio);
} }
///------------------------------------------------------------------------- ///-------------------------------------------------------------------------
@ -42,19 +39,15 @@ namespace cloonel {
Character::Character (const std::string&& parPath, SDLMain* parMain, float2 parSize) : Character::Character (const std::string&& parPath, SDLMain* parMain, float2 parSize) :
Placeable(float2(0.0f)), Placeable(float2(0.0f)),
Drawable(parSize), Drawable(parSize),
m_screenRatio(), m_screenRatio(parMain),
m_screenRatioID(0),
m_sdlmain(parMain),
m_texture(new Texture(parPath, parMain, false)) m_texture(new Texture(parPath, parMain, false))
{ {
assert(parMain); assert(parMain);
m_screenRatioID = parMain->RegisterForResChange(&m_screenRatio);
} }
///------------------------------------------------------------------------- ///-------------------------------------------------------------------------
///------------------------------------------------------------------------- ///-------------------------------------------------------------------------
Character::~Character() noexcept { Character::~Character() noexcept {
m_sdlmain->UnregisterForResChange(m_screenRatioID);
} }
///------------------------------------------------------------------------- ///-------------------------------------------------------------------------

View file

@ -42,9 +42,7 @@ namespace cloonel {
virtual void Draw ( void ) const; virtual void Draw ( void ) const;
private: private:
SizeNotifiable m_screenRatio; SizeNotifiable<regbehaviuors::AutoRegister> m_screenRatio;
size_t m_screenRatioID;
SDLMain* const m_sdlmain;
const std::unique_ptr<Texture> m_texture; const std::unique_ptr<Texture> m_texture;
}; };
} //unnamed namespace } //unnamed namespace

View file

@ -28,7 +28,7 @@ namespace cloonel {
SDL_Window* window; SDL_Window* window;
SDL_Renderer* renderer; SDL_Renderer* renderer;
SizeRatio sizeratio; SizeRatio sizeratio;
ObserversManager<SizeNotifiable*> resChangeNotifList; ObserversManager<SizeNotifiableBase*> resChangeNotifList;
bool initialized; 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); parNotif->NotifyResChanged(m_localData->sizeratio);
return m_localData->resChangeNotifList.Add(parNotif); return m_localData->resChangeNotifList.Add(parNotif);
} }

View file

@ -26,7 +26,7 @@
struct SDL_Renderer; struct SDL_Renderer;
namespace cloonel { namespace cloonel {
class SizeNotifiable; class SizeNotifiableBase;
class SDLMain { class SDLMain {
public: public:
@ -38,7 +38,7 @@ namespace cloonel {
ushort2 WidthHeight ( void ) const noexcept; ushort2 WidthHeight ( void ) const noexcept;
void SetResolution ( ushort2 parRes ); void SetResolution ( ushort2 parRes );
size_t RegisterForResChange ( SizeNotifiable* parNotif ); size_t RegisterForResChange ( SizeNotifiableBase* parNotif );
void UnregisterForResChange ( size_t parID ) noexcept; void UnregisterForResChange ( size_t parID ) noexcept;
private: private:

View file

@ -1,10 +1,40 @@
#include "sizenotifiable.hpp" #include "sizenotifiable.hpp"
#include "sizeratio.hpp" #include "sizeratio.hpp"
#include "sdlmain.hpp"
#include <cassert>
#include <ciso646>
namespace cloonel { 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(); m_scaleRatio = parSize.Ratio();
} }
} //namespace cloonel } //namespace cloonel

View file

@ -21,21 +21,94 @@
#define id78906DE4FB0D43219CD3F0D9C620FC06 #define id78906DE4FB0D43219CD3F0D9C620FC06
#include "vector.hpp" #include "vector.hpp"
#include <cstddef>
namespace cloonel { namespace cloonel {
class SizeRatio; class SizeRatio;
class SDLMain;
class SizeNotifiableBase;
class SizeNotifiable { namespace regbehaviuors {
class DontRegister {
public: public:
SizeNotifiable ( void ) = default; enum { SDLMAIN_NEEDED = false };
virtual ~SizeNotifiable ( void ) noexcept = default;
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;
public:
virtual void NotifyResChanged ( const SizeRatio& parSize ); virtual void NotifyResChanged ( const SizeRatio& parSize );
const float2& Ratio ( void ) const noexcept { return m_scaleRatio; } const float2& Ratio ( void ) const noexcept { return m_scaleRatio; }
private: private:
float2 m_scaleRatio; float2 m_scaleRatio;
}; };
template <class RegisterBehaviour, bool=RegisterBehaviour::SDLMAIN_NEEDED>
class SizeNotifiable;
template <class RegisterBehaviour>
class SizeNotifiable<RegisterBehaviour, false> : 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 RegisterBehaviour>
class SizeNotifiable<RegisterBehaviour, true> : 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 } //namespace cloonel
#endif #endif

View file

@ -20,8 +20,9 @@
#ifndef idDDB250A761E9458899C0687B7C8C5B7D #ifndef idDDB250A761E9458899C0687B7C8C5B7D
#define idDDB250A761E9458899C0687B7C8C5B7D #define idDDB250A761E9458899C0687B7C8C5B7D
#include <memory>
#include "drawable.hpp" #include "drawable.hpp"
#include "sizenotifiable.hpp"
#include <memory>
namespace cloonel { namespace cloonel {
class Texture; class Texture;
@ -37,7 +38,12 @@ namespace cloonel {
virtual void Draw ( void ) const; virtual void Draw ( void ) const;
private: private:
class TileCountNotifiable : public SizeNotifiable<regbehaviuors::DontRegister> {
};
TileCountNotifiable m_tileCount;
const std::unique_ptr<Texture> m_tile; const std::unique_ptr<Texture> m_tile;
size_t m_tileCountID;
}; };
} //namespace cloonel } //namespace cloonel