From c11506e95878a39837c5af92dc99b4a1248c13ac Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Thu, 23 Jan 2025 15:06:38 +0100 Subject: [PATCH] feat(EventManager): Out of line some methods --- fairmq/CMakeLists.txt | 3 +- fairmq/EventManager.cxx | 20 ++++++++++ fairmq/EventManager.h | 82 +++++++++++++++++++++++++---------------- fairmq/Properties.h | 3 +- 4 files changed, 73 insertions(+), 35 deletions(-) create mode 100644 fairmq/EventManager.cxx diff --git a/fairmq/CMakeLists.txt b/fairmq/CMakeLists.txt index c6790f3e..e9e7923c 100644 --- a/fairmq/CMakeLists.txt +++ b/fairmq/CMakeLists.txt @@ -1,5 +1,5 @@ ################################################################################ -# Copyright (C) 2012-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH # +# Copyright (C) 2012-2025 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH # # # # This software is distributed under the terms of the # # GNU Lesser General Public Licence (LGPL) version 3, # @@ -119,6 +119,7 @@ if(BUILD_FAIRMQ) Channel.cxx Device.cxx DeviceRunner.cxx + EventManager.cxx JSONParser.cxx MemoryResources.cxx Plugin.cxx diff --git a/fairmq/EventManager.cxx b/fairmq/EventManager.cxx new file mode 100644 index 00000000..174026fb --- /dev/null +++ b/fairmq/EventManager.cxx @@ -0,0 +1,20 @@ +/******************************************************************************** + * Copyright (C) 2025 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * * + * This software is distributed under the terms of the * + * GNU Lesser General Public Licence (LGPL) version 3, * + * copied verbatim in the file "LICENSE" * + ********************************************************************************/ +#include "EventManager.h" + +#include +#include + +template std::shared_ptr< + fair::mq::EventManager::Signal> + fair::mq::EventManager::GetSignal( + const std::pair& key) const; + +template void fair::mq::EventManager::Subscribe( + const std::string& subscriber, + std::function); diff --git a/fairmq/EventManager.h b/fairmq/EventManager.h index bb119d95..f5d7240a 100644 --- a/fairmq/EventManager.h +++ b/fairmq/EventManager.h @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2014-2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2014-2025 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -57,27 +57,8 @@ class EventManager template using Signal = boost::signals2::signal; - template - auto Subscribe(const std::string& subscriber, std::function callback) -> void - { - const std::type_index event_type_index{typeid(E)}; - const std::type_index callback_type_index{typeid(std::function)}; - const auto signalsKey = std::make_pair(event_type_index, callback_type_index); - const auto connectionsKey = std::make_pair(subscriber, signalsKey); - - const auto connection = GetSignal(signalsKey)->connect(callback); - - { - std::lock_guard lock{fMutex}; - - if (fConnections.find(connectionsKey) != fConnections.end()) - { - fConnections.at(connectionsKey).disconnect(); - fConnections.erase(connectionsKey); - } - fConnections.insert({connectionsKey, connection}); - } - } + template + auto Subscribe(const std::string& subscriber, std::function callback) -> void; template auto Unsubscribe(const std::string& subscriber) -> void @@ -119,21 +100,58 @@ class EventManager mutable std::mutex fMutex; template - auto GetSignal(const SignalsKey& key) const -> std::shared_ptr> + auto GetSignal(const SignalsKey& key) const -> std::shared_ptr>; +}; /* class EventManager */ + +struct PropertyChangeAsString : Event {}; + +template +auto EventManager::GetSignal(const SignalsKey& key) const -> std::shared_ptr> +{ + std::lock_guard lock{fMutex}; + + if (fSignals.find(key) == fSignals.end()) { + // wrapper is needed because boost::signals2::signal is neither copyable nor movable + // and I don't know how else to insert it into the map + auto signal = std::make_shared>(); + fSignals.insert(std::make_pair(key, signal)); + } + + return boost::any_cast>>(fSignals.at(key)); +} + +template +auto EventManager::Subscribe(const std::string& subscriber, + std::function callback) -> void +{ + const std::type_index event_type_index{typeid(E)}; + const std::type_index callback_type_index{ + typeid(std::function)}; + const auto signalsKey = std::make_pair(event_type_index, callback_type_index); + const auto connectionsKey = std::make_pair(subscriber, signalsKey); + + const auto connection = GetSignal(signalsKey)->connect(callback); + { std::lock_guard lock{fMutex}; - if (fSignals.find(key) == fSignals.end()) - { - // wrapper is needed because boost::signals2::signal is neither copyable nor movable - // and I don't know how else to insert it into the map - auto signal = std::make_shared>(); - fSignals.insert(std::make_pair(key, signal)); + if (fConnections.find(connectionsKey) != fConnections.end()) { + fConnections.at(connectionsKey).disconnect(); + fConnections.erase(connectionsKey); } - - return boost::any_cast>>(fSignals.at(key)); + fConnections.insert({connectionsKey, connection}); } -}; /* class EventManager */ +} + +extern template std::shared_ptr< + fair::mq::EventManager::Signal> + fair::mq::EventManager::GetSignal( + const std::pair& key) const; + +extern template void + fair::mq::EventManager::Subscribe( + const std::string& subscriber, + std::function); } // namespace fair::mq diff --git a/fairmq/Properties.h b/fairmq/Properties.h index 24b00329..55b2c307 100644 --- a/fairmq/Properties.h +++ b/fairmq/Properties.h @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2014-2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2014-2025 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -29,7 +29,6 @@ using Property = boost::any; using Properties = std::map; struct PropertyChange : Event {}; -struct PropertyChangeAsString : Event {}; class PropertyHelper {