tesseract_urdf_common_unit.h
Go to the documentation of this file.
1 #ifndef TESSERACT_URDF_COMMON_UNIT_H
2 #define TESSERACT_URDF_COMMON_UNIT_H
3 
6 #include <fstream>
7 #include <iostream>
8 #include <vector>
9 
10 #include <gtest/gtest.h>
11 #include <console_bridge/console.h>
12 #include <tinyxml2.h>
14 
17 #include <tesseract_common/utils.h>
19 
20 template <typename ElementType>
21 bool runTest(ElementType& type,
22  std::function<ElementType(const tinyxml2::XMLElement*)> func,
23  const std::string& xml_string,
24  const std::string& element_name)
25 {
26  tinyxml2::XMLDocument xml_doc;
27  EXPECT_TRUE(xml_doc.Parse(xml_string.c_str()) == tinyxml2::XML_SUCCESS);
28 
29  tinyxml2::XMLElement* element = xml_doc.FirstChildElement(element_name.c_str());
30  EXPECT_TRUE(element != nullptr);
31 
32  try
33  {
34  type = func(element);
35  }
36  catch (const std::exception& e)
37  {
39  return false;
40  }
41 
42  return true;
43 }
44 
45 template <typename ElementType>
46 bool runTest(
47  ElementType& type,
48  std::function<ElementType(const tinyxml2::XMLElement*, const tesseract_common::ResourceLocator&, bool)> func,
49  const std::string& xml_string,
50  const std::string& element_name,
51  const tesseract_common::ResourceLocator& locator,
52  bool visual)
53 {
54  tinyxml2::XMLDocument xml_doc;
55  EXPECT_TRUE(xml_doc.Parse(xml_string.c_str()) == tinyxml2::XML_SUCCESS);
56 
57  tinyxml2::XMLElement* element = xml_doc.FirstChildElement(element_name.c_str());
58  EXPECT_TRUE(element != nullptr);
59 
60  try
61  {
62  type = func(element, locator, visual);
63  }
64  catch (const std::exception& e)
65  {
67  return false;
68  }
69 
70  return true;
71 }
72 
73 template <typename ElementType>
74 bool runTest(ElementType& type,
75  std::function<ElementType(const tinyxml2::XMLElement*, const tesseract_common::ResourceLocator&)> func,
76  const std::string& xml_string,
77  const std::string& element_name,
78  const tesseract_common::ResourceLocator& locator)
79 {
80  tinyxml2::XMLDocument xml_doc;
81  EXPECT_TRUE(xml_doc.Parse(xml_string.c_str()) == tinyxml2::XML_SUCCESS);
82 
83  tinyxml2::XMLElement* element = xml_doc.FirstChildElement(element_name.c_str());
84  EXPECT_TRUE(element != nullptr);
85 
86  try
87  {
88  type = func(element, locator);
89  }
90  catch (const std::exception& e)
91  {
93  return false;
94  }
95 
96  return true;
97 }
98 
99 template <typename ElementType>
100 bool runTest(ElementType& type,
101  std::function<ElementType(const tinyxml2::XMLElement*,
103  std::unordered_map<std::string, tesseract_scene_graph::Material::Ptr>&)> func,
104  const std::string& xml_string,
105  const std::string& element_name,
106  const tesseract_common::ResourceLocator& locator,
107  std::unordered_map<std::string, tesseract_scene_graph::Material::Ptr>& available_materials)
108 {
109  tinyxml2::XMLDocument xml_doc;
110  EXPECT_TRUE(xml_doc.Parse(xml_string.c_str()) == tinyxml2::XML_SUCCESS);
111 
112  tinyxml2::XMLElement* element = xml_doc.FirstChildElement(element_name.c_str());
113  EXPECT_TRUE(element != nullptr);
114 
115  try
116  {
117  type = func(element, locator, available_materials);
118  }
119  catch (const std::exception& e)
120  {
122  return false;
123  }
124 
125  return true;
126 }
127 
128 template <typename ElementType>
129 bool runTest(ElementType& type,
130  std::function<ElementType(const tinyxml2::XMLElement*,
131  std::unordered_map<std::string, tesseract_scene_graph::Material::Ptr>&,
132  bool)> func,
133  const std::string& xml_string,
134  const std::string& element_name,
135  std::unordered_map<std::string, tesseract_scene_graph::Material::Ptr>& available_materials,
136  const bool visual)
137 {
138  tinyxml2::XMLDocument xml_doc;
139  EXPECT_TRUE(xml_doc.Parse(xml_string.c_str()) == tinyxml2::XML_SUCCESS);
140 
141  tinyxml2::XMLElement* element = xml_doc.FirstChildElement(element_name.c_str());
142  EXPECT_TRUE(element != nullptr);
143 
144  try
145  {
146  type = func(element, available_materials, visual);
147  }
148  catch (const std::exception& e)
149  {
151  return false;
152  }
153 
154  return true;
155 }
156 
162 inline std::string stripNewline(const std::string& in)
163 {
164  std::string out;
165  if (!in.empty() && in.back() == '\n')
166  out = in.substr(0, in.size() - 1);
167  else
168  out = in;
169  return out;
170 }
171 
172 inline std::string toString(const tinyxml2::XMLElement* element)
173 {
174  std::string ret;
175  if (element != nullptr)
176  {
177  tinyxml2::XMLPrinter printer;
178  element->Accept(&printer);
179  std::stringstream ss;
180  ss << printer.CStr();
181  ret = stripNewline(ss.str());
182  }
183  return ret;
184 }
185 
193 template <typename TessType>
194 int writeTest(TessType& type,
195  std::function<tinyxml2::XMLElement*(const TessType&, tinyxml2::XMLDocument&)> func,
196  std::string& text)
197 {
198  tinyxml2::XMLDocument doc;
199  int status = 0;
200  try
201  {
202  tinyxml2::XMLElement* element = func(type, doc);
203  text = toString(element);
204  if (element != nullptr)
205  status = 0;
206  else
207  status = 2;
208  }
209  catch (...)
210  {
211  text = "";
212  status = 1;
213  }
214  return status;
215 }
216 
225 template <typename TessType>
226 int writeTest(TessType& type,
227  std::function<tinyxml2::XMLElement*(const TessType&, tinyxml2::XMLDocument&, const std::string&)> func,
228  std::string& text,
229  const std::string& directory)
230 {
231  tinyxml2::XMLDocument doc;
232  int status = 0;
233  try
234  {
235  tinyxml2::XMLElement* element = func(type, doc, directory);
236  text = toString(element);
237  if (element != nullptr)
238  status = 0;
239  else
240  status = 2;
241  }
242  catch (...)
243  {
244  text = "";
245  status = 1;
246  }
247  return status;
248 }
249 
259 template <typename TessType>
261  TessType& type,
262  std::function<
263  tinyxml2::XMLElement*(const TessType&, tinyxml2::XMLDocument&, const std::string&, const std::string&)> func,
264  std::string& text,
265  const std::string& directory,
266  const std::string& filename)
267 {
268  tinyxml2::XMLDocument doc;
269  int status = 0;
270  try
271  {
272  tinyxml2::XMLElement* element = func(type, doc, directory, filename);
273  text = toString(element);
274  if (element != nullptr)
275  status = 0;
276  else
277  status = 2;
278  }
279  catch (...)
280  {
281  text = "";
282  status = 1;
283  }
284  return status;
285 }
286 
297 template <typename TessType>
298 int writeTest(TessType& type,
299  std::function<tinyxml2::XMLElement*(const TessType&,
300  tinyxml2::XMLDocument&,
301  const std::string&,
302  const std::string&,
303  const int)> func,
304  std::string& text,
305  const std::string& directory,
306  const std::string& link_name,
307  const int id)
308 {
309  tinyxml2::XMLDocument doc;
310  int status = 0;
311  try
312  {
313  tinyxml2::XMLElement* element = func(type, doc, directory, link_name, id);
314  text = toString(element);
315  if (element != nullptr)
316  status = 0;
317  else
318  status = 2;
319  }
320  catch (...)
321  {
322  text = "";
323  status = 1;
324  }
325  return status;
326 }
327 
328 inline void writeTextToFile(const std::string& path, const std::string& text)
329 {
330  std::ofstream file;
331  file.open(path);
332  file << text;
333  file.close();
334 }
335 
336 #endif // TESSERACT_URDF_COMMON_UNIT_H
writeTextToFile
void writeTextToFile(const std::string &path, const std::string &text)
Definition: tesseract_urdf_common_unit.h:328
writeTest
int writeTest(TessType &type, std::function< tinyxml2::XMLElement *(const TessType &, tinyxml2::XMLDocument &)> func, std::string &text)
writeTest - test write functions
Definition: tesseract_urdf_common_unit.h:194
utils.h
resource_locator.h
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
EXPECT_TRUE
#define EXPECT_TRUE(args)
runTest
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH TESSERACT_COMMON_IGNORE_WARNINGS_POP bool runTest(ElementType &type, std::function< ElementType(const tinyxml2::XMLElement *)> func, const std::string &xml_string, const std::string &element_name)
Definition: tesseract_urdf_common_unit.h:21
toString
std::string toString(const tinyxml2::XMLElement *element)
Definition: tesseract_urdf_common_unit.h:172
urdf_parser.h
A urdf parser for tesseract.
tesseract_common::printNestedException
void printNestedException(const std::exception &e, int level=0)
tesseract_common::ResourceLocator
TESSERACT_COMMON_IGNORE_WARNINGS_POP
stripNewline
std::string stripNewline(const std::string &in)
stripNewline - For some reason, tinyxml2 printers add a trailing newline. This function removes it.
Definition: tesseract_urdf_common_unit.h:162
type
type
macros.h


tesseract_urdf
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:02:07