FairMQ  1.2.0
C++ Message Passing Framework
FairProgOptionsHelper.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: FairProgOptionsHelper.h
10  * Author: winckler
11  *
12  * Created on March 11, 2015, 5:38 PM
13  */
14 
15 #ifndef FAIRPROGOPTIONSHELPER_H
16 #define FAIRPROGOPTIONSHELPER_H
17 
18 #include <boost/program_options.hpp>
19 #include <boost/filesystem.hpp>
20 
21 #include <string>
22 #include <vector>
23 #include <iostream>
24 #include <ostream>
25 #include <iterator>
26 
27 namespace fair
28 {
29 namespace mq
30 {
31 
32 struct VarValInfo
33 {
34  std::string value;
35  std::string type;
36  std::string defaulted;
37  std::string empty;
38 };
39 
40 template<class T>
41 std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
42 {
43  std::copy(v.begin(), v.end(), std::ostream_iterator<T>(os, " "));
44  return os;
45 }
46 
47 template<typename T>
48 bool typeIs(const boost::program_options::variable_value& varValue)
49 {
50  auto& value = varValue.value();
51  if (auto q = boost::any_cast<T>(&value))
52  {
53  return true;
54  }
55  else
56  {
57  return false;
58  }
59 }
60 
61 template<typename T>
62 std::string ConvertVariableValueToString(const boost::program_options::variable_value& varValue)
63 {
64  auto& value = varValue.value();
65  std::ostringstream ostr;
66  if (auto q = boost::any_cast<T>(&value))
67  {
68  ostr << *q;
69  }
70  std::string valueStr = ostr.str();
71  return valueStr;
72 }
73 
74 // string specialization
75 template<>
76 inline std::string ConvertVariableValueToString<std::string>(const boost::program_options::variable_value& varValue)
77 {
78  auto& value = varValue.value();
79  std::string valueStr;
80  if (auto q = boost::any_cast<std::string>(&value))
81  {
82  valueStr = *q;
83  }
84  return valueStr;
85 }
86 
87 // boost::filesystem::path specialization
88 template<>
89 inline std::string ConvertVariableValueToString<boost::filesystem::path>(const boost::program_options::variable_value& varValue)
90 {
91  auto& value = varValue.value();
92  std::string valueStr;
93  if (auto q = boost::any_cast<boost::filesystem::path>(&value))
94  {
95  valueStr = (*q).string();
96  }
97  return valueStr;
98 }
99 
100 // policy to convert boost variable value into string
102 {
103  using returned_type = std::string;
104 
105  template<typename T>
106  std::string Value(const boost::program_options::variable_value& varValue, const std::string&, const std::string&, const std::string&)
107  {
108  return ConvertVariableValueToString<T>(varValue);
109  }
110 
111  returned_type DefaultValue(const std::string&, const std::string&)
112  {
113  return std::string("empty value");
114  }
115 };
116 
117 // policy to convert variable value content into VarValInfo
119 {
121 
122  template<typename T>
123  returned_type Value(const boost::program_options::variable_value& varValue, const std::string& type, const std::string& defaulted, const std::string& empty)
124  {
125  std::string valueStr = ConvertVariableValueToString<T>(varValue);
126  return fair::mq::VarValInfo{valueStr, type, defaulted, empty};
127  }
128 
129  returned_type DefaultValue(const std::string& defaulted, const std::string& empty)
130  {
131  return fair::mq::VarValInfo{std::string("Unknown value"), std::string(" [Unknown]"), defaulted, empty};
132  }
133 };
134 
135 // host class that take one of the two policy defined above
136 template<typename T>
138 {
139  auto operator()(const boost::program_options::variable_value& varValue) -> typename T::returned_type
140  {
141  std::string defaulted;
142  std::string empty;
143 
144  if (varValue.empty())
145  {
146  empty = " [empty]";
147  }
148  else
149  {
150  if (varValue.defaulted())
151  {
152  defaulted = " [default]";
153  }
154  else
155  {
156  defaulted = " [provided]";
157  }
158  }
159 
160  if (typeIs<std::string>(varValue))
161  return T::template Value<std::string>(varValue, std::string("<string>"), defaulted, empty);
162 
163  if (typeIs<std::vector<std::string>>(varValue))
164  return T::template Value<std::vector<std::string>>(varValue, std::string("<vector<string>>"), defaulted, empty);
165 
166  if (typeIs<int>(varValue))
167  return T::template Value<int>(varValue, std::string("<int>"), defaulted, empty);
168 
169  if (typeIs<std::vector<int>>(varValue))
170  return T::template Value<std::vector<int>>(varValue, std::string("<vector<int>>"), defaulted, empty);
171 
172  if (typeIs<float>(varValue))
173  return T::template Value<float>(varValue, std::string("<float>"), defaulted, empty);
174 
175  if (typeIs<std::vector<float>>(varValue))
176  return T::template Value<std::vector<float>>(varValue, std::string("<vector<float>>"), defaulted, empty);
177 
178  if (typeIs<double>(varValue))
179  return T::template Value<double>(varValue, std::string("<double>"), defaulted, empty);
180 
181  if (typeIs<std::vector<double>>(varValue))
182  return T::template Value<std::vector<double>>(varValue, std::string("<vector<double>>"), defaulted, empty);
183 
184  if (typeIs<short>(varValue))
185  return T::template Value<short>(varValue, std::string("<short>"), defaulted, empty);
186 
187  if (typeIs<std::vector<short>>(varValue))
188  return T::template Value<std::vector<short>>(varValue, std::string("<vector<short>>"), defaulted, empty);
189 
190  if (typeIs<long>(varValue))
191  return T::template Value<long>(varValue, std::string("<long>"), defaulted, empty);
192 
193  if (typeIs<std::vector<long>>(varValue))
194  return T::template Value<std::vector<long>>(varValue, std::string("<vector<long>>"), defaulted, empty);
195 
196  if (typeIs<std::size_t>(varValue))
197  return T::template Value<std::size_t>(varValue, std::string("<std::size_t>"), defaulted, empty);
198 
199  if (typeIs<std::vector<std::size_t>>(varValue))
200  return T::template Value<std::vector<std::size_t>>(varValue, std::string("<vector<std::size_t>>"), defaulted, empty);
201 
202  if (typeIs<std::uint32_t>(varValue))
203  return T::template Value<std::uint32_t>(varValue, std::string("<std::uint32_t>"), defaulted, empty);
204 
205  if (typeIs<std::vector<std::uint32_t>>(varValue))
206  return T::template Value<std::vector<std::uint32_t>>(varValue, std::string("<vector<std::uint32_t>>"), defaulted, empty);
207 
208  if (typeIs<std::uint64_t>(varValue))
209  return T::template Value<std::uint64_t>(varValue, std::string("<std::uint64_t>"), defaulted, empty);
210 
211  if (typeIs<std::vector<std::uint64_t>>(varValue))
212  return T::template Value<std::vector<std::uint64_t>>(varValue, std::string("<vector<std::uint64_t>>"), defaulted, empty);
213 
214  if (typeIs<bool>(varValue))
215  return T::template Value<bool>(varValue, std::string("<bool>"), defaulted, empty);
216 
217  if (typeIs<std::vector<bool>>(varValue))
218  return T::template Value<std::vector<bool>>(varValue, std::string("<vector<bool>>"), defaulted, empty);
219 
220  if (typeIs<boost::filesystem::path>(varValue))
221  return T::template Value<boost::filesystem::path>(varValue, std::string("<boost::filesystem::path>"), defaulted, empty);
222 
223  // if we get here, the type is not supported return unknown info
224  return T::DefaultValue(defaulted, empty);
225  }
226 };
227 
228 } // namespace mq
229 } // namespace fair
230 
231 #endif /* FAIRPROGOPTIONSHELPER_H */
Definition: FairProgOptionsHelper.h:32
Definition: FairProgOptionsHelper.h:118
Definition: DeviceRunner.h:23
Definition: FairProgOptionsHelper.h:101
Definition: FairProgOptionsHelper.h:137