Replace Listenable system by signals
Former-commit-id: 16fb0b3e703ca4b41ceb97fab938cebb05f677d4
This commit is contained in:
@@ -1,33 +0,0 @@
|
||||
// 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
|
||||
@@ -1,68 +0,0 @@
|
||||
// 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>
|
||||
@@ -11,6 +11,16 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#define NazaraSignal(SignalName, ...) using SignalName ## Type = NzSignal<__VA_ARGS__>; \
|
||||
mutable SignalName ## Type SignalName
|
||||
|
||||
#define NazaraSlotType(Class, SignalName) Class::SignalName ## Type::ConnectionGuard
|
||||
#define NazaraSlot(Class, SignalName, SlotName) NazaraSlotType(Class, SignalName) SlotName
|
||||
|
||||
#define NazaraConnect(Instance, SignalName, Callback) (Instance).SignalName.Connect(this, Callback)
|
||||
#define NazaraDisconnect(SlotName) SlotName.GetConnection().Disconnect()
|
||||
|
||||
|
||||
template<typename... Args>
|
||||
class NzSignal
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user