FairMQ  1.3.9
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 #include <boost/spirit/home/support/detail/hold_any.hpp>
21 
22 #include <string>
23 #include <vector>
24 #include <iostream>
25 #include <ostream>
26 #include <iterator>
27 #include <typeinfo>
28 
29 namespace fair
30 {
31 namespace mq
32 {
33 
34 template<class T>
35 std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
36 {
37  for (const auto& i : v)
38  {
39  os << i << " ";
40  }
41  return os;
42 }
43 
44 struct VarValInfo
45 {
46  std::string value;
47  std::string type;
48  std::string defaulted;
49 };
50 
51 template<typename T>
52 std::string ConvertVariableValueToString(const boost::program_options::variable_value& varVal)
53 {
54  std::ostringstream oss;
55  if (auto q = boost::any_cast<T>(&varVal.value())) {
56  oss << *q;
57  }
58  return oss.str();
59 }
60 
61 namespace options
62 {
63 
64 // policy to convert boost variable value into string
65 struct ToString
66 {
67  using returned_type = std::string;
68 
69  template<typename T>
70  std::string Value(const boost::program_options::variable_value& varVal, const std::string&, const std::string&)
71  {
72  return ConvertVariableValueToString<T>(varVal);
73  }
74 
75  returned_type DefaultValue(const std::string&)
76  {
77  return std::string("[unidentified]");
78  }
79 };
80 
81 // policy to convert variable value content into VarValInfo
83 {
84  using returned_type = VarValInfo;
85 
86  template<typename T>
87  returned_type Value(const boost::program_options::variable_value& varVal, const std::string& type, const std::string& defaulted)
88  {
89  return VarValInfo{ConvertVariableValueToString<T>(varVal), type, defaulted};
90  }
91 
92  returned_type DefaultValue(const std::string& defaulted)
93  {
94  return VarValInfo{std::string("[unidentified]"), std::string("[unidentified]"), defaulted};
95  }
96 };
97 
98 } // namespace options
99 
100 // host class that take one of the two policy defined above
101 template<typename T>
103 {
104  auto operator()(const boost::program_options::variable_value& varVal) -> typename T::returned_type
105  {
106  std::string defaulted;
107 
108  if (varVal.defaulted())
109  {
110  defaulted = " [default]";
111  }
112  else
113  {
114  defaulted = " [provided]";
115  }
116 
117  if (typeid(std::string) == varVal.value().type())
118  return T::template Value<std::string>(varVal, std::string("<string>"), defaulted);
119 
120  if (typeid(std::vector<std::string>) == varVal.value().type())
121  return T::template Value<std::vector<std::string>>(varVal, std::string("<vector<string>>"), defaulted);
122 
123  if (typeid(int) == varVal.value().type())
124  return T::template Value<int>(varVal, std::string("<int>"), defaulted);
125 
126  if (typeid(std::vector<int>) == varVal.value().type())
127  return T::template Value<std::vector<int>>(varVal, std::string("<vector<int>>"), defaulted);
128 
129  if (typeid(float) == varVal.value().type())
130  return T::template Value<float>(varVal, std::string("<float>"), defaulted);
131 
132  if (typeid(std::vector<float>) == varVal.value().type())
133  return T::template Value<std::vector<float>>(varVal, std::string("<vector<float>>"), defaulted);
134 
135  if (typeid(double) == varVal.value().type())
136  return T::template Value<double>(varVal, std::string("<double>"), defaulted);
137 
138  if (typeid(std::vector<double>) == varVal.value().type())
139  return T::template Value<std::vector<double>>(varVal, std::string("<vector<double>>"), defaulted);
140 
141  if (typeid(short) == varVal.value().type())
142  return T::template Value<short>(varVal, std::string("<short>"), defaulted);
143 
144  if (typeid(std::vector<short>) == varVal.value().type())
145  return T::template Value<std::vector<short>>(varVal, std::string("<vector<short>>"), defaulted);
146 
147  if (typeid(long) == varVal.value().type())
148  return T::template Value<long>(varVal, std::string("<long>"), defaulted);
149 
150  if (typeid(std::vector<long>) == varVal.value().type())
151  return T::template Value<std::vector<long>>(varVal, std::string("<vector<long>>"), defaulted);
152 
153  if (typeid(std::size_t) == varVal.value().type())
154  return T::template Value<std::size_t>(varVal, std::string("<std::size_t>"), defaulted);
155 
156  if (typeid(std::vector<std::size_t>) == varVal.value().type())
157  return T::template Value<std::vector<std::size_t>>(varVal, std::string("<vector<std::size_t>>"), defaulted);
158 
159  if (typeid(std::uint32_t) == varVal.value().type())
160  return T::template Value<std::uint32_t>(varVal, std::string("<std::uint32_t>"), defaulted);
161 
162  if (typeid(std::vector<std::uint32_t>) == varVal.value().type())
163  return T::template Value<std::vector<std::uint32_t>>(varVal, std::string("<vector<std::uint32_t>>"), defaulted);
164 
165  if (typeid(std::uint64_t) == varVal.value().type())
166  return T::template Value<std::uint64_t>(varVal, std::string("<std::uint64_t>"), defaulted);
167 
168  if (typeid(std::vector<std::uint64_t>) == varVal.value().type())
169  return T::template Value<std::vector<std::uint64_t>>(varVal, std::string("<vector<std::uint64_t>>"), defaulted);
170 
171  if (typeid(bool) == varVal.value().type())
172  return T::template Value<bool>(varVal, std::string("<bool>"), defaulted);
173 
174  if (typeid(std::vector<bool>) == varVal.value().type())
175  return T::template Value<std::vector<bool>>(varVal, std::string("<vector<bool>>"), defaulted);
176 
177  if (typeid(boost::filesystem::path) == varVal.value().type())
178  return T::template Value<boost::filesystem::path>(varVal, std::string("<boost::filesystem::path>"), defaulted);
179 
180  if (typeid(std::vector<boost::filesystem::path>) == varVal.value().type())
181  return T::template Value<std::vector<boost::filesystem::path>>(varVal, std::string("<std::vector<boost::filesystem::path>>"), defaulted);
182 
183  // if we get here, the type is not supported return unknown info
184  return T::DefaultValue(defaulted);
185  }
186 };
187 
188 } // namespace mq
189 } // namespace fair
190 
191 #endif /* FAIRPROGOPTIONSHELPER_H */
Definition: FairProgOptionsHelper.h:44
Definition: FairProgOptionsHelper.h:65
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23
Definition: FairProgOptionsHelper.h:82
Definition: FairProgOptionsHelper.h:102

privacy