/******************************************************************************** * Copyright (C) 2014 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" * ********************************************************************************/ /* * File: FairMQProgOptions.h * Author: winckler * * Created on March 11, 2015, 10:20 PM */ #ifndef FAIRMQPROGOPTIONS_H #define FAIRMQPROGOPTIONS_H #include #include "FairProgOptions.h" #include "FairMQChannel.h" #include #include #include #include #include namespace fair { namespace mq { struct PropertyChange : Event {}; struct PropertyChangeAsString : Event {}; } /* namespace mq */ } /* namespace fair */ class FairMQProgOptions : public FairProgOptions { protected: using FairMQMap = std::unordered_map>; public: FairMQProgOptions(); virtual ~FairMQProgOptions(); int ParseAll(const std::vector& cmdLineArgs, bool allowUnregistered); // parse command line. // default parser for the mq-configuration file (JSON/XML) is called if command line key mq-config is called int ParseAll(const int argc, char const* const* argv, bool allowUnregistered = false) override; FairMQMap GetFairMQMap() const { return fFairMQMap; } std::unordered_map GetChannelInfo() const { return fChannelInfo; } template int UpdateValue(const std::string& key, T val) { std::unique_lock lock(fConfigMutex); if (fVarMap.count(key)) { // update variable map UpdateVarMap::type>(key, val); // update FairMQChannel map, check first if data are int or string if (std::is_same::value || std::is_same::value) { if (fMQKeyMap.count(key)) { UpdateChannelMap(fMQKeyMap.at(key).channel, fMQKeyMap.at(key).index, fMQKeyMap.at(key).member, val); } } lock.unlock(); //if (std::is_same::value || std::is_same::value)//if one wants to restrict type fEvents.Emit::type>(key, val); fEvents.Emit(key, GetStringValue(key)); return 0; } else { LOG(error) << "UpdateValue failed: key '" << key << "' not found in the variable map"; return 1; } return 0; } template int SetValue(const std::string& key, T val) { std::unique_lock lock(fConfigMutex); // update variable map UpdateVarMap::type>(key, val); // update FairMQChannel map, check first if data are int or string if (std::is_same::value || std::is_same::value) { if (fMQKeyMap.count(key)) { UpdateChannelMap(fMQKeyMap.at(key).channel, fMQKeyMap.at(key).index, fMQKeyMap.at(key).member, val); } } lock.unlock(); //if (std::is_same::value || std::is_same::value)//if one wants to restrict type fEvents.Emit::type>(key, val); fEvents.Emit(key, GetStringValue(key)); return 0; } template void Subscribe(const std::string& subscriber, std::function func) { std::unique_lock lock(fConfigMutex); static_assert(!std::is_same::value || !std::is_same::value, "In template member FairMQProgOptions::Subscribe(key,Lambda) the types const char* or char* for the calback signatures are not supported."); fEvents.Subscribe(subscriber, func); } template void Unsubscribe(const std::string& subscriber) { std::unique_lock lock(fConfigMutex); fEvents.Unsubscribe(subscriber); } void SubscribeAsString(const std::string& subscriber, std::function func) { std::unique_lock lock(fConfigMutex); fEvents.Subscribe(subscriber, func); } void UnsubscribeAsString(const std::string& subscriber) { std::unique_lock lock(fConfigMutex); fEvents.Unsubscribe(subscriber); } // replace FairMQChannelMap, and update variable map accordingly int UpdateChannelMap(const FairMQMap& map); protected: struct MQKey { std::string channel; int index; std::string member; }; po::options_description fMQCmdOptions; po::options_description fMQParserOptions; FairMQMap fFairMQMap; // map of read channel info - channel name - number of subchannels std::unordered_map fChannelInfo; std::map fMQKeyMap;// key=full path - val=key info int ImmediateOptions() override; // for custom help & version printing void InitOptionDescription(); // read FairMQChannelMap and insert/update corresponding values in variable map // create key for variable map as follow : channelName.index.memberName void UpdateMQValues(); int Store(const FairMQMap& channels); private: template void EmitUpdate(const std::string& key, T val) { //compile time check whether T is const char* or char*, and in that case a compile time error is thrown. static_assert(!std::is_same::value || !std::is_same::value, "In template member FairMQProgOptions::EmitUpdate(key,val) the types const char* or char* for the calback signatures are not supported."); fEvents.Emit(key, val); fEvents.Emit(key, GetStringValue(key)); } int UpdateChannelMap(const std::string& channelName, int index, const std::string& member, const std::string& val); int UpdateChannelMap(const std::string& channelName, int index, const std::string& member, int val); // for cases other than int and string template int UpdateChannelMap(const std::string& /*channelName*/, int /*index*/, const std::string& /*member*/, T /*val*/) { return 0; } void UpdateChannelInfo(); fair::mq::EventManager fEvents; }; #endif /* FAIRMQPROGOPTIONS_H */