moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
generated_file.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
41#include <chrono>
42#include <memory>
43#include <string>
44#include <iostream>
45#include <fstream>
46
47namespace moveit_setup
48{
52enum class FileStatus
53{
54 NEW, // The file does not exist in the configuration package
55 UNCHANGED, // The file exists and would be the same as the generated file
56 CHANGED, // The file exists, but a new version will be written
57 EXTERNALLY_MODIFIED, // The file exists and was externally modified
58 CONFLICTED // The file exists, was externally modified and there are changes to be written
59};
60
61MOVEIT_CLASS_FORWARD(GeneratedFile); // Defines GeneratedFilePtr, ConstPtr, WeakPtr... etc
62
66class GeneratedFile : public std::enable_shared_from_this<GeneratedFile>
67{
68public:
69 GeneratedFile(const GeneratedFile&) = default;
71 virtual ~GeneratedFile() = default;
72
73 GeneratedFile(const std::filesystem::path& package_path, const GeneratedTime& last_gen_time)
74 : package_path_(package_path), last_gen_time_(last_gen_time)
75 {
76 }
77
81 virtual std::filesystem::path getRelativePath() const = 0;
82
86 virtual std::string getDescription() const = 0;
87
91 virtual bool hasChanges() const = 0;
92
96 virtual bool write() = 0;
97
101 std::filesystem::path getPath() const
102 {
104 }
105
107 {
108 std::filesystem::path full_path = getPath();
109 if (!std::filesystem::is_regular_file(full_path) || last_gen_time_ == GeneratedTime())
110 {
111 return FileStatus::NEW;
112 }
113 GeneratedTime mod_time = std::filesystem::last_write_time(full_path);
115 {
117 }
118 else
119 {
121 }
122 }
123
124protected:
125 static constexpr GeneratedTime::duration TIME_MOD_TOLERANCE = std::chrono::seconds(10);
126
127 std::filesystem::path package_path_;
129};
130
132{
133public:
135
136 bool write() override
137 {
138 YAML::Emitter emitter;
139 bool ret = writeYaml(emitter);
140 if (!ret)
141 {
142 return false;
143 }
144
145 std::filesystem::path file_path = getPath();
146 createParentFolders(file_path);
147 std::ofstream output_stream(file_path, std::ios_base::trunc);
148 if (!output_stream.good())
149 {
150 return false;
151 }
152
153 output_stream << emitter.c_str();
154 output_stream.close();
155
156 return true; // file created successfully
157 }
158
159 virtual bool writeYaml(YAML::Emitter& emitter) = 0;
160};
161
162} // namespace moveit_setup
#define MOVEIT_CLASS_FORWARD(C)
Container for the logic for a single file to appear in MoveIt configuration package.
virtual std::string getDescription() const =0
Returns an English description of this file's purpose.
static constexpr GeneratedTime::duration TIME_MOD_TOLERANCE
GeneratedFile(const GeneratedFile &)=default
std::filesystem::path getPath() const
Returns the fully qualified path to this file.
virtual std::filesystem::path getRelativePath() const =0
Returns the path relative to the configuration package root.
virtual bool hasChanges() const =0
Returns true if this file will have changes when it is written to file.
std::filesystem::path package_path_
virtual ~GeneratedFile()=default
virtual bool write()=0
Writes the file to disk.
GeneratedFile(GeneratedFile &&)=default
const GeneratedTime & last_gen_time_
GeneratedFile(const std::filesystem::path &package_path, const GeneratedTime &last_gen_time)
bool write() override
Writes the file to disk.
virtual bool writeYaml(YAML::Emitter &emitter)=0
bool createParentFolders(const std::filesystem::path &file_path)
Create parent folders (recursively)
Definition utilities.hpp:76
std::filesystem::file_time_type GeneratedTime
FileStatus
Status associated with each GeneratedFile.