formatting amd naming issues in message_bus.hpp
This commit is contained in:
parent
54e6786ceb
commit
cf977a32aa
|
|
@ -93,7 +93,9 @@ public:
|
||||||
* @return True if the object matches the type, false otherwise.
|
* @return True if the object matches the type, false otherwise.
|
||||||
*/
|
*/
|
||||||
template<typename MessageType>
|
template<typename MessageType>
|
||||||
[[nodiscard]] bool is() const { return type_ == std::type_index(typeid(MessageType)); }
|
[[nodiscard]] bool is() const {
|
||||||
|
return type_ == std::type_index(typeid(MessageType));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Accesses the stored object as a typed reference.
|
* @brief Accesses the stored object as a typed reference.
|
||||||
|
|
@ -105,10 +107,14 @@ public:
|
||||||
*/
|
*/
|
||||||
template<typename MessageType>
|
template<typename MessageType>
|
||||||
const MessageType& get() const {
|
const MessageType& get() const {
|
||||||
if (!is<MessageType>()) throw std::bad_cast();
|
if (!is<MessageType>()) {
|
||||||
const void* p = raw_ptr();
|
throw std::bad_cast();
|
||||||
if (!p) throw std::runtime_error("AnyMessage: empty pointer");
|
}
|
||||||
return *static_cast<const MessageType*>(p);
|
const void* ptr = raw_ptr();
|
||||||
|
if (!ptr) {
|
||||||
|
throw std::runtime_error("AnyMessage: empty pointer");
|
||||||
|
}
|
||||||
|
return *static_cast<const MessageType*>(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -209,26 +215,24 @@ public:
|
||||||
* @return A subscription object to manage the handler's registration.
|
* @return A subscription object to manage the handler's registration.
|
||||||
*/
|
*/
|
||||||
template<typename MessageType>
|
template<typename MessageType>
|
||||||
subscription subscribe(std::function<void(const MessageType&)> handler,
|
subscription subscribe(std::function<void(const MessageType&)> handler, Filter<MessageType> filter = nullptr) {
|
||||||
Filter<MessageType> filter = nullptr)
|
|
||||||
{
|
|
||||||
auto id = next_id_.fetch_add(1, std::memory_order_relaxed);
|
auto id = next_id_.fetch_add(1, std::memory_order_relaxed);
|
||||||
std::unique_lock writeLock(mutex_);
|
std::unique_lock write_lock(mutex_);
|
||||||
|
|
||||||
auto &vec = handlers_[std::type_index(typeid(MessageType))];
|
auto &vec = handlers_[std::type_index(typeid(MessageType))];
|
||||||
Entry e;
|
Entry entry;
|
||||||
e.id = id;
|
entry.id = id;
|
||||||
e.handler = [h = std::move(handler)](const void* p) {
|
entry.handler = [h = std::move(handler)](const void* p) {
|
||||||
h(*static_cast<const MessageType*>(p));
|
h(*static_cast<const MessageType*>(p));
|
||||||
};
|
};
|
||||||
if (filter) {
|
if (filter) {
|
||||||
e.filter = [f = std::move(filter)](const void* p) -> bool {
|
entry.filter = [f = std::move(filter)](const void* p) -> bool {
|
||||||
return f(*static_cast<const MessageType*>(p));
|
return f(*static_cast<const MessageType*>(p));
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
e.filter = nullptr;
|
entry.filter = nullptr;
|
||||||
}
|
}
|
||||||
vec.emplace_back(std::move(e));
|
vec.emplace_back(std::move(entry));
|
||||||
return {this, std::type_index(typeid(MessageType)), id};
|
return {this, std::type_index(typeid(MessageType)), id};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -245,8 +249,7 @@ public:
|
||||||
template<typename MessageType, typename CallerClass>
|
template<typename MessageType, typename CallerClass>
|
||||||
subscription subscribe(CallerClass* instance,
|
subscription subscribe(CallerClass* instance,
|
||||||
MemberHandler<MessageType, CallerClass> memberFn,
|
MemberHandler<MessageType, CallerClass> memberFn,
|
||||||
Filter<MessageType> filter = nullptr)
|
Filter<MessageType> filter = nullptr) {
|
||||||
{
|
|
||||||
auto fn = [instance, memberFn](const MessageType& m) { (instance->*memberFn)(m); };
|
auto fn = [instance, memberFn](const MessageType& m) { (instance->*memberFn)(m); };
|
||||||
return subscribe<MessageType>(std::function<void(const MessageType&)>(fn), std::move(filter));
|
return subscribe<MessageType>(std::function<void(const MessageType&)>(fn), std::move(filter));
|
||||||
}
|
}
|
||||||
|
|
@ -257,29 +260,28 @@ public:
|
||||||
* @tparam MessageType The type of the message to subscribe to.
|
* @tparam MessageType The type of the message to subscribe to.
|
||||||
* @tparam CallerClass The class of the shared_ptr instance.
|
* @tparam CallerClass The class of the shared_ptr instance.
|
||||||
* @param instance A shared pointer to the instance.
|
* @param instance A shared pointer to the instance.
|
||||||
* @param memberFn A pointer to the member function to execute.
|
* @param member_func A pointer to the member function to execute.
|
||||||
* @param filter An optional filter function.
|
* @param filter An optional filter function.
|
||||||
* @return A subscription object to manage the handler's registration.
|
* @return A subscription object to manage the handler's registration.
|
||||||
*/
|
*/
|
||||||
template<typename MessageType, typename CallerClass>
|
template<typename MessageType, typename CallerClass>
|
||||||
subscription subscribe(std::shared_ptr<CallerClass> instance,
|
subscription subscribe(std::shared_ptr<CallerClass> instance,
|
||||||
MemberHandler<MessageType, CallerClass> memberFn,
|
MemberHandler<MessageType, CallerClass> member_func,
|
||||||
Filter<MessageType> filter = nullptr)
|
Filter<MessageType> filter = nullptr) {
|
||||||
{
|
std::weak_ptr<CallerClass> caller_ptr = instance;
|
||||||
std::weak_ptr<CallerClass> w = instance;
|
auto handler = [caller_ptr, member_func](const MessageType& msg) {
|
||||||
auto handler = [w, memberFn](const MessageType& m) {
|
if (auto caller = caller_ptr.lock()) {
|
||||||
if (auto s = w.lock()) {
|
(caller.get()->*member_func)(msg);
|
||||||
(s.get()->*memberFn)(m);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
std::function<bool(const MessageType&)> local_filter = nullptr;
|
std::function<bool(const MessageType&)> local_filter = nullptr;
|
||||||
if (filter) {
|
if (filter) {
|
||||||
local_filter = [w, filter = std::move(filter)](const MessageType& m) -> bool {
|
local_filter = [caller_ptr, filter = std::move(filter)](const MessageType& msg) -> bool {
|
||||||
if (w.expired()) {
|
if (caller_ptr.expired()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return filter(m);
|
return filter(msg);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return subscribe<MessageType>(std::move(handler), std::move(local_filter));
|
return subscribe<MessageType>(std::move(handler), std::move(local_filter));
|
||||||
|
|
@ -321,7 +323,7 @@ public:
|
||||||
void publish(const MessageType& msg) const {
|
void publish(const MessageType& msg) const {
|
||||||
std::vector<Entry> snapshot;
|
std::vector<Entry> snapshot;
|
||||||
{
|
{
|
||||||
std::shared_lock readLock(mutex_);
|
std::shared_lock read_lock(mutex_);
|
||||||
const auto it = handlers_.find(std::type_index(typeid(MessageType)));
|
const auto it = handlers_.find(std::type_index(typeid(MessageType)));
|
||||||
if (it == handlers_.end()) {
|
if (it == handlers_.end()) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -329,7 +331,9 @@ public:
|
||||||
snapshot = it->second; // copy list to avoid holding lock during callbacks
|
snapshot = it->second; // copy list to avoid holding lock during callbacks
|
||||||
}
|
}
|
||||||
for (const auto &e : snapshot) {
|
for (const auto &e : snapshot) {
|
||||||
if (!e.filter || e.filter(&msg)) e.handler(&msg);
|
if (!e.filter || e.filter(&msg)) {
|
||||||
|
e.handler(&msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue