moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
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// ament_index_cpp's get_package_share_directory API changed twice.
41// Guard on ament_index_cpp's own version — not rclcpp — because the
42// changes live in ament_index_cpp and its release cadence is independent
43// of rclcpp's.
44// 1.14+ (Rolling on Resolute):
45// get_package_share_directory.hpp REMOVED, replaced with
46// get_package_share_path.hpp exposing get_package_share_path(pkg)
47// returning std::filesystem::path directly.
48// 1.5+ (Jazzy 1.8.4, Kilted 1.11.4, Lyrical 1.13.3, Rolling-on-Noble 1.13.1):
49// 2-arg out-param overload get_package_share_directory(pkg, path)
50// available; the 1-arg form is deprecated starting in 1.13.
51// Older (Humble 1.4.1):
52// only the non-deprecated 1-arg get_package_share_directory(pkg)
53// returning std::string is available.
54#include <ament_index_cpp/version.h>
55#if AMENT_INDEX_CPP_VERSION_GTE(1, 14, 0)
56#include <ament_index_cpp/get_package_share_path.hpp>
57#else
58#include <ament_index_cpp/get_package_share_directory.hpp>
59#endif
60#include <filesystem>
61#include <string>
62#include <tinyxml2.h>
63#include <yaml-cpp/yaml.h>
64
65namespace moveit_setup
66{
70inline std::filesystem::path getSharePath(const std::string& package_name)
71{
72 try
73 {
74#if AMENT_INDEX_CPP_VERSION_GTE(1, 14, 0)
75 return ament_index_cpp::get_package_share_path(package_name);
76#elif AMENT_INDEX_CPP_VERSION_GTE(1, 5, 0)
77 std::filesystem::path path;
78 ament_index_cpp::get_package_share_directory(package_name, path);
79 return path;
80#else
81 return std::filesystem::path(ament_index_cpp::get_package_share_directory(package_name));
82#endif
83 }
84 catch (const std::runtime_error& e)
85 {
86 return std::filesystem::path();
87 }
88}
89
94inline bool createFolders(const std::filesystem::path& output_path)
95{
96 return std::filesystem::is_directory(output_path) || std::filesystem::create_directories(output_path);
97}
98
103inline bool createParentFolders(const std::filesystem::path& file_path)
104{
105 return createFolders(file_path.parent_path());
106}
107
115bool extractPackageNameFromPath(const std::filesystem::path& path, std::string& package_name,
116 std::filesystem::path& relative_filepath);
117
122{
123 const char* name;
124 const char* value;
125 bool required = false;
126};
127
140tinyxml2::XMLElement* uniqueInsert(tinyxml2::XMLDocument& doc, tinyxml2::XMLElement& element, const char* tag,
141 const std::vector<XMLAttribute>& attributes = {}, const char* text = nullptr);
142
143// Formerly "parse"
144template <typename T>
145inline bool getYamlProperty(const YAML::Node& node, const std::string& key, T& storage, const T& default_value = T())
146{
147 const YAML::Node& n = node[key];
148 bool valid = n.IsDefined();
149 storage = valid ? n.as<T>() : default_value;
150 return valid;
151}
152
153inline bool getYamlProperty(const YAML::Node& node, const std::string& key, std::filesystem::path& storage,
154 const std::string& default_value = "")
155{
156 std::string storage_s;
157 bool ret = getYamlProperty(node, key, storage_s, default_value);
158 if (ret)
159 {
160 storage = storage_s;
161 }
162 return ret;
163}
164
165} // namespace moveit_setup
bool getYamlProperty(const YAML::Node &node, const std::string &key, T &storage, const T &default_value=T())
std::filesystem::path getSharePath(const std::string &package_name)
Return a path for the given package's share folder.
Definition utilities.hpp:70
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.
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).
bool createFolders(const std::filesystem::path &output_path)
Create folders (recursively).
Definition utilities.hpp:94
Simple structure for easy xml creation.