moveit2
The MoveIt Motion Planning Framework for ROS 2.
utilities.hpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2021, PickNik Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of PickNik Robotics nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: David V. Lu!! */
36 
37 #pragma once
38 
39 #include <ament_index_cpp/get_package_prefix.hpp>
40 #include <ament_index_cpp/get_package_share_directory.hpp>
41 #include <filesystem>
42 #include <string>
43 #include <tinyxml2.h>
44 #include <yaml-cpp/yaml.h>
45 
46 namespace moveit_setup
47 {
51 inline std::filesystem::path getSharePath(const std::string& package_name)
52 {
53  try
54  {
55  return std::filesystem::path(ament_index_cpp::get_package_share_directory(package_name));
56  }
57  catch (const std::runtime_error& e)
58  {
59  return std::filesystem::path();
60  }
61 }
62 
67 inline bool createFolders(const std::filesystem::path& output_path)
68 {
69  return std::filesystem::is_directory(output_path) || std::filesystem::create_directories(output_path);
70 }
71 
76 inline bool createParentFolders(const std::filesystem::path& file_path)
77 {
78  return createFolders(file_path.parent_path());
79 }
80 
88 bool extractPackageNameFromPath(const std::filesystem::path& path, std::string& package_name,
89  std::filesystem::path& relative_filepath);
90 
95 {
96  const char* name;
97  const char* value;
98  bool required = false;
99 };
100 
113 tinyxml2::XMLElement* uniqueInsert(tinyxml2::XMLDocument& doc, tinyxml2::XMLElement& element, const char* tag,
114  const std::vector<XMLAttribute>& attributes = {}, const char* text = nullptr);
115 
116 // Formerly "parse"
117 template <typename T>
118 inline bool getYamlProperty(const YAML::Node& node, const std::string& key, T& storage, const T& default_value = T())
119 {
120  const YAML::Node& n = node[key];
121  bool valid = n.IsDefined();
122  storage = valid ? n.as<T>() : default_value;
123  return valid;
124 }
125 
126 inline bool getYamlProperty(const YAML::Node& node, const std::string& key, std::filesystem::path& storage,
127  const std::string& default_value = "")
128 {
129  std::string storage_s;
130  bool ret = getYamlProperty(node, key, storage_s, default_value);
131  if (ret)
132  {
133  storage = storage_s;
134  }
135  return ret;
136 }
137 
138 } // namespace moveit_setup
bool getYamlProperty(const YAML::Node &node, const std::string &key, T &storage, const T &default_value=T())
Definition: utilities.hpp:118
std::filesystem::path getSharePath(const std::string &package_name)
Return a path for the given package's share folder.
Definition: utilities.hpp:51
tinyxml2::XMLElement * uniqueInsert(tinyxml2::XMLDocument &doc, tinyxml2::XMLElement &element, const char *tag, const std::vector< XMLAttribute > &attributes={}, const char *text=nullptr)
Insert a new XML element with a given tag, attributes, and text.
Definition: utilities.cpp:105
bool extractPackageNameFromPath(const std::filesystem::path &path, std::string &package_name, std::filesystem::path &relative_filepath)
Definition: utilities.cpp:41
bool createParentFolders(const std::filesystem::path &file_path)
Create parent folders (recursively)
Definition: utilities.hpp:76
bool createFolders(const std::filesystem::path &output_path)
Create folders (recursively)
Definition: utilities.hpp:67
string package_name
Definition: setup.py:4
Simple structure for easy xml creation.
Definition: utilities.hpp:95