tools.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016 SoftBank Robotics Europe
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #include <boost/algorithm/string.hpp>
19 
21 
22 qi::AnyValue fromStringVectorToAnyValue(const std::vector<std::string> &vector)
23 {
24  qi::AnyValue res;
25  try
26  {
27  std::vector<qi::AnyValue> vector_qi;
28  vector_qi.reserve(vector.size());
29  vector_qi.resize(vector.size());
30 
31  std::vector<std::string>::const_iterator it = vector.begin();
32  std::vector<qi::AnyValue>::iterator it_qi = vector_qi.begin();
33  for(; it != vector.end(); ++it, ++it_qi)
34  {
35  *it_qi = qi::AnyValue(qi::AnyReference::from(*it), false, false);
36  }
37  res = qi::AnyValue(qi::AnyReference::from(vector_qi), false, false);
38  }
39  catch(const std::exception& e)
40  {
41  std::cout << "Could not convert to qi::AnyValue \n\tTrace: " << e.what() << std::endl;
42  }
43  return res;
44 }
45 
46 qi::AnyValue fromDoubleVectorToAnyValue(const std::vector<double> &vector)
47 {
48  qi::AnyValue res;
49  try
50  {
51  std::vector<qi::AnyValue> vector_qi;
52  vector_qi.reserve(vector.size());
53  vector_qi.resize(vector.size());
54 
55  std::vector<double>::const_iterator it = vector.begin();
56  std::vector<qi::AnyValue>::iterator it_qi = vector_qi.begin();
57  for(; it != vector.end(); ++it, ++it_qi)
58  {
59  *it_qi = qi::AnyValue(qi::AnyReference::from(static_cast<float>(*it)), false, false);
60  }
61  res = qi::AnyValue(qi::AnyReference::from(vector_qi), false, false);
62  }
63  catch(const std::exception& e)
64  {
65  std::cout << "Could not convert to qi::AnyValue \n\tTrace: " << e.what() << std::endl;
66  }
67  return res;
68 }
69 
70 std::vector<float> fromAnyValueToFloatVector(qi::AnyValue& value)
71 {
72  std::vector<float> result;
73  qi::AnyReferenceVector anyrefs = value.asListValuePtr();
74 
75  for(int i=0; i<anyrefs.size(); ++i)
76  {
77  try
78  {
79  result.push_back(anyrefs[i].content().toFloat());
80  }
81  catch(std::runtime_error& e)
82  {
83  result.push_back(0.0f);
84  std::cout << e.what() << "=> set to 0.0f" << std::endl;
85  }
86  }
87  return result;
88 }
89 
90 std::vector<int> fromAnyValueToIntVector(qi::AnyValue& value)
91 {
92  std::vector<int> result;
93  qi::AnyReferenceVector anyrefs = value.asListValuePtr();
94 
95  for(int i=0; i<anyrefs.size();i++)
96  {
97  try
98  {
99  result.push_back(anyrefs[i].content().toInt());
100  }
101  catch(std::runtime_error& e)
102  {
103  result.push_back(-1);
104  std::cout << e.what() << "=> set to -1" << std::endl;
105  }
106  }
107  return result;
108 }
109 
110 std::string print(const std::vector <std::string> &vector)
111 {
112  std::stringstream ss;
113  if (vector.size() > 0)
114  {
115  std::copy(vector.begin(), vector.end()-1, std::ostream_iterator<std::string>(ss,", "));
116  std::copy(vector.end()-1, vector.end(), std::ostream_iterator<std::string>(ss));
117  }
118  return ss.str();
119 }
120 
121 std::vector <std::string> toVector(const std::string &input)
122 {
123  std::vector <std::string> value;
124  boost::split (value, input, boost::is_any_of(" "));
125 
126  //check for empty values
127  if (!value.empty())
128  for(std::vector<std::string>::iterator it=value.begin(); it != value.end(); ++it)
129  if ((*it).empty())
130  {
131  value.erase(it);
132  it--;
133  }
134  return value;
135 }
136 
138  std::vector <std::string> *joints)
139 {
140  if (topicList.size() == 0)
141  {
142  ROS_WARN("Mentioned controller does not have joints");
143  return;
144  }
145  for (int i = 0; i < topicList.size(); ++i)
146  {
147  std::string tmp = static_cast<std::string>(topicList[i]);
148  if (tmp.compare("") != std::string::npos)
149  joints->push_back(tmp);
150  }
151 }
std::vector< float > fromAnyValueToFloatVector(qi::AnyValue &value)
Definition: tools.cpp:70
f
int size() const
#define ROS_WARN(...)
qi::AnyValue fromStringVectorToAnyValue(const std::vector< std::string > &vector)
Definition: tools.cpp:22
std::vector< int > fromAnyValueToIntVector(qi::AnyValue &value)
Definition: tools.cpp:90
std::vector< std::string > toVector(const std::string &input)
Definition: tools.cpp:121
qi::AnyValue fromDoubleVectorToAnyValue(const std::vector< double > &vector)
Definition: tools.cpp:46
std::string print(const std::vector< std::string > &vector)
Definition: tools.cpp:110
void xmlToVector(XmlRpc::XmlRpcValue &topicList, std::vector< std::string > *joints)
Definition: tools.cpp:137


naoqi_dcm_driver
Author(s): Konstantinos Chatzilygeroudis , Mikael Arguedas , Karsten Knese , Natalia Lyubova
autogenerated on Thu Jul 25 2019 03:49:27