Move semantics in SizeNotifiable.

This commit is contained in:
King_DuckZ 2014-03-26 23:00:02 +01:00
parent 6fe884cd5a
commit 2a4ae78374
4 changed files with 40 additions and 22 deletions

View file

@ -33,6 +33,16 @@ namespace cloonel {
assert(m_surface); assert(m_surface);
} }
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
Platform::Platform (Platform&& parOther) :
m_screenRatio(std::move(parOther.m_screenRatio)),
m_position(parOther.m_position),
m_size(parOther.m_size),
m_surface(parOther.m_surface)
{
}
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
void Platform::Draw() const { void Platform::Draw() const {

View file

@ -30,8 +30,10 @@ namespace cloonel {
class Platform : public Drawable { class Platform : public Drawable {
public: public:
Platform ( SDLMain* parSdlMain, const float2& parPos, Texture* parTexture, const float2& parSize ); Platform ( SDLMain* parSdlMain, const float2& parPos, Texture* parTexture, const float2& parSize );
Platform ( Platform&& parOther );
Platform ( const Platform& ) = delete;
virtual ~Platform ( void ) noexcept = default; virtual ~Platform ( void ) noexcept = default;
Platform& operator= ( Platform&& parOther ); Platform& operator= ( Platform&& parOther ) = delete;
virtual void Draw ( void ) const; virtual void Draw ( void ) const;

View file

@ -25,16 +25,24 @@ namespace cloonel {
///---------------------------------------------------------------------- ///----------------------------------------------------------------------
void AutoRegister::Unregister() noexcept { void AutoRegister::Unregister() noexcept {
#if !defined(NDEBUG) #if !defined(NDEBUG)
assert(m_registered); assert(m_registered or not m_sdlmain);
m_registered = false; m_registered = false;
#endif #endif
if (m_registered) {
assert(m_sdlmain); assert(m_sdlmain);
m_sdlmain->UnregisterForResChange(m_id); m_sdlmain->UnregisterForResChange(m_id);
} }
}
///---------------------------------------------------------------------- ///----------------------------------------------------------------------
///---------------------------------------------------------------------- ///----------------------------------------------------------------------
void AutoRegister::Swap (AutoRegister& parOther) noexcept { AutoRegister::AutoRegister (AutoRegister&& parOther) :
m_sdlmain(nullptr),
m_id(0)
#if !defined(NDEBUG)
, m_registered(false)
#endif
{
std::swap(m_sdlmain, parOther.m_sdlmain); std::swap(m_sdlmain, parOther.m_sdlmain);
std::swap(m_id, parOther.m_id); std::swap(m_id, parOther.m_id);
#if !defined(NDEBUG) #if !defined(NDEBUG)
@ -48,11 +56,4 @@ namespace cloonel {
void SizeNotifiableBase::NotifyResChanged (const SizeRatio& parSize) { void SizeNotifiableBase::NotifyResChanged (const SizeRatio& parSize) {
m_scaleRatio = parSize.Ratio(); m_scaleRatio = parSize.Ratio();
} }
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void SizeNotifiableBase::swap (SizeNotifiableBase& parOther) noexcept {
std::swap(m_scaleRatio.x(), parOther.m_scaleRatio.x());
std::swap(m_scaleRatio.y(), parOther.m_scaleRatio.y());
}
} //namespace cloonel } //namespace cloonel

View file

@ -34,17 +34,18 @@ namespace cloonel {
enum { SDLMAIN_NEEDED = false }; enum { SDLMAIN_NEEDED = false };
DontRegister ( void ) = default; DontRegister ( void ) = default;
DontRegister ( DontRegister&& ) { }
~DontRegister ( void ) noexcept = default; ~DontRegister ( void ) noexcept = default;
void Register ( SizeNotifiableBase* ) const noexcept { return; } void Register ( SizeNotifiableBase* ) const noexcept { return; }
void Unregister ( void ) const noexcept { return; } void Unregister ( void ) const noexcept { return; }
void Swap ( DontRegister& ) noexcept { return; }
}; };
class AutoRegister { class AutoRegister {
public: public:
enum { SDLMAIN_NEEDED = true }; enum { SDLMAIN_NEEDED = true };
AutoRegister ( AutoRegister&& parOther );
explicit AutoRegister ( SDLMain* parMain ) : explicit AutoRegister ( SDLMain* parMain ) :
m_sdlmain(parMain) m_sdlmain(parMain)
#if !defined(NDEBUG) #if !defined(NDEBUG)
@ -56,7 +57,6 @@ namespace cloonel {
void Register ( SizeNotifiableBase* parNotifiable ); void Register ( SizeNotifiableBase* parNotifiable );
void Unregister ( void ) noexcept; void Unregister ( void ) noexcept;
void Swap ( AutoRegister& parOther ) noexcept;
private: private:
SDLMain* m_sdlmain; SDLMain* m_sdlmain;
@ -73,7 +73,7 @@ namespace cloonel {
class SizeNotifiableBase { class SizeNotifiableBase {
protected: protected:
SizeNotifiableBase ( void ) = default; SizeNotifiableBase ( void ) = default;
SizeNotifiableBase ( const SizeNotifiableBase& ) = delete; SizeNotifiableBase ( const SizeNotifiableBase& ) = default;
virtual ~SizeNotifiableBase ( void ) noexcept = default; virtual ~SizeNotifiableBase ( void ) noexcept = default;
SizeNotifiableBase& operator= ( const SizeNotifiableBase& ) = delete; SizeNotifiableBase& operator= ( const SizeNotifiableBase& ) = delete;
@ -81,9 +81,6 @@ namespace cloonel {
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; }
protected:
void swap ( SizeNotifiableBase& parOther ) noexcept;
private: private:
float2 m_scaleRatio; float2 m_scaleRatio;
}; };
@ -95,20 +92,30 @@ namespace cloonel {
class SizeNotifiable<RegisterBehaviour, false> : private RegisterBehaviour, public SizeNotifiableBase { class SizeNotifiable<RegisterBehaviour, false> : private RegisterBehaviour, public SizeNotifiableBase {
static_assert(RegisterBehaviour::SDLMAIN_NEEDED == false, "SdlMainNeeded mismatches expected value"); static_assert(RegisterBehaviour::SDLMAIN_NEEDED == false, "SdlMainNeeded mismatches expected value");
public: public:
SizeNotifiable ( const SizeNotifiable& ) = delete;
SizeNotifiable ( SizeNotifiable&& parOther ) :
RegisterBehaviour(parOther),
SizeNotifiableBase(parOther)
{
}
SizeNotifiable ( void ) { SizeNotifiable ( void ) {
this->Register(this); this->Register(this);
} }
virtual ~SizeNotifiable ( void ) noexcept { virtual ~SizeNotifiable ( void ) noexcept {
this->Unregister(); this->Unregister();
} }
void swap ( SizeNotifiable& parOther ) noexcept { SizeNotifiableBase::swap(parOther); RegisterBehaviour::swap(parOther); }
}; };
template <class RegisterBehaviour> template <class RegisterBehaviour>
class SizeNotifiable<RegisterBehaviour, true> : private RegisterBehaviour, public SizeNotifiableBase { class SizeNotifiable<RegisterBehaviour, true> : private RegisterBehaviour, public SizeNotifiableBase {
static_assert(RegisterBehaviour::SDLMAIN_NEEDED == true, "SdlMainNeeded mismatches expected value"); static_assert(RegisterBehaviour::SDLMAIN_NEEDED == true, "SdlMainNeeded mismatches expected value");
public: public:
SizeNotifiable ( const SizeNotifiable& ) = delete;
SizeNotifiable ( SizeNotifiable&& parOther ) :
RegisterBehaviour(std::move(parOther)),
SizeNotifiableBase(parOther)
{
}
explicit SizeNotifiable ( SDLMain* parSdlMain ) : explicit SizeNotifiable ( SDLMain* parSdlMain ) :
RegisterBehaviour(parSdlMain) RegisterBehaviour(parSdlMain)
{ {
@ -117,8 +124,6 @@ namespace cloonel {
virtual ~SizeNotifiable ( void ) noexcept { virtual ~SizeNotifiable ( void ) noexcept {
this->Unregister(); this->Unregister();
} }
void swap ( SizeNotifiable& parOther ) noexcept { SizeNotifiableBase::swap(parOther); RegisterBehaviour::swap(parOther); }
}; };
} //namespace cloonel } //namespace cloonel