9 #ifndef FAIR_MQ_PROGOPTIONS_H 10 #define FAIR_MQ_PROGOPTIONS_H 12 #include "FairMQChannel.h" 13 #include "FairMQLogger.h" 14 #include <fairmq/EventManager.h> 15 #include <fairmq/ProgOptionsFwd.h> 16 #include <fairmq/Properties.h> 17 #include <fairmq/tools/Strings.h> 19 #include <boost/program_options.hpp> 25 #include <unordered_map> 42 void ParseAll(
const std::vector<std::string>& cmdArgs,
bool allowUnregistered);
43 void ParseAll(
const int argc,
char const*
const* argv,
bool allowUnregistered =
true);
46 void AddToCmdLineOptions(
const boost::program_options::options_description optDesc,
bool visible =
true);
47 boost::program_options::options_description& GetCmdLineOptions();
52 int Count(
const std::string& key)
const;
56 std::unordered_map<std::string, int> GetChannelInfo()
const;
59 std::vector<std::string> GetPropertyKeys()
const;
67 std::lock_guard<std::mutex> lock(fMtx);
68 if (fVarMap.count(key)) {
69 return fVarMap[key].as<T>();
80 T
GetProperty(
const std::string& key,
const T& ifNotFound)
const 82 std::lock_guard<std::mutex> lock(fMtx);
83 if (fVarMap.count(key)) {
84 return fVarMap[key].as<T>();
96 std::string GetPropertyAsString(
const std::string& key)
const;
105 std::string GetPropertyAsString(
const std::string& key,
const std::string& ifNotFound)
const;
110 fair::mq::Properties GetProperties(
const std::string& q)
const;
116 fair::mq::Properties GetPropertiesStartingWith(
const std::string& q)
const;
120 std::map<std::string, std::string> GetPropertiesAsString(
const std::string& q)
const;
126 std::map<std::string, std::string> GetPropertiesAsStringStartingWith(
const std::string& q)
const;
134 std::unique_lock<std::mutex> lock(fMtx);
136 SetVarMapValue<typename std::decay<T>::type>(key, val);
150 std::unique_lock<std::mutex> lock(fMtx);
152 if (fVarMap.count(key)) {
153 SetVarMapValue<typename std::decay<T>::type>(key, val);
161 LOG(debug) <<
"UpdateProperty failed, no property found with key '" << key <<
"'";
168 void SetProperties(
const fair::mq::Properties& input);
171 bool UpdateProperties(
const fair::mq::Properties& input);
174 void DeleteProperty(
const std::string& key);
179 void AddChannel(
const std::string& name,
const FairMQChannel& channel);
187 void Subscribe(
const std::string& subscriber, std::function<
void(
typename fair::mq::PropertyChange::KeyType, T)> func)
const 189 std::lock_guard<std::mutex> lock(fMtx);
190 static_assert(!std::is_same<T,const char*>::value || !std::is_same<T, char*>::value,
191 "In template member ProgOptions::Subscribe<T>(key,Lambda) the types const char* or char* for the calback signatures are not supported.");
200 std::lock_guard<std::mutex> lock(fMtx);
209 void SubscribeAsString(
const std::string& subscriber, std::function<
void(
typename fair::mq::PropertyChange::KeyType, std::string)> func)
const 211 std::lock_guard<std::mutex> lock(fMtx);
219 std::lock_guard<std::mutex> lock(fMtx);
224 void PrintHelp()
const;
226 void PrintOptions()
const;
228 void PrintOptionsRaw()
const;
231 const boost::program_options::variables_map&
GetVarMap()
const {
return fVarMap; }
239 std::lock_guard<std::mutex> lock(fMtx);
240 if (fVarMap.count(key)) {
241 return fVarMap[key].as<T>();
243 LOG(warn) <<
"Config has no key: " << key <<
". Returning default constructed object.";
248 int SetValue(
const std::string& key, T val) { SetProperty(key, val);
return 0; }
252 std::string GetStringValue(
const std::string& key)
const;
255 void ParseDefaults();
256 std::unordered_map<std::string, int> GetChannelInfoImpl()
const;
259 void SetVarMapValue(
const std::string& key,
const T& val)
261 std::map<std::string, boost::program_options::variable_value>& vm = fVarMap;
262 vm[key].value() = boost::any(val);
265 boost::program_options::variables_map fVarMap;
266 boost::program_options::options_description fAllOptions;
267 std::vector<std::string> fUnregisteredOptions;
270 mutable std::mutex fMtx;
T GetProperty(const std::string &key) const
Read config property, throw if no property with this key exists.
Definition: ProgOptions.h:65
void Subscribe(const std::string &subscriber, std::function< void(typename fair::mq::PropertyChange::KeyType, T)> func) const
Subscribe to property updates of type T.
Definition: ProgOptions.h:187
T GetValue(const std::string &key) const
Read config property, return default-constructed object if key doesn't exist.
Definition: ProgOptions.h:237
T GetProperty(const std::string &key, const T &ifNotFound) const
Read config property, return provided value if no property with this key exists.
Definition: ProgOptions.h:80
void SubscribeAsString(const std::string &subscriber, std::function< void(typename fair::mq::PropertyChange::KeyType, std::string)> func) const
Subscribe to property updates, with values converted to string.
Definition: ProgOptions.h:209
void Unsubscribe(const std::string &subscriber) const
Unsubscribe from property updates of type T.
Definition: ProgOptions.h:198
Manages event callbacks from different subscribers.
Definition: EventManager.h:51
Definition: ProgOptions.h:34
Definition: FairMQChannel.h:30
bool UpdateProperty(const std::string &key, T val)
Updates an existing config property (or fails if it doesn't exist)
Definition: ProgOptions.h:148
Definition: ProgOptions.h:36
void SetProperty(const std::string &key, T val)
Set config property.
Definition: ProgOptions.h:132
void UnsubscribeAsString(const std::string &subscriber) const
Unsubscribe from property updates that convert to string.
Definition: ProgOptions.h:217
Definition: Properties.h:33
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23
const boost::program_options::variables_map & GetVarMap() const
returns the property container
Definition: ProgOptions.h:231
Definition: Properties.h:32