Skip to content

Source · fairmq/

Properties.hpublic header

Defines class fair::mq::PropertyHelper, struct fair::mq::PropertyChange.

Described in Configuration and properties.

fairmq/Properties.h74 lines · C++API: PropertyHelperAPI: PropertyChangeGitHub ↗
1/********************************************************************************2 * Copyright (C) 2014-2025 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH  *3 *                                                                              *4 *              This software is distributed under the terms of the             *5 *              GNU Lesser General Public Licence (LGPL) version 3,             *6 *                  copied verbatim in the file "LICENSE"                       *7 ********************************************************************************/8#ifndef FAIR_MQ_PROPERTIES_H9#define FAIR_MQ_PROPERTIES_H10 11#include <fairmq/EventManager.h>12 13#include <boost/any.hpp>14#include <boost/core/demangle.hpp>15 16#include <functional>17#include <map>18#include <unordered_map>19#include <stdexcept>20#include <string>21#include <typeindex>22#include <typeinfo>23#include <utility> // pair24 25namespace fair::mq26{27 28using Property = boost::any;29using Properties = std::map<std::string, Property>;30 31struct PropertyChange : Event<std::string> {};32 33class PropertyHelper34{35  public:36    template<typename T>37    static void AddType(std::string label = "")38    {39        if (label.empty()) {40            label = boost::core::demangle(typeid(T).name());41        }42        fTypeInfos[std::type_index(typeid(T))] = [label](const Property& p) {43            std::stringstream ss;44            ss << boost::any_cast<T>(p);45            return std::pair<std::string, std::string>{ss.str(), label};46        };47        fEventEmitters[std::type_index(typeid(T))] = [](const fair::mq::EventManager& em, const std::string& k, const Property& p) {48            em.Emit<PropertyChange, T>(k, boost::any_cast<T>(p));49        };50    }51 52    static std::string ConvertPropertyToString(const Property& p)53    {54        return fTypeInfos.at(p.type())(p).first;55    }56 57    // returns <valueAsString, typenameAsString>58    static std::pair<std::string, std::string> GetPropertyInfo(const Property& p)59    {60        try {61            return fTypeInfos.at(p.type())(p);62        } catch (std::out_of_range& oor) {63            return {"[unidentified_type]", "[unidentified_type]"};64        }65    }66 67    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)68  private:69    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)70};71 72}73 74#endif /* FAIR_MQ_PROPERTIES_H */