FairMQ  1.4.14
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
30 {
31 namespace mq
32 {
33 
34 struct PropertyNotFoundError : std::runtime_error { using std::runtime_error::runtime_error; };
35 
37 {
38  public:
39  ProgOptions();
40  virtual ~ProgOptions() {}
41 
42  void ParseAll(const std::vector<std::string>& cmdArgs, bool allowUnregistered);
43  void ParseAll(const int argc, char const* const* argv, bool allowUnregistered = true);
44  void Notify();
45 
46  void AddToCmdLineOptions(const boost::program_options::options_description optDesc, bool visible = true);
47  boost::program_options::options_description& GetCmdLineOptions();
48 
52  int Count(const std::string& key) const;
53 
56  std::unordered_map<std::string, int> GetChannelInfo() const;
59  std::vector<std::string> GetPropertyKeys() const;
60 
64  template<typename T>
65  T GetProperty(const std::string& key) const
66  {
67  std::lock_guard<std::mutex> lock(fMtx);
68  if (fVarMap.count(key)) {
69  return fVarMap[key].as<T>();
70  } else {
71  throw PropertyNotFoundError(fair::mq::tools::ToString("Config has no key: ", key));
72  }
73  }
74 
79  template<typename T>
80  T GetProperty(const std::string& key, const T& ifNotFound) const
81  {
82  std::lock_guard<std::mutex> lock(fMtx);
83  if (fVarMap.count(key)) {
84  return fVarMap[key].as<T>();
85  }
86  return ifNotFound;
87  }
88 
96  std::string GetPropertyAsString(const std::string& key) const;
105  std::string GetPropertyAsString(const std::string& key, const std::string& ifNotFound) const;
106 
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;
127 
131  template<typename T>
132  void SetProperty(const std::string& key, T val)
133  {
134  std::unique_lock<std::mutex> lock(fMtx);
135 
136  SetVarMapValue<typename std::decay<T>::type>(key, val);
137 
138  lock.unlock();
139 
140  fEvents.Emit<fair::mq::PropertyChange, typename std::decay<T>::type>(key, val);
141  fEvents.Emit<fair::mq::PropertyChangeAsString, std::string>(key, GetPropertyAsString(key));
142  }
143 
147  template<typename T>
148  bool UpdateProperty(const std::string& key, T val)
149  {
150  std::unique_lock<std::mutex> lock(fMtx);
151 
152  if (fVarMap.count(key)) {
153  SetVarMapValue<typename std::decay<T>::type>(key, val);
154 
155  lock.unlock();
156 
157  fEvents.Emit<fair::mq::PropertyChange, typename std::decay<T>::type>(key, val);
158  fEvents.Emit<fair::mq::PropertyChangeAsString, std::string>(key, GetPropertyAsString(key));
159  return true;
160  } else {
161  LOG(debug) << "UpdateProperty failed, no property found with key '" << key << "'";
162  return false;
163  }
164  }
165 
168  void SetProperties(const fair::mq::Properties& input);
171  bool UpdateProperties(const fair::mq::Properties& input);
174  void DeleteProperty(const std::string& key);
175 
179  void AddChannel(const std::string& name, const FairMQChannel& channel);
180 
186  template<typename T>
187  void Subscribe(const std::string& subscriber, std::function<void(typename fair::mq::PropertyChange::KeyType, T)> func) const
188  {
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.");
192  fEvents.Subscribe<fair::mq::PropertyChange, T>(subscriber, func);
193  }
194 
197  template<typename T>
198  void Unsubscribe(const std::string& subscriber) const
199  {
200  std::lock_guard<std::mutex> lock(fMtx);
201  fEvents.Unsubscribe<fair::mq::PropertyChange, T>(subscriber);
202  }
203 
209  void SubscribeAsString(const std::string& subscriber, std::function<void(typename fair::mq::PropertyChange::KeyType, std::string)> func) const
210  {
211  std::lock_guard<std::mutex> lock(fMtx);
212  fEvents.Subscribe<fair::mq::PropertyChangeAsString, std::string>(subscriber, func);
213  }
214 
217  void UnsubscribeAsString(const std::string& subscriber) const
218  {
219  std::lock_guard<std::mutex> lock(fMtx);
220  fEvents.Unsubscribe<fair::mq::PropertyChangeAsString, std::string>(subscriber);
221  }
222 
224  void PrintHelp() const;
226  void PrintOptions() const;
228  void PrintOptionsRaw() const;
229 
231  const boost::program_options::variables_map& GetVarMap() const { return fVarMap; }
232 
236  template<typename T>
237  T GetValue(const std::string& key) const /* TODO: deprecate this */
238  {
239  std::lock_guard<std::mutex> lock(fMtx);
240  if (fVarMap.count(key)) {
241  return fVarMap[key].as<T>();
242  } else {
243  LOG(warn) << "Config has no key: " << key << ". Returning default constructed object.";
244  return T();
245  }
246  }
247  template<typename T>
248  int SetValue(const std::string& key, T val) /* TODO: deprecate this */ { SetProperty(key, val); return 0; }
252  std::string GetStringValue(const std::string& key) const; /* TODO: deprecate this */
253 
254  private:
255  void ParseDefaults();
256  std::unordered_map<std::string, int> GetChannelInfoImpl() const;
257 
258  template<typename T>
259  void SetVarMapValue(const std::string& key, const T& val)
260  {
261  std::map<std::string, boost::program_options::variable_value>& vm = fVarMap;
262  vm[key].value() = boost::any(val);
263  }
264 
265  boost::program_options::variables_map fVarMap;
266  boost::program_options::options_description fAllOptions;
267  std::vector<std::string> fUnregisteredOptions;
268 
269  mutable fair::mq::EventManager fEvents;
270  mutable std::mutex fMtx;
271 };
272 
273 } // namespace mq
274 } // namespace fair
275 
276 #endif /* FAIR_MQ_PROGOPTIONS_H */
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&#39;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&#39;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

privacy