FairMQ  1.2.0
C++ Message Passing Framework
FairProgOptions.h
1 /********************************************************************************
2  * Copyright (C) 2014 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  * File: FairProgOptions.h
10  * Author: winckler
11  *
12  * Created on March 11, 2015, 5:38 PM
13  */
14 
15 #ifndef FAIRPROGOPTIONS_H
16 #define FAIRPROGOPTIONS_H
17 
18 #include "FairMQLogger.h"
19 #include "FairProgOptionsHelper.h"
20 #include <fairmq/Tools.h>
21 
22 #include <boost/program_options.hpp>
23 #include <boost/filesystem.hpp>
24 
25 #include <string>
26 #include <vector>
27 #include <iostream>
28 #include <fstream>
29 #include <mutex>
30 #include <exception>
31 
32 namespace po = boost::program_options;
33 namespace fs = boost::filesystem;
34 
36 {
37  public:
39  virtual ~FairProgOptions();
40 
41  auto GetPropertyKeys() const -> std::vector<std::string>
42  {
43  std::lock_guard<std::mutex> lock{fConfigMutex};
44 
45  std::vector<std::string> result;
46 
47  for (const auto& it : fVarMap)
48  {
49  result.push_back(it.first.c_str());
50  }
51 
52  return result;
53  }
54 
55  // add options_description
56  int AddToCmdLineOptions(const po::options_description optDesc, bool visible = true);
57  po::options_description& GetCmdLineOptions();
58 
59  // get value corresponding to the key
60  template<typename T>
61  T GetValue(const std::string& key) const
62  {
63  std::unique_lock<std::mutex> lock(fConfigMutex);
64 
65  T val = T();
66 
67  if (fVarMap.count(key))
68  {
69  val = fVarMap[key].as<T>();
70  }
71  else
72  {
73  LOG(warn) << "Config has no key: " << key << ". Returning default constructed object.";
74  }
75 
76  return val;
77  }
78 
79  // Given a key, convert the variable value to string
80  std::string GetStringValue(const std::string& key)
81  {
82  std::unique_lock<std::mutex> lock(fConfigMutex);
83 
84  std::string valueStr;
85  try
86  {
87  if (fVarMap.count(key))
88  {
90  }
91  }
92  catch (std::exception& e)
93  {
94  LOG(error) << "Exception thrown for the key '" << key << "'";
95  LOG(error) << e.what();
96  }
97 
98  return valueStr;
99  }
100 
101  int Count(const std::string& key) const
102  {
103  std::unique_lock<std::mutex> lock(fConfigMutex);
104 
105  return fVarMap.count(key);
106  }
107 
108  //restrict conversion to fundamental types
109  template<typename T>
110  T ConvertTo(const std::string& strValue)
111  {
112  if (std::is_arithmetic<T>::value)
113  {
114  std::istringstream iss(strValue);
115  T val;
116  iss >> val;
117  return val;
118  }
119  else
120  {
121  LOG(error) << "the provided string " << strValue << " cannot be converted in the requested type. The target types must be arithmetic types";
122  }
123  }
124 
125  const po::variables_map& GetVarMap() const { return fVarMap; }
126 
127  int ParseCmdLine(const int argc, char const* const* argv, bool allowUnregistered = false);
128  void ParseDefaults();
129 
130  virtual int ParseAll(const int argc, char const* const* argv, bool allowUnregistered = false) = 0;
131 
132  virtual int PrintOptions();
133  virtual int PrintOptionsRaw();
134 
135  protected:
136  // options container
137  po::variables_map fVarMap;
138 
139  // options descriptions
140  po::options_description fGeneralOptions;
141  po::options_description fAllOptions;
142 
143  mutable std::mutex fConfigMutex;
144 
145  virtual int ImmediateOptions() = 0;
146 
147  // UpdateVarMap() and Replace() --> helper functions to modify the value of variable map after calling po::store
148  template<typename T>
149  void UpdateVarMap(const std::string& key, const T& val)
150  {
151  Replace(fVarMap, key, val);
152  }
153 
154  template<typename T>
155  void Replace(std::map<std::string, po::variable_value>& vm, const std::string& key, const T& val)
156  {
157  vm[key].value() = boost::any(val);
158  }
159 
160  private:
161  fair::mq::VarValInfo GetVariableValueInfo(const po::variable_value& varValue);
162 
163  static void Max(int& val, const int& comp)
164  {
165  if (comp > val)
166  {
167  val = comp;
168  }
169  }
170 };
171 
172 #endif /* FAIRPROGOPTIONS_H */
Definition: FairProgOptionsHelper.h:32
Definition: FairProgOptions.h:35
int AddToCmdLineOptions(const po::options_description optDesc, bool visible=true)
Add option descriptions.
Definition: FairProgOptions.cxx:47
Definition: FairProgOptionsHelper.h:137