flatten 20260225
This commit is contained in:
75
tjp/core/events/Signal.cpp
Normal file
75
tjp/core/events/Signal.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#include "Signal.hpp"
|
||||
|
||||
#include <tjp/core/algorithm/set_erase.hpp>
|
||||
#include <tjp/core/threads/TestMutex.hpp>
|
||||
|
||||
namespace tjp::core::signal {
|
||||
|
||||
Listener *Signal::insert(Callback &&callback)
|
||||
{
|
||||
auto lock = lock_of(mutex);
|
||||
auto listener = strong<Listener>(std::move(callback));
|
||||
auto r = listeners.emplace(listener);
|
||||
debug_assert(r.second);
|
||||
|
||||
return ptr_of(listener);
|
||||
}
|
||||
|
||||
void Signal::erase(Listener *listener)
|
||||
{
|
||||
auto lock = lock_of(mutex);
|
||||
|
||||
invalidated = true;
|
||||
set_erase(listeners, listener);
|
||||
}
|
||||
|
||||
void Signal::signal()
|
||||
{
|
||||
// this is correct, below, we reset to begin if it is the special case of the end
|
||||
Listeners::iterator i = listeners.end();
|
||||
|
||||
invalidated = false;
|
||||
|
||||
StrongPtr<Listener> listener = nullptr;
|
||||
do
|
||||
{
|
||||
mutex.lock();
|
||||
if (i == listeners.end())
|
||||
{
|
||||
i = listeners.begin();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (invalidated)
|
||||
{
|
||||
invalidated = false;
|
||||
i = listeners.upper_bound(listener);
|
||||
}
|
||||
else
|
||||
{
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
if (i == listeners.end())
|
||||
break;
|
||||
|
||||
listener = *i;
|
||||
mutex.unlock();
|
||||
|
||||
(*listener)();
|
||||
}
|
||||
while (true);
|
||||
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
void Signal::clear()
|
||||
{
|
||||
mutex.lock();
|
||||
listeners.clear();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
21
tjp/core/events/Signal.h
Normal file
21
tjp/core/events/Signal.h
Normal file
@@ -0,0 +1,21 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <tjp/core/containers/Function.h>
|
||||
|
||||
namespace tjp::core {
|
||||
namespace signal {
|
||||
|
||||
using Callback = Function<void()>;
|
||||
using Listener = Callback;
|
||||
|
||||
struct Signal;
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
using signal::Signal;
|
||||
using SignalListener = signal::Listener;
|
||||
|
||||
} // namespace
|
||||
40
tjp/core/events/Signal.hpp
Normal file
40
tjp/core/events/Signal.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Signal.h"
|
||||
#include <tjp/core/containers/Set.hpp>
|
||||
|
||||
#include <tjp/core/threads/TestMutex.hpp>
|
||||
#include <tjp/core/containers/ByPtr.hpp>
|
||||
|
||||
namespace tjp::core {
|
||||
namespace signal {
|
||||
|
||||
using Callback = Function<void()>;
|
||||
using Listener = Callback;
|
||||
|
||||
struct Signal
|
||||
{
|
||||
[[no_unique_address]]
|
||||
TestMutex mutex;
|
||||
|
||||
typedef SetSortBy<StrongPtr<Listener>, core::ByPtr<Listener>> Listeners;
|
||||
Listeners listeners;
|
||||
|
||||
bool invalidated = false;
|
||||
|
||||
Listener *insert(Callback &&callback);
|
||||
void erase(Listener *listener);
|
||||
|
||||
// todo change to emit
|
||||
void signal();
|
||||
void clear();
|
||||
} ;
|
||||
|
||||
} // namespace
|
||||
|
||||
using signal::Signal;
|
||||
using SignalListener = signal::Listener;
|
||||
|
||||
} // namespace
|
||||
Reference in New Issue
Block a user