param_utils.h
Go to the documentation of this file.
1 // Utilities for checking params
2 // Author: Max Schwarz <max.schwarz@ais.uni-bonn.de>
3 
4 #ifndef PARAM_UTILS_H
5 #define PARAM_UTILS_H
6 
7 #include <catch_ros/catch.hpp>
8 
9 #include <XmlRpcValue.h>
10 
11 typedef std::map<std::string, XmlRpc::XmlRpcValue> ParameterMap;
12 
13 namespace Catch
14 {
15  template<>
16  struct StringMaker<XmlRpc::XmlRpcValue::Type>
17  {
18  static std::string convert(const XmlRpc::XmlRpcValue::Type& value)
19  {
20  switch(value)
21  {
22  case XmlRpc::XmlRpcValue::TypeInvalid: return "TypeInvalid";
23  case XmlRpc::XmlRpcValue::TypeBoolean: return "TypeBoolean";
24  case XmlRpc::XmlRpcValue::TypeInt: return "TypeInt";
25  case XmlRpc::XmlRpcValue::TypeDouble: return "TypeDouble";
26  case XmlRpc::XmlRpcValue::TypeString: return "TypeString";
27  case XmlRpc::XmlRpcValue::TypeDateTime: return "TypeDateTime";
28  case XmlRpc::XmlRpcValue::TypeBase64: return "TypeBase64";
29  case XmlRpc::XmlRpcValue::TypeArray: return "TypeArray";
30  case XmlRpc::XmlRpcValue::TypeStruct: return "TypeStruct";
31  default: return "unknown type";
32  }
33  }
34  };
35 
36  template<>
37  struct StringMaker<XmlRpc::XmlRpcValue>
38  {
39  static std::string convert(const XmlRpc::XmlRpcValue& value)
40  {
41  // We need a copy, since the cast operators don't work on const
42  // refs *sigh*
43  XmlRpc::XmlRpcValue copy = value;
44 
45  std::stringstream ss;
46  ss << "XmlRpc";
47  switch(value.getType())
48  {
50  ss << "<int>(" << static_cast<int>(copy) << ")";
51  break;
53  ss << "<bool>(" << static_cast<bool>(copy) << ")";
54  break;
56  ss << "<string>('" << static_cast<std::string>(copy) << "')";
57  break;
59  ss << "<double>(" << static_cast<double>(copy) << ")";
60  break;
61  default:
62  ss << "<"
64  << ">(?)";
65  break;
66  }
67 
68  return ss.str();
69  }
70  };
71 
72  template<>
73  struct StringMaker<ParameterMap>
74  {
75  static std::string convert(const ParameterMap& value)
76  {
77  std::stringstream ss;
78  ss << "{";
79  for(auto& param : value)
80  {
81  ss << "\"" << param.first << "\"=" << StringMaker<XmlRpc::XmlRpcValue>::convert(param.second) << ", ";
82  }
83  ss << "}";
84 
85  return ss.str();
86  }
87  };
88 }
89 
90 template<class T>
91 void checkTypedParam(const ParameterMap& parameters, const std::string& name, XmlRpc::XmlRpcValue::Type expectedType, T expected)
92 {
93  CAPTURE(parameters);
94  CAPTURE(name);
95 
96  auto it = parameters.find(name);
97  REQUIRE(it != parameters.end());
98 
99  XmlRpc::XmlRpcValue value = it->second;
100 
101  REQUIRE(value.getType() == expectedType);
102 
103  T typedValue = value;
104 
105  REQUIRE(typedValue == expected);
106 }
107 
108 template<class T>
109 T getTypedParam(const ParameterMap& parameters, const std::string& name)
110 {
111  CAPTURE(parameters);
112  CAPTURE(name);
113 
114  auto it = parameters.find(name);
115  REQUIRE(it != parameters.end());
116 
117  XmlRpc::XmlRpcValue value = it->second;
118 
119  T typedValue = value;
120 
121  return typedValue;
122 }
123 
124 template<class T>
125 T getTypedParam(const ParameterMap& parameters, const std::string& name, XmlRpc::XmlRpcValue::Type expectedType)
126 {
127  CAPTURE(parameters);
128  CAPTURE(name);
129 
130  auto it = parameters.find(name);
131  REQUIRE(it != parameters.end());
132 
133  XmlRpc::XmlRpcValue value = it->second;
134 
135  REQUIRE(value.getType() == expectedType);
136 
137  T typedValue = value;
138 
139  return typedValue;
140 }
141 
142 #endif
void checkTypedParam(const ParameterMap &parameters, const std::string &name, XmlRpc::XmlRpcValue::Type expectedType, T expected)
Definition: param_utils.h:91
bool param(const std::string &param_name, T &param_val, const T &default_val)
Type const & getType() const
T getTypedParam(const ParameterMap &parameters, const std::string &name)
Definition: param_utils.h:109
static std::string convert(const ParameterMap &value)
Definition: param_utils.h:75
static std::string convert(const XmlRpc::XmlRpcValue &value)
Definition: param_utils.h:39
std::map< std::string, XmlRpc::XmlRpcValue > ParameterMap
Definition: param_utils.h:11
static std::string convert(const XmlRpc::XmlRpcValue::Type &value)
Definition: param_utils.h:18


rosmon_core
Author(s): Max Schwarz
autogenerated on Wed Jul 10 2019 03:10:12