mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-12 16:21:13 +00:00
75 lines
2.6 KiB
C++
75 lines
2.6 KiB
C++
/********************************************************************************
|
|
* 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, *
|
|
* copied verbatim in the file "LICENSE" *
|
|
********************************************************************************/
|
|
#ifndef FAIR_MQ_PROPERTIES_H
|
|
#define FAIR_MQ_PROPERTIES_H
|
|
|
|
#include <fairmq/EventManager.h>
|
|
|
|
#include <boost/any.hpp>
|
|
#include <boost/core/demangle.hpp>
|
|
|
|
#include <functional>
|
|
#include <map>
|
|
#include <unordered_map>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <typeindex>
|
|
#include <typeinfo>
|
|
#include <utility> // pair
|
|
|
|
namespace fair::mq
|
|
{
|
|
|
|
using Property = boost::any;
|
|
using Properties = std::map<std::string, Property>;
|
|
|
|
struct PropertyChange : Event<std::string> {};
|
|
|
|
class PropertyHelper
|
|
{
|
|
public:
|
|
template<typename T>
|
|
static void AddType(std::string label = "")
|
|
{
|
|
if (label.empty()) {
|
|
label = boost::core::demangle(typeid(T).name());
|
|
}
|
|
fTypeInfos[std::type_index(typeid(T))] = [label](const Property& p) {
|
|
std::stringstream ss;
|
|
ss << boost::any_cast<T>(p);
|
|
return std::pair<std::string, std::string>{ss.str(), label};
|
|
};
|
|
fEventEmitters[std::type_index(typeid(T))] = [](const fair::mq::EventManager& em, const std::string& k, const Property& p) {
|
|
em.Emit<PropertyChange, T>(k, boost::any_cast<T>(p));
|
|
};
|
|
}
|
|
|
|
static std::string ConvertPropertyToString(const Property& p)
|
|
{
|
|
return fTypeInfos.at(p.type())(p).first;
|
|
}
|
|
|
|
// returns <valueAsString, typenameAsString>
|
|
static std::pair<std::string, std::string> GetPropertyInfo(const Property& p)
|
|
{
|
|
try {
|
|
return fTypeInfos.at(p.type())(p);
|
|
} catch (std::out_of_range& oor) {
|
|
return {"[unidentified_type]", "[unidentified_type]"};
|
|
}
|
|
}
|
|
|
|
static std::unordered_map<std::type_index, void(*)(const fair::mq::EventManager&, const std::string&, const Property&)> fEventEmitters; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
|
private:
|
|
static std::unordered_map<std::type_index, std::function<std::pair<std::string, std::string>(const Property&)>> fTypeInfos; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
|
};
|
|
|
|
}
|
|
|
|
#endif /* FAIR_MQ_PROPERTIES_H */
|