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_share_directory.hpp>
40 #include <filesystem>
41 #include <string>
42 #include <tinyxml2.h>
43 #include <yaml-cpp/yaml.h>
44 
45 namespace moveit_setup
46 {
50 inline std::filesystem::path getSharePath(const std::string& package_name)
51 {
52  return std::filesystem::path(ament_index_cpp::get_package_share_directory(package_name));
53 }
54 
59 inline bool createFolders(const std::filesystem::path& output_path)
60 {
61  return std::filesystem::is_directory(output_path) || std::filesystem::create_directories(output_path);
62 }
63 
68 inline bool createParentFolders(const std::filesystem::path& file_path)
69 {
70  return createFolders(file_path.parent_path());
71 }
72 
80 bool extractPackageNameFromPath(const std::filesystem::path& path, std::string& package_name,
81  std::filesystem::path& relative_filepath);
82 
87 {
88  const char* name;
89  const char* value;
90  bool required = false;
91 };
92 
105 tinyxml2::XMLElement* uniqueInsert(tinyxml2::XMLDocument& doc, tinyxml2::XMLElement& element, const char* tag,
106  const std::vector<XMLAttribute>& attributes = {}, const char* text = nullptr);
107 
108 // Formerly "parse"
109 template <typename T>
110 inline bool getYamlProperty(const YAML::Node& node, const std::string& key, T& storage, const T& default_value = T())
111 {
112  const YAML::Node& n = node[key];
113  bool valid = n.IsDefined();
114  storage = valid ? n.as<T>() : default_value;
115  return valid;
116 }
117 
118 inline bool getYamlProperty(const YAML::Node& node, const std::string& key, std::filesystem::path& storage,
119  const std::string& default_value = "")
120 {
121  std::string storage_s;
122  bool ret = getYamlProperty(node, key, storage_s, default_value);
123  if (ret)
124  {
125  storage = storage_s;
126  }
127  return ret;
128 }
129 
130 } // namespace moveit_setup
bool getYamlProperty(const YAML::Node &node, const std::string &key, T &storage, const T &default_value=T())
Definition: utilities.hpp:110
std::filesystem::path getSharePath(const std::string &package_name)
Return a path for the given package's share folder.
Definition: utilities.hpp:50
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:100
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:68
bool createFolders(const std::filesystem::path &output_path)
Create folders (recursively)
Definition: utilities.hpp:59
string package_name
Definition: setup.py:4
Simple structure for easy xml creation.
Definition: utilities.hpp:87