Core/Signal: Add Connection object and Disconnection
Former-commit-id: 19cda39e3e9fbc5e26c716c413bafe41deecf7ce
This commit is contained in:
parent
f8682d227b
commit
32e55c2e72
|
|
@ -8,26 +8,97 @@
|
||||||
#define NAZARA_SIGNAL_HPP
|
#define NAZARA_SIGNAL_HPP
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <vector>
|
#include <memory>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
class NzSignal
|
class NzSignal
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using Callback = std::function<void(Args...)>;
|
using Callback = std::function<void(Args...)>;
|
||||||
|
class Connection;
|
||||||
|
class ConnectionGuard;
|
||||||
|
|
||||||
NzSignal() = default;
|
NzSignal() = default;
|
||||||
|
NzSignal(const NzSignal&) = delete;
|
||||||
|
NzSignal(NzSignal&& signal);
|
||||||
~NzSignal() = default;
|
~NzSignal() = default;
|
||||||
|
|
||||||
void Connect(const Callback& func);
|
void Clear();
|
||||||
void Connect(Callback&& func);
|
|
||||||
template<typename O> void Connect(O& object, void (O::*method)(Args...));
|
Connection&& Connect(const Callback& func);
|
||||||
template<typename O> void Connect(O* object, void (O::*method)(Args...));
|
Connection&& Connect(Callback&& func);
|
||||||
|
template<typename O> Connection&& Connect(O& object, void (O::*method)(Args...));
|
||||||
|
template<typename O> Connection&& Connect(O* object, void (O::*method)(Args...));
|
||||||
|
|
||||||
void operator()(Args&&... args);
|
void operator()(Args&&... args);
|
||||||
|
|
||||||
|
NzSignal& operator=(const NzSignal&) = delete;
|
||||||
|
NzSignal& operator=(NzSignal&& signal);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<Callback> m_callbacks;
|
struct Slot;
|
||||||
|
|
||||||
|
using SlotPtr = std::shared_ptr<Slot>;
|
||||||
|
using SlotList = std::list<SlotPtr>;
|
||||||
|
|
||||||
|
struct Slot
|
||||||
|
{
|
||||||
|
Slot(NzSignal* me) :
|
||||||
|
signal(me)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Callback callback;
|
||||||
|
NzSignal* signal;
|
||||||
|
typename SlotList::iterator it;
|
||||||
|
};
|
||||||
|
|
||||||
|
void Disconnect(const SlotPtr& slot);
|
||||||
|
|
||||||
|
SlotList m_slots;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
class NzSignal<Args...>::Connection
|
||||||
|
{
|
||||||
|
using BaseClass = NzSignal<Args...>;
|
||||||
|
friend BaseClass;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Connection(const Connection& connection) = default;
|
||||||
|
Connection(Connection&& connection) = default;
|
||||||
|
~Connection() = default;
|
||||||
|
|
||||||
|
void Disconnect();
|
||||||
|
|
||||||
|
bool IsConnected() const;
|
||||||
|
|
||||||
|
Connection& operator=(const Connection& connection) = default;
|
||||||
|
Connection& operator=(Connection&& connection) = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Connection(const SlotPtr& slot);
|
||||||
|
|
||||||
|
std::weak_ptr<Slot> m_ptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
class NzSignal<Args...>::ConnectionGuard
|
||||||
|
{
|
||||||
|
using BaseClass = NzSignal<Args...>;
|
||||||
|
using Connection = BaseClass::Connection;
|
||||||
|
|
||||||
|
public:
|
||||||
|
ConnectionGuard(const Connection& connection);
|
||||||
|
ConnectionGuard(Connection&& connection);
|
||||||
|
~ConnectionGuard();
|
||||||
|
|
||||||
|
Connection& operator=(const Connection& connection) = delete;
|
||||||
|
Connection& operator=(Connection&& connection) = delete;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Connection m_connection;
|
||||||
};
|
};
|
||||||
|
|
||||||
#include <Nazara/Core/Signal.inl>
|
#include <Nazara/Core/Signal.inl>
|
||||||
|
|
|
||||||
|
|
@ -7,20 +7,41 @@
|
||||||
#include <Nazara/Core/Debug.hpp>
|
#include <Nazara/Core/Debug.hpp>
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
void NzSignal<Args...>::Connect(const Callback& func)
|
NzSignal<Args...>::NzSignal(NzSignal&& signal)
|
||||||
{
|
{
|
||||||
m_callbacks.push_back(func);
|
operator=(std::move(signal));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
void NzSignal<Args...>::Connect(Callback&& func)
|
void NzSignal<Args...>::Clear()
|
||||||
{
|
{
|
||||||
m_callbacks.emplace_back(std::move(func));
|
m_slots.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
typename NzSignal<Args...>::Connection&& NzSignal<Args...>::Connect(const Callback& func)
|
||||||
|
{
|
||||||
|
return Connect(std::move(Callback(func)));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
typename NzSignal<Args...>::Connection&& NzSignal<Args...>::Connect(Callback&& func)
|
||||||
|
{
|
||||||
|
auto tempPtr = std::make_shared<Slot>(this);
|
||||||
|
tempPtr->callback = std::move(func);
|
||||||
|
|
||||||
|
m_slots.emplace_back(std::move(tempPtr));
|
||||||
|
|
||||||
|
const auto& slotPtr = m_slots.back();
|
||||||
|
slotPtr->it = m_slots.end();
|
||||||
|
--slotPtr->it;
|
||||||
|
|
||||||
|
return Connection(slotPtr);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
template<typename O>
|
template<typename O>
|
||||||
void NzSignal<Args...>::Connect(O& object, void (O::*method) (Args...))
|
typename NzSignal<Args...>::Connection&& NzSignal<Args...>::Connect(O& object, void (O::*method) (Args...))
|
||||||
{
|
{
|
||||||
return Connect([&object, method] (Args&&... args)
|
return Connect([&object, method] (Args&&... args)
|
||||||
{
|
{
|
||||||
|
|
@ -30,7 +51,7 @@ void NzSignal<Args...>::Connect(O& object, void (O::*method) (Args...))
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
template<typename O>
|
template<typename O>
|
||||||
void NzSignal<Args...>::Connect(O* object, void (O::*method)(Args...))
|
typename NzSignal<Args...>::Connection&& NzSignal<Args...>::Connect(O* object, void (O::*method)(Args...))
|
||||||
{
|
{
|
||||||
return Connect([object, method] (Args&&... args)
|
return Connect([object, method] (Args&&... args)
|
||||||
{
|
{
|
||||||
|
|
@ -41,8 +62,65 @@ void NzSignal<Args...>::Connect(O* object, void (O::*method)(Args...))
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
void NzSignal<Args...>::operator()(Args&&... args)
|
void NzSignal<Args...>::operator()(Args&&... args)
|
||||||
{
|
{
|
||||||
for (const Callback& func : m_callbacks)
|
for (const SlotPtr& slot : m_slots)
|
||||||
func(std::forward<Args>(args)...);
|
slot->callback(std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
NzSignal<Args...>& NzSignal<Args...>::operator=(NzSignal&& signal)
|
||||||
|
{
|
||||||
|
m_slots = std::move(signal.m_slots);
|
||||||
|
|
||||||
|
// We need to update the signal pointer inside of each slot
|
||||||
|
for (SlotPtr& slot : m_slots)
|
||||||
|
slot->signal = this;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void NzSignal<Args...>::Disconnect(const SlotPtr& slot)
|
||||||
|
{
|
||||||
|
m_slots.erase(slot->it);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
NzSignal<Args...>::Connection::Connection(const SlotPtr& slot) :
|
||||||
|
m_ptr(slot)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void NzSignal<Args...>::Connection::Disconnect()
|
||||||
|
{
|
||||||
|
if (SlotPtr ptr = m_ptr.lock())
|
||||||
|
ptr->signal->Disconnect(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
bool NzSignal<Args...>::Connection::IsConnected() const
|
||||||
|
{
|
||||||
|
return !m_ptr.expired();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
NzSignal<Args...>::ConnectionGuard::ConnectionGuard(const Connection& connection) :
|
||||||
|
m_connection(connection)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
NzSignal<Args...>::ConnectionGuard::ConnectionGuard(Connection&& connection) :
|
||||||
|
m_connection(std::move(connection))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
NzSignal<Args...>::ConnectionGuard::~ConnectionGuard()
|
||||||
|
{
|
||||||
|
m_connection.Disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <Nazara/Core/DebugOff.hpp>
|
#include <Nazara/Core/DebugOff.hpp>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue