clooneljump/src/sizenotifiable.hpp
King_DuckZ 5e76b49cae Use the deferred virtual call mechanism to avoid the virtual
call bug in the constructor.

See docs/wrong_virtual_call.md for a description.
2016-11-10 23:19:55 +01:00

151 lines
4.2 KiB
C++

/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id78906DE4FB0D43219CD3F0D9C620FC06
#define id78906DE4FB0D43219CD3F0D9C620FC06
#include "vectypes.hpp"
#include <cstddef>
#include <algorithm>
#include <functional>
#include <cassert>
namespace cloonel {
class SizeRatio;
class SDLMain;
class SizeNotifiableBase;
namespace regbehaviours {
class DontRegister {
public:
enum { SDLMAIN_NEEDED = false };
DontRegister ( void ) = default;
DontRegister ( DontRegister&&, SizeNotifiableBase* ) { }
~DontRegister ( void ) noexcept = default;
void Register ( SizeNotifiableBase* ) const noexcept { return; }
void Unregister ( void ) const noexcept { return; }
};
class AutoRegister {
public:
enum { SDLMAIN_NEEDED = true };
AutoRegister ( AutoRegister&& parOther, SizeNotifiableBase* parSwapTo );
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* m_sdlmain;
size_t m_id;
#if !defined(NDEBUG)
bool m_registered;
#endif
};
} //namespace regbehaviours
template <class RegisterBehaviour, bool=RegisterBehaviour::SDLMAIN_NEEDED>
class SizeNotifiable;
class DeferredRegister {
public:
template <typename R>
DeferredRegister ( SizeNotifiable<R>* parObject ) noexcept;
~DeferredRegister ( void );
private:
std::function<void()> m_call;
};
class SizeNotifiableBase {
protected:
SizeNotifiableBase ( void ) = default;
SizeNotifiableBase ( const SizeNotifiableBase& ) = default;
virtual ~SizeNotifiableBase ( void ) noexcept = default;
SizeNotifiableBase& operator= ( const SizeNotifiableBase& ) = delete;
public:
void NotifyResChanged ( const SizeRatio& parSize );
const float2& Ratio ( void ) const noexcept { return m_scaleRatio; }
private:
virtual void OnResChanged ( const SizeRatio& );
float2 m_scaleRatio;
};
template <class RegisterBehaviour>
class SizeNotifiable<RegisterBehaviour, false> : private RegisterBehaviour, public SizeNotifiableBase {
static_assert(RegisterBehaviour::SDLMAIN_NEEDED == false, "SdlMainNeeded mismatches expected value");
friend class DeferredRegister;
public:
SizeNotifiable ( const SizeNotifiable& ) = delete;
SizeNotifiable ( SizeNotifiable&& parOther ) :
RegisterBehaviour(std::move(parOther), this),
SizeNotifiableBase(parOther)
{
}
explicit SizeNotifiable ( const DeferredRegister& parRegisterFunctor ) {
static_cast<void>(parRegisterFunctor);
}
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");
friend class DeferredRegister;
public:
SizeNotifiable ( const SizeNotifiable& ) = delete;
SizeNotifiable ( SizeNotifiable&& parOther ) :
RegisterBehaviour(std::move(parOther), this),
SizeNotifiableBase(parOther)
{
}
SizeNotifiable ( SDLMain* parSdlMain, const DeferredRegister& parRegisterFunctor ) :
RegisterBehaviour(parSdlMain)
{
static_cast<void>(parRegisterFunctor);
}
virtual ~SizeNotifiable ( void ) noexcept {
this->Unregister();
}
};
template <typename R>
DeferredRegister::DeferredRegister (SizeNotifiable<R>* parObject) noexcept :
m_call(std::bind(&R::Register, static_cast<R*>(parObject), parObject))
{
assert(m_call);
}
} //namespace cloonel
#endif