9 #ifndef FAIRMQPROGOPTIONS_H 10 #define FAIRMQPROGOPTIONS_H 12 #include <fairmq/EventManager.h> 13 #include "FairMQLogger.h" 14 #include "FairMQChannel.h" 15 #include <fairmq/Tools.h> 17 #include <boost/program_options.hpp> 19 #include <unordered_map> 40 using FairMQChannelMap = std::unordered_map<std::string, std::vector<FairMQChannel>>;
46 int ParseAll(
const std::vector<std::string>& cmdLineArgs,
bool allowUnregistered);
49 int ParseAll(
const int argc,
char const*
const* argv,
bool allowUnregistered =
true);
51 FairMQChannelMap GetFairMQMap()
const;
52 std::unordered_map<std::string, int> GetChannelInfo()
const;
55 int SetValue(
const std::string& key, T val)
57 std::unique_lock<std::mutex> lock(fConfigMutex);
60 UpdateVarMap<typename std::decay<T>::type>(key, val);
62 if (key ==
"channel-config")
64 ParseChannelsFromCmdLine();
66 else if (fChannelKeyMap.count(key))
68 UpdateChannelValue(fChannelKeyMap.at(key).channel, fChannelKeyMap.at(key).index, fChannelKeyMap.at(key).member, val);
81 void Subscribe(
const std::string& subscriber, std::function<
void(
typename fair::mq::PropertyChange::KeyType, T)> func)
83 std::unique_lock<std::mutex> lock(fConfigMutex);
85 static_assert(!std::is_same<T,const char*>::value || !std::is_same<T, char*>::value,
86 "In template member FairMQProgOptions::Subscribe<T>(key,Lambda) the types const char* or char* for the calback signatures are not supported.");
92 void Unsubscribe(
const std::string& subscriber)
94 std::unique_lock<std::mutex> lock(fConfigMutex);
99 void SubscribeAsString(
const std::string& subscriber, std::function<
void(
typename fair::mq::PropertyChange::KeyType, std::string)> func)
101 std::unique_lock<std::mutex> lock(fConfigMutex);
106 void UnsubscribeAsString(
const std::string& subscriber)
108 std::unique_lock<std::mutex> lock(fConfigMutex);
113 std::vector<std::string> GetPropertyKeys()
const;
117 T GetValue(
const std::string& key)
const 119 std::unique_lock<std::mutex> lock(fConfigMutex);
123 if (fVarMap.count(key))
125 val = fVarMap[key].as<T>();
129 LOG(warn) <<
"Config has no key: " << key <<
". Returning default constructed object.";
136 std::string GetStringValue(
const std::string& key);
138 int Count(
const std::string& key)
const;
141 T ConvertTo(
const std::string& strValue)
143 if (std::is_arithmetic<T>::value)
145 std::istringstream iss(strValue);
152 LOG(error) <<
"the provided string " << strValue <<
" cannot be converted to the requested type. The target type must be arithmetic type.";
157 int AddToCmdLineOptions(
const boost::program_options::options_description optDesc,
bool visible =
true);
158 boost::program_options::options_description& GetCmdLineOptions();
160 const boost::program_options::variables_map& GetVarMap()
const {
return fVarMap; }
163 int PrintOptionsRaw();
165 void AddChannel(
const std::string& channelName,
const FairMQChannel& channel)
167 fFairMQChannelMap[channelName].push_back(channel);
178 boost::program_options::variables_map fVarMap;
179 FairMQChannelMap fFairMQChannelMap;
181 boost::program_options::options_description fAllOptions;
182 boost::program_options::options_description fGeneralOptions;
183 boost::program_options::options_description fMQOptions;
184 boost::program_options::options_description fParserOptions;
186 mutable std::mutex fConfigMutex;
188 std::unordered_map<std::string, int> fChannelInfo;
189 std::unordered_map<std::string, ChannelKey> fChannelKeyMap;
190 std::vector<std::string> fUnregisteredOptions;
194 void ParseCmdLine(
const int argc,
char const*
const* argv,
bool allowUnregistered =
true);
195 void ParseDefaults();
199 void UpdateMQValues();
200 int Store(
const FairMQChannelMap& channels);
203 void EmitUpdate(
const std::string& key, T val)
206 static_assert(!std::is_same<T,const char*>::value || !std::is_same<T, char*>::value,
207 "In template member FairMQProgOptions::EmitUpdate<T>(key,val) the types const char* or char* for the calback signatures are not supported.");
212 int UpdateChannelMap(
const FairMQChannelMap& map);
214 int UpdateChannelValue(
const std::string&,
int,
const std::string&, T)
216 LOG(error) <<
"update of FairMQChannel map failed, because value type not supported";
219 int UpdateChannelValue(
const std::string& channelName,
int index,
const std::string& member,
const std::string& val);
220 int UpdateChannelValue(
const std::string& channelName,
int index,
const std::string& member,
int val);
221 int UpdateChannelValue(
const std::string& channelName,
int index,
const std::string& member,
bool val);
223 void UpdateChannelInfo();
227 void UpdateVarMap(
const std::string& key,
const T& val)
229 std::map<std::string, boost::program_options::variable_value>& vm = fVarMap;
230 vm[key].value() = boost::any(val);
233 void ParseChannelsFromCmdLine();
Definition: EventManager.h:33
Manages event callbacks from different subscribers.
Definition: EventManager.h:53
Definition: FairMQChannel.h:27
Definition: FairMQProgOptions.h:37
Definition: FairMQProgOptions.h:32
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23
Definition: FairMQProgOptions.h:31