Core: Added Listenable class (Made class Listener system generic)

Former-commit-id: 3baed32d6720c6455f50af51f262292ece9943fa
This commit is contained in:
Lynix
2015-05-06 18:17:18 +02:00
parent 3195a50114
commit 44ff678002
11 changed files with 121 additions and 206 deletions

View File

@@ -0,0 +1,33 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_LISTENABLE_HPP
#define NAZARA_LISTENABLE_HPP
#include <Nazara/Prerequesites.hpp>
#include <unordered_map>
template<typename Base>
class NzListenable
{
public:
NzListenable();
~NzListenable() = default;
template<typename L> void AddListener(L* listener, void* userdata = nullptr) const;
template<typename L> void RemoveListener(L* listener) const;
template<typename F, typename... Args> void Notify(F callback, Args&&... args);
template<typename F> void NotifyRelease(F callback);
private:
mutable std::unordered_map<void*, void*> m_listeners;
bool m_listenersLocked;
};
#include <Nazara/Core/Listenable.inl>
#endif // NAZARA_LISTENABLE_HPP

View File

@@ -0,0 +1,68 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Debug.hpp>
template<typename Base>
NzListenable<Base>::NzListenable() :
m_listenersLocked(false)
{
}
template<typename Base>
template<typename L>
void NzListenable<Base>::AddListener(L* listener, void* userdata) const
{
static_assert(std::is_base_of<typename Base::Listener, L>::value, "`listener` does not derive from a Listener");
if (!m_listenersLocked)
m_listeners.insert(std::make_pair(listener, userdata));
}
template<typename Base>
template<typename L>
void NzListenable<Base>::RemoveListener(L* listener) const
{
static_assert(std::is_base_of<typename Base::Listener, L>::value, "`listener` does not derive from a Listener");
if (!m_listenersLocked)
m_listeners.erase(listener);
}
template<typename Base>
template<typename F, typename... Args>
void NzListenable<Base>::Notify(F callback, Args&&... args)
{
using Listener = typename Base::Listener;
m_listenersLocked = true;
auto it = m_listeners.begin();
while (it != m_listeners.end())
{
Listener* listener = static_cast<Listener*>(it->first);
if (!(listener->*callback)(static_cast<Base*>(this), std::forward<Args>(args)..., it->second))
m_listeners.erase(it++);
else
++it;
}
m_listenersLocked = false;
}
template<typename Base>
template<typename F>
void NzListenable<Base>::NotifyRelease(F callback)
{
using Listener = typename Base::Listener;
m_listenersLocked = true;
for (auto& pair : m_listeners)
{
Listener* listener = static_cast<Listener*>(pair.first);
(listener->*callback)(static_cast<Base*>(this), pair.second);
}
}
#include <Nazara/Core/DebugOff.hpp>

View File

@@ -8,24 +8,21 @@
#define NAZARA_RENDERTARGET_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Listenable.hpp>
#include <Nazara/Renderer/Config.hpp>
#include <Nazara/Renderer/RenderTargetParameters.hpp>
#include <unordered_map>
class NzRenderer;
class NAZARA_API NzRenderTarget
class NAZARA_API NzRenderTarget : public NzListenable<NzRenderTarget>
{
friend class NzRenderer;
public:
class Listener;
NzRenderTarget();
NzRenderTarget() = default;
virtual ~NzRenderTarget();
void AddListener(Listener* listener, void* userdata = nullptr) const;
virtual unsigned int GetHeight() const = 0;
virtual NzRenderTargetParameters GetParameters() const = 0;
virtual unsigned int GetWidth() const = 0;
@@ -33,8 +30,6 @@ class NAZARA_API NzRenderTarget
bool IsActive() const;
virtual bool IsRenderable() const = 0;
void RemoveListener(Listener* listener) const;
bool SetActive(bool active);
// Fonctions OpenGL
@@ -55,13 +50,6 @@ class NAZARA_API NzRenderTarget
virtual bool Activate() const = 0;
virtual void Desactivate() const;
virtual void EnsureTargetUpdated() const = 0;
void NotifyParametersChange();
void NotifySizeChange();
private:
mutable std::unordered_map<Listener*, void*> m_listeners;
bool m_listenersLocked;
};
#endif // NAZARA_RENDERTARGET_HPP

View File

@@ -8,6 +8,7 @@
#define NAZARA_ABSTRACTATLAS_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Listenable.hpp>
#include <Nazara/Core/SparsePtr.hpp>
#include <Nazara/Math/Rect.hpp>
#include <Nazara/Utility/Enums.hpp>
@@ -16,16 +17,12 @@
class NzAbstractImage;
class NzImage;
class NAZARA_API NzAbstractAtlas
class NAZARA_API NzAbstractAtlas : public NzListenable<NzAbstractAtlas>
{
public:
class Listener;
NzAbstractAtlas();
NzAbstractAtlas() = default;
virtual ~NzAbstractAtlas();
void AddListener(Listener* font, void* userdata = nullptr) const;
virtual void Clear() = 0;
virtual void Free(NzSparsePtr<const NzRectui> rects, NzSparsePtr<unsigned int> layers, unsigned int count) = 0;
virtual NzAbstractImage* GetLayer(unsigned int layerIndex) const = 0;
@@ -33,8 +30,6 @@ class NAZARA_API NzAbstractAtlas
virtual nzUInt32 GetStorage() const = 0;
virtual bool Insert(const NzImage& image, NzRectui* rect, bool* flipped, unsigned int* layerIndex) = 0;
void RemoveListener(Listener* font) const;
class NAZARA_API Listener
{
public:
@@ -45,14 +40,6 @@ class NAZARA_API NzAbstractAtlas
virtual bool OnAtlasLayerChange(const NzAbstractAtlas* atlas, NzAbstractImage* oldLayer, NzAbstractImage* newLayer, void* userdata);
virtual void OnAtlasReleased(const NzAbstractAtlas* atlas, void* userdata);
};
protected:
void NotifyCleared();
void NotifyLayerChange(NzAbstractImage* oldLayer, NzAbstractImage* newLayer);
private:
mutable std::unordered_map<Listener*, void*> m_listeners;
bool m_listenersLocked;
};
#endif // NAZARA_ABSTRACTATLAS_HPP

View File

@@ -8,6 +8,7 @@
#define NAZARA_NODE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Listenable.hpp>
#include <Nazara/Math/Matrix4.hpp>
#include <Nazara/Math/Quaternion.hpp>
#include <Nazara/Math/Vector3.hpp>
@@ -15,17 +16,13 @@
#include <unordered_map>
#include <vector>
class NAZARA_API NzNode
class NAZARA_API NzNode : public NzListenable<NzNode>
{
public:
class Listener;
NzNode();
NzNode(const NzNode& node);
virtual ~NzNode();
void AddListener(Listener* listener, void* userdata = nullptr) const;
void EnsureDerivedUpdate() const;
void EnsureTransformMatrixUpdate() const;
@@ -56,7 +53,6 @@ class NAZARA_API NzNode
NzNode& Move(const NzVector3f& movement, nzCoordSys coordSys = nzCoordSys_Local);
NzNode& Move(float movementX, float movementY, float movementZ = 0.f, nzCoordSys coordSys = nzCoordSys_Local);
void RemoveListener(Listener* listener) const;
NzNode& Rotate(const NzQuaternionf& rotation, nzCoordSys coordSys = nzCoordSys_Local);
NzNode& Scale(const NzVector3f& scale);
@@ -131,14 +127,6 @@ class NAZARA_API NzNode
bool m_inheritScale;
mutable bool m_transformMatrixUpdated;
private:
void NotifyInvalidation();
void NotifyParented(const NzNode* parent);
private:
mutable std::unordered_map<Listener*, void*> m_listeners;
bool m_listenersLocked;
};
#endif // NAZARA_NODE_HPP