FairMQ  1.4.33
C++ Message Queuing Library and Framework
ProgOptions.h
1 /********************************************************************************
2  * Copyright (C) 2014-2018 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 
9 #ifndef FAIR_MQ_PROGOPTIONS_H
10 #define FAIR_MQ_PROGOPTIONS_H
11 
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>
18 
19 #include <boost/program_options.hpp>
20 
21 #include <functional>
22 #include <map>
23 #include <mutex>
24 #include <string>
25 #include <unordered_map>
26 #include <vector>
27 #include <stdexcept>
28 
29 namespace fair::mq
30 {
31 
32 struct PropertyNotFoundError : std::runtime_error { using std::runtime_error::runtime_error; };
33 
34 class ProgOptions
35 {
36  public:
37  ProgOptions();
38  virtual ~ProgOptions() {}
39 
40  void ParseAll(const std::vector<std::string>& cmdArgs, bool allowUnregistered);
41  void ParseAll(const int argc, char const* const* argv, bool allowUnregistered = true);
42  void Notify();
43 
44  void AddToCmdLineOptions(const boost::program_options::options_description optDesc, bool visible = true);
45  boost::program_options::options_description& GetCmdLineOptions();
46 
50  int Count(const std::string& key) const;
51 
54  std::unordered_map<std::string, int> GetChannelInfo() const;
57  std::vector<std::string> GetPropertyKeys() const;
58 
62  template<typename T>
63  T GetProperty(const std::string& key) const
64  {
65  std::lock_guard<std::mutex> lock(fMtx);
66  if (fVarMap.count(key)) {
67  return fVarMap[key].as<T>();
68  } else {
69  throw PropertyNotFoundError(fair::mq::tools::ToString("Config has no key: ", key));
70  }
71  }
72 
77  template<typename T>
78  T GetProperty(const std::string& key, const T& ifNotFound) const
79  {
80  std::lock_guard<std::mutex> lock(fMtx);
81  if (fVarMap.count(key)) {
82  return fVarMap[key].as<T>();
83  }
84  return ifNotFound;
85  }
86 
94  std::string GetPropertyAsString(const std::string& key) const;
103  std::string GetPropertyAsString(const std::string& key, const std::string& ifNotFound) const;
104 
108  fair::mq::Properties GetProperties(const std::string& q) const;
114  fair::mq::Properties GetPropertiesStartingWith(const std::string& q) const;
118  std::map<std::string, std::string> GetPropertiesAsString(const std::string& q) const;
124  std::map<std::string, std::string> GetPropertiesAsStringStartingWith(const std::string& q) const;
125 
129  template<typename T>
130  void SetProperty(const std::string& key, T val)
131  {
132  std::unique_lock<std::mutex> lock(fMtx);
133 
134  SetVarMapValue<typename std::decay<T>::type>(key, val);
135 
136  lock.unlock();
137 
138  fEvents.Emit<fair::mq::PropertyChange, typename std::decay<T>::type>(key, val);
139  fEvents.Emit<fair::mq::PropertyChangeAsString, std::string>(key, GetPropertyAsString(key));
140  }
141 
145  template<typename T>
146  bool UpdateProperty(const std::string& key, T val)
147  {
148  std::unique_lock<std::mutex> lock(fMtx);
149 
150  if (fVarMap.count(key)) {
151  SetVarMapValue<typename std::decay<T>::type>(key, val);
152 
153  lock.unlock();
154 
155  fEvents.Emit<fair::mq::PropertyChange, typename std::decay<T>::type>(key, val);
156  fEvents.Emit<fair::mq::PropertyChangeAsString, std::string>(key, GetPropertyAsString(key));
157  return true;
158  } else {
159  LOG(debug) << "UpdateProperty failed, no property found with key '" << key << "'";
160  return false;
161  }
162  }
163 
166  void SetProperties(const fair::mq::Properties& input);
169  bool UpdateProperties(const fair::mq::Properties& input);
172  void DeleteProperty(const std::string& key);
173 
177  void AddChannel(const std::string& name, const FairMQChannel& channel);
178 
184  template<typename T>
185  void Subscribe(const std::string& subscriber, std::function<void(typename fair::mq::PropertyChange::KeyType, T)> func) const
186  {
187  std::lock_guard<std::mutex> lock(fMtx);
188  static_assert(!std::is_same<T,const char*>::value || !std::is_same<T, char*>::value,
189  "In template member ProgOptions::Subscribe<T>(key,Lambda) the types const char* or char* for the calback signatures are not supported.");
190  fEvents.Subscribe<fair::mq::PropertyChange, T>(subscriber, func);
191  }
192 
195  template<typename T>
196  void Unsubscribe(const std::string& subscriber) const
197  {
198  std::lock_guard<std::mutex> lock(fMtx);
199  fEvents.Unsubscribe<fair::mq::PropertyChange, T>(subscriber);
200  }
201 
207  void SubscribeAsString(const std::string& subscriber, std::function<void(typename fair::mq::PropertyChange::KeyType, std::string)> func) const
208  {
209  std::lock_guard<std::mutex> lock(fMtx);
210  fEvents.Subscribe<fair::mq::PropertyChangeAsString, std::string>(subscriber, func);
211  }
212 
215  void UnsubscribeAsString(const std::string& subscriber) const
216  {
217  std::lock_guard<std::mutex> lock(fMtx);
218  fEvents.Unsubscribe<fair::mq::PropertyChangeAsString, std::string>(subscriber);
219  }
220 
222  void PrintHelp() const;
224  void PrintOptions() const;
226  void PrintOptionsRaw() const;
227 
229  const boost::program_options::variables_map& GetVarMap() const { return fVarMap; }
230 
234  template<typename T>
235  T GetValue(const std::string& key) const /* TODO: deprecate this */
236  {
237  std::lock_guard<std::mutex> lock(fMtx);
238  if (fVarMap.count(key)) {
239  return fVarMap[key].as<T>();
240  } else {
241  LOG(warn) << "Config has no key: " << key << ". Returning default constructed object.";
242  return T();
243  }
244  }
245  template<typename T>
246  int SetValue(const std::string& key, T val) /* TODO: deprecate this */ { SetProperty(key, val); return 0; }
250  std::string GetStringValue(const std::string& key) const; /* TODO: deprecate this */
251 
252  private:
253  void ParseDefaults();
254  std::unordered_map<std::string, int> GetChannelInfoImpl() const;
255 
256  template<typename T>
257  void SetVarMapValue(const std::string& key, const T& val)
258  {
259  std::map<std::string, boost::program_options::variable_value>& vm = fVarMap;
260  vm[key].value() = boost::any(val);
261  }
262 
263  boost::program_options::variables_map fVarMap;
264  boost::program_options::options_description fAllOptions;
265  std::vector<std::string> fUnregisteredOptions;
266 
267  mutable fair::mq::EventManager fEvents;
268  mutable std::mutex fMtx;
269 };
270 
271 } // namespace fair::mq
272 
273 #endif /* FAIR_MQ_PROGOPTIONS_H */
fair::mq::ProgOptions
Definition: ProgOptions.h:41
fair::mq::ProgOptions::GetPropertyKeys
std::vector< std::string > GetPropertyKeys() const
Discover the list of property keys.
Definition: ProgOptions.cxx:189
fair::mq::ProgOptions::GetVarMap
const boost::program_options::variables_map & GetVarMap() const
returns the property container
Definition: ProgOptions.h:235
fair::mq::ProgOptions::GetPropertiesStartingWith
fair::mq::Properties GetPropertiesStartingWith(const std::string &q) const
Read several config properties whose keys start with the provided string.
Definition: ProgOptions.cxx:256
fair::mq::ProgOptions::UpdateProperty
bool UpdateProperty(const std::string &key, T val)
Updates an existing config property (or fails if it doesn't exist)
Definition: ProgOptions.h:152
fair::mq::ProgOptions::Unsubscribe
void Unsubscribe(const std::string &subscriber) const
Unsubscribe from property updates of type T.
Definition: ProgOptions.h:202
fair::mq
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23
fair::mq::ProgOptions::AddChannel
void AddChannel(const std::string &name, const FairMQChannel &channel)
Takes the provided channel and creates properties based on it.
Definition: ProgOptions.cxx:357
fair::mq::ProgOptions::GetProperty
T GetProperty(const std::string &key) const
Read config property, throw if no property with this key exists.
Definition: ProgOptions.h:69
fair::mq::ProgOptions::GetPropertyAsString
std::string GetPropertyAsString(const std::string &key) const
Read config property as string, throw if no property with this key exists.
fair::mq::ProgOptions::SetProperty
void SetProperty(const std::string &key, T val)
Set config property.
Definition: ProgOptions.h:136
fair::mq::ProgOptions::GetPropertiesAsString
std::map< std::string, std::string > GetPropertiesAsString(const std::string &q) const
Read several config properties as string whose keys match the provided regular expression.
Definition: ProgOptions.cxx:271
fair::mq::ProgOptions::PrintHelp
void PrintHelp() const
prints full options description
Definition: ProgOptions.cxx:383
fair::mq::PropertyChange
Definition: Properties.h:37
fair::mq::ProgOptions::SubscribeAsString
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:213
fair::mq::PropertyNotFoundError
Definition: ProgOptions.h:38
fair::mq::EventManager
Manages event callbacks from different subscribers.
Definition: EventManager.h:56
fair::mq::ProgOptions::UpdateProperties
bool UpdateProperties(const fair::mq::Properties &input)
Updates multiple existing config properties (or fails of any of then do not exist,...
Definition: ProgOptions.cxx:323
fair::mq::ProgOptions::Subscribe
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:191
fair::mq::ProgOptions::UnsubscribeAsString
void UnsubscribeAsString(const std::string &subscriber) const
Unsubscribe from property updates that convert to string.
Definition: ProgOptions.h:221
FairMQChannel
Wrapper class for FairMQSocket and related methods.
Definition: FairMQChannel.h:35
fair::mq::ProgOptions::GetPropertiesAsStringStartingWith
std::map< std::string, std::string > GetPropertiesAsStringStartingWith(const std::string &q) const
Read several config properties as string whose keys start with the provided string.
Definition: ProgOptions.cxx:291
fair::mq::ProgOptions::PrintOptionsRaw
void PrintOptionsRaw() const
prints full options description in a compact machine-readable format
Definition: ProgOptions.cxx:432
fair::mq::ProgOptions::SetProperties
void SetProperties(const fair::mq::Properties &input)
Set multiple config properties.
Definition: ProgOptions.cxx:306
fair::mq::ProgOptions::GetProperties
fair::mq::Properties GetProperties(const std::string &q) const
Read several config properties whose keys match the provided regular expression.
Definition: ProgOptions.cxx:236
fair::mq::ProgOptions::Count
int Count(const std::string &key) const
Checks a property with the given key exist in the configuration.
Definition: ProgOptions.cxx:155
fair::mq::ProgOptions::DeleteProperty
void DeleteProperty(const std::string &key)
Deletes a property with the given key from the config store.
Definition: ProgOptions.cxx:349
fair::mq::PropertyChangeAsString
Definition: Properties.h:38
fair::mq::ProgOptions::GetStringValue
std::string GetStringValue(const std::string &key) const
Read config property as string, return default-constructed object if key doesn't exist.
Definition: ProgOptions.cxx:213
fair::mq::ProgOptions::GetValue
T GetValue(const std::string &key) const
Read config property, return default-constructed object if key doesn't exist.
Definition: ProgOptions.h:241
fair::mq::ProgOptions::PrintOptions
void PrintOptions() const
prints properties stored in the property container
Definition: ProgOptions.cxx:388
fair::mq::ProgOptions::GetChannelInfo
std::unordered_map< std::string, int > GetChannelInfo() const
Retrieve current channel information.
Definition: ProgOptions.cxx:161

privacy