ros1_foxglove_bridge/src/param_utils.cpp
Go to the documentation of this file.
1 
2 #include <iostream>
3 #include <stdexcept>
4 
5 #include <foxglove_bridge/param_utils.hpp>
6 
7 namespace foxglove_bridge {
8 
10  const auto type = value.getType();
11 
12  if (type == XmlRpc::XmlRpcValue::Type::TypeBoolean) {
13  return foxglove::ParameterValue(static_cast<bool>(value));
14  } else if (type == XmlRpc::XmlRpcValue::Type::TypeInt) {
15  return foxglove::ParameterValue(static_cast<int64_t>(static_cast<int>(value)));
16  } else if (type == XmlRpc::XmlRpcValue::Type::TypeDouble) {
17  return foxglove::ParameterValue(static_cast<double>(value));
18  } else if (type == XmlRpc::XmlRpcValue::Type::TypeString) {
19  return foxglove::ParameterValue(static_cast<std::string>(value));
20  } else if (type == XmlRpc::XmlRpcValue::Type::TypeStruct) {
21  std::unordered_map<std::string, foxglove::ParameterValue> paramMap;
22  for (const auto& [elementName, elementVal] : value) {
23  paramMap.insert({elementName, fromRosParam(elementVal)});
24  }
25  return foxglove::ParameterValue(paramMap);
26  } else if (type == XmlRpc::XmlRpcValue::Type::TypeArray) {
27  std::vector<foxglove::ParameterValue> paramVec;
28  for (int i = 0; i < value.size(); ++i) {
29  paramVec.push_back(fromRosParam(value[i]));
30  }
31  return foxglove::ParameterValue(paramVec);
32  } else if (type == XmlRpc::XmlRpcValue::Type::TypeInvalid) {
33  throw std::runtime_error("Parameter not set");
34  } else {
35  throw std::runtime_error("Unsupported parameter type: " + std::to_string(type));
36  }
37 }
38 
39 foxglove::Parameter fromRosParam(const std::string& name, const XmlRpc::XmlRpcValue& value) {
40  return foxglove::Parameter(name, fromRosParam(value));
41 }
42 
44  const auto paramType = param.getType();
45  if (paramType == foxglove::ParameterType::PARAMETER_BOOL) {
46  return param.getValue<bool>();
47  } else if (paramType == foxglove::ParameterType::PARAMETER_INTEGER) {
48  return static_cast<int>(param.getValue<int64_t>());
49  } else if (paramType == foxglove::ParameterType::PARAMETER_DOUBLE) {
50  return param.getValue<double>();
51  } else if (paramType == foxglove::ParameterType::PARAMETER_STRING) {
52  return param.getValue<std::string>();
53  } else if (paramType == foxglove::ParameterType::PARAMETER_STRUCT) {
54  XmlRpc::XmlRpcValue valueStruct;
55  const auto& paramMap =
56  param.getValue<std::unordered_map<std::string, foxglove::ParameterValue>>();
57  for (const auto& [paramName, paramElement] : paramMap) {
58  valueStruct[paramName] = toRosParam(paramElement);
59  }
60  return valueStruct;
61  } else if (paramType == foxglove::ParameterType::PARAMETER_ARRAY) {
63  const auto vec = param.getValue<std::vector<foxglove::ParameterValue>>();
64  for (int i = 0; i < static_cast<int>(vec.size()); ++i) {
65  arr[i] = toRosParam(vec[i]);
66  }
67  return arr;
68  } else {
69  throw std::runtime_error("Unsupported parameter type");
70  }
71 
72  return XmlRpc::XmlRpcValue();
73 }
74 
75 std::vector<std::regex> parseRegexPatterns(const std::vector<std::string>& patterns) {
76  std::vector<std::regex> result;
77  for (const auto& pattern : patterns) {
78  try {
79  result.push_back(
80  std::regex(pattern, std::regex_constants::ECMAScript | std::regex_constants::icase));
81  } catch (...) {
82  continue;
83  }
84  }
85  return result;
86 }
87 
88 } // namespace foxglove_bridge
ParameterType getType() const
Definition: parameter.hpp:40
XmlRpc::XmlRpcValue toRosParam(const foxglove::ParameterValue &param)
std::vector< std::regex > parseRegexPatterns(const std::vector< std::string > &strings)
Type const & getType() const
foxglove::Parameter fromRosParam(const std::string &name, const XmlRpc::XmlRpcValue &value)
const T & getValue() const
Definition: parameter.hpp:45


foxglove_bridge
Author(s): Foxglove
autogenerated on Mon Jul 3 2023 02:12:22