Add unit test for the deferred virtual call implementation in SizeNotifiable.

Fix the build as necessary.
Change DeferredRegister() to take no construction parameters.
This commit is contained in:
King_DuckZ 2016-11-14 00:29:49 +01:00
parent 0a3897f60a
commit 10f830809b
11 changed files with 164 additions and 16 deletions

View file

@ -43,7 +43,7 @@ namespace cloonel {
Placeable(float2(0.0f)),
Drawable(parSize),
m_bottomBar(float2(0.0f), parSize.x()),
m_screenRatio(parMain, DeferredRegister(&m_screenRatio)),
m_screenRatio(parMain, DeferredRegister()),
m_bounceCallback(&DoNothing),
m_texture(new Texture(parPath, parMain, false))
#if defined(WITH_DEBUG_VISUALS)

View file

@ -28,7 +28,7 @@
#include "tiledwallpaper.hpp"
#include "texture.hpp"
#include "platformspawner.hpp"
#include "CloonelJumpConfig.h"
#include "CloonelJumpProjectConfig.h"
#include <algorithm>
#include <SDL2/SDL_scancode.h>
#include <ciso646>

View file

@ -28,7 +28,7 @@ namespace cloonel {
///--------------------------------------------------------------------------
Platform::Platform (SDLMain* parSdlMain, const float2& parPos, Texture* parTexture, const float2& parSize) :
Placeable(parPos),
m_screenRatio(parSdlMain, &m_screenRatio),
m_screenRatio(parSdlMain, DeferredRegister()),
m_size(parSize),
m_collisionTop(new HorzCollisionBar(parPos, parSize.x())),
m_surface(parTexture)

View file

@ -20,7 +20,7 @@
#include "platformset.hpp"
#include "platform.hpp"
#include "CloonelJumpConfig.h"
#include "CloonelJumpProjectConfig.h"
#include "casts.hpp"
#include <ciso646>
#include <algorithm>

View file

@ -19,7 +19,7 @@
#include "platformspawner.hpp"
#include "platform.hpp"
#include "CloonelJumpConfig.h"
#include "CloonelJumpProjectConfig.h"
#include "texture.hpp"
#include "gameplayscene.hpp"
#include "mover.hpp"

View file

@ -67,7 +67,7 @@ namespace cloonel {
///--------------------------------------------------------------------------
DeferredRegister::~DeferredRegister() {
assert(m_call);
if (not std::uncaught_exception())
if (m_call and not std::uncaught_exception())
m_call();
}
} //namespace cloonel

View file

@ -1,5 +1,5 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
Copyright 2014-2016 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
@ -25,6 +25,7 @@
#include <algorithm>
#include <functional>
#include <cassert>
#include <ciso646>
namespace cloonel {
class SizeRatio;
@ -75,12 +76,14 @@ namespace cloonel {
class DeferredRegister {
public:
template <typename R>
DeferredRegister ( SizeNotifiable<R>* parObject ) noexcept;
DeferredRegister ( void ) noexcept = default;
~DeferredRegister ( void );
template <typename R>
void SetThis ( SizeNotifiable<R>* parObject ) const noexcept;
private:
std::function<void()> m_call;
mutable std::function<void()> m_call;
};
class SizeNotifiableBase {
@ -112,7 +115,7 @@ namespace cloonel {
{
}
explicit SizeNotifiable ( const DeferredRegister& parRegisterFunctor ) {
static_cast<void>(parRegisterFunctor);
parRegisterFunctor.SetThis(this);
}
virtual ~SizeNotifiable ( void ) noexcept {
this->Unregister();
@ -133,7 +136,7 @@ namespace cloonel {
SizeNotifiable ( SDLMain* parSdlMain, const DeferredRegister& parRegisterFunctor ) :
RegisterBehaviour(parSdlMain)
{
static_cast<void>(parRegisterFunctor);
parRegisterFunctor.SetThis(this);
}
virtual ~SizeNotifiable ( void ) noexcept {
this->Unregister();
@ -141,9 +144,9 @@ namespace cloonel {
};
template <typename R>
DeferredRegister::DeferredRegister (SizeNotifiable<R>* parObject) noexcept :
m_call(std::bind(&R::Register, static_cast<R*>(parObject), parObject))
{
void DeferredRegister::SetThis (SizeNotifiable<R>* parObject) const noexcept {
assert(not m_call);
m_call = std::bind(&R::Register, static_cast<R*>(parObject), parObject);
assert(m_call);
}
} //namespace cloonel

View file

@ -98,7 +98,7 @@ namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
TiledWallpaper::TileCountNotifiable::TileCountNotifiable (SDLMain* parMain, const ushort2& parTileSize) :
BaseClass(parMain, this),
BaseClass(parMain, DeferredRegister()),
m_tileCount(CountTilesInScreen(parMain->WidthHeight(), parTileSize)),
m_tileSize(vector_cast<float2>(parTileSize))
#if defined(WITH_DEBUG_VISUALS)