added a simple message bus

This commit is contained in:
2025-08-09 15:39:13 +02:00
parent 35fad9f47c
commit 60cef7e938
5 changed files with 483 additions and 4 deletions
+6 -4
View File
@@ -13,8 +13,8 @@ add_library(matador-core STATIC
../../include/matador/net/reactor.hpp
../../include/matador/net/select_fd_sets.hpp
../../include/matador/net/socket_interrupter.hpp
../../include/matador/object/attribute_definition_generator.hpp
../../include/matador/object/attribute_definition.hpp
../../include/matador/object/attribute_definition_generator.hpp
../../include/matador/object/basic_object_info.hpp
../../include/matador/object/error_code.hpp
../../include/matador/object/foreign_node_completer.hpp
@@ -30,6 +30,7 @@ add_library(matador-core STATIC
../../include/matador/object/schema.hpp
../../include/matador/object/schema_node.hpp
../../include/matador/object/schema_node_iterator.hpp
../../include/matador/sql/statement_cache.hpp
../../include/matador/utils/access.hpp
../../include/matador/utils/attribute_reader.hpp
../../include/matador/utils/attribute_writer.hpp
@@ -47,13 +48,14 @@ add_library(matador-core STATIC
../../include/matador/utils/errors.hpp
../../include/matador/utils/export.hpp
../../include/matador/utils/fetch_type.hpp
../../include/matador/utils/file.hpp
../../include/matador/utils/field_attributes.hpp
../../include/matador/utils/file.hpp
../../include/matador/utils/foreign_attributes.hpp
../../include/matador/utils/identifier.hpp
../../include/matador/utils/leader_follower_thread_pool.hpp
../../include/matador/utils/library.hpp
../../include/matador/utils/macro_map.hpp
../../include/matador/utils/message_bus.hpp
../../include/matador/utils/os.hpp
../../include/matador/utils/placeholder.hpp
../../include/matador/utils/primary_key_attribute.hpp
@@ -73,8 +75,8 @@ add_library(matador-core STATIC
logger/log_manager.cpp
logger/logger.cpp
logger/rotating_file_sink.cpp
object/attribute_definition_generator.cpp
object/attribute_definition.cpp
object/attribute_definition_generator.cpp
object/basic_object_info.cpp
object/error_code.cpp
object/foreign_node_completer.cpp
@@ -93,6 +95,7 @@ add_library(matador-core STATIC
utils/identifier.cpp
utils/leader_follower_thread_pool.cpp
utils/library.cpp
utils/message_bus.cpp
utils/os.cpp
utils/primary_key_attribute.cpp
utils/string.cpp
@@ -101,7 +104,6 @@ add_library(matador-core STATIC
utils/uuid.cpp
utils/value.cpp
utils/version.cpp
../../include/matador/sql/statement_cache.hpp
)
target_link_libraries(matador-core ${CMAKE_DL_LIBS})
+97
View File
@@ -0,0 +1,97 @@
#include "matador/utils/message_bus.hpp"
namespace matador::utils {
message::message()
: ptr_(nullptr)
, owner_(nullptr)
, type_(typeid(void)) {}
bool message::empty() const {
return ptr_ == nullptr && !owner_;
}
std::type_index message::type() const {
return type_;
}
const void * message::msg_ptr() const {
return owner_ ? owner_.get() : ptr_;
}
subscription::subscription(message_bus *bus, const std::type_index type, const uint64_t id)
: bus_(bus)
, type_(type)
, id_(id) {}
subscription::subscription(subscription &&other) noexcept
: bus_(other.bus_)
, type_(other.type_)
, id_(other.id_) {
other.bus_ = nullptr;
}
subscription& subscription::operator=(subscription &&other) noexcept {
if (this != &other) {
unsubscribe();
bus_ = other.bus_;
type_ = other.type_;
id_ = other.id_;
other.bus_ = nullptr;
}
return *this;
}
subscription::~subscription() {
unsubscribe();
}
bool subscription::valid() const {
return bus_ != nullptr;
}
void message_bus::unsubscribe(const std::type_index type, HandlerId id) {
std::unique_lock writeLock(mutex_);
const auto it = handlers_.find(type);
if (it == handlers_.end()) {
return;
}
auto &handler_vector = it->second;
handler_vector.erase(std::remove_if(handler_vector.begin(), handler_vector.end(),
[id](const Entry &e) { return e.id == id; }),
handler_vector.end());
if (handler_vector.empty()) {
handlers_.erase(it);
}
}
void message_bus::unsubscribe(HandlerId id) {
std::unique_lock writeLock(mutex_);
for (auto it = handlers_.begin(); it != handlers_.end(); ) {
auto &handler_vector = it->second;
handler_vector.erase(std::remove_if(handler_vector.begin(), handler_vector.end(),
[id](const Entry &e) { return e.id == id; }),
handler_vector.end());
if (handler_vector.empty()) it = handlers_.erase(it);
else ++it;
}
}
void message_bus::publish(const message &msg) const {
if (msg.empty()) {
return;
}
std::vector<Entry> snapshot;
{
std::shared_lock readLock(mutex_);
const auto it = handlers_.find(msg.type());
if (it == handlers_.end()) {
return;
}
snapshot = it->second;
}
const void* p = msg.msg_ptr();
for (const auto &e : snapshot) {
if (!e.filter || e.filter(p)) e.handler(p);
}
}
}