xbot/thread.hpp

42 lines
844 B
C++

#pragma once
#include "ircmsg.hpp"
#include <memory>
#include <vector>
enum class EventOutcome
{
Pass,
Consume,
};
enum class ThreadOutcome
{
Continue,
Finish,
};
struct Event {
virtual ~Event() {}
};
struct Thread
{
using priority_type = std::uint64_t;
using callback_result = std::pair<ThreadOutcome, EventOutcome>;
virtual ~Thread() {}
virtual auto on_event(Event const& event) -> callback_result { return {}; };
virtual auto priority() const -> priority_type = 0;
};
struct Dispatcher
{
std::vector<std::shared_ptr<Thread>> threads_;
std::vector<std::shared_ptr<Event>> events_;
bool dispatching_;
/// Apply a function to all the threads in priority order
auto dispatch(std::shared_ptr<Event> event) -> void;
auto add_thread(std::shared_ptr<Thread> thread) -> void;
};