scenario_simulator_v2 C++ API
launch.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef CONCEALER__LAUNCH_HPP_
16 #define CONCEALER__LAUNCH_HPP_
17 
18 #include <boost/algorithm/string.hpp>
19 #include <concealer/execute.hpp>
20 #include <cstdlib>
21 #include <iostream>
22 #include <string>
23 #include <system_error>
24 #include <type_traits>
25 #include <vector>
26 
27 namespace concealer
28 {
29 template <typename Parameters>
31  const std::string & package, const std::string & file, const Parameters & parameters)
32 {
33  const auto argv = [&]() {
34  auto argv = std::vector<std::string>();
35 
36  argv.push_back("python3");
37  argv.push_back(boost::algorithm::replace_all_copy(dollar("which ros2"), "\n", ""));
38  argv.push_back("launch");
39  argv.push_back(package);
40  argv.push_back(file);
41 
42  for (const auto & parameter : parameters) {
43  argv.push_back(parameter);
44  }
45 
46  return argv;
47  }();
48 
49  if (const auto process_id = fork(); process_id < 0) {
50  throw std::system_error(errno, std::system_category());
51  } else if (process_id == 0 and execute(argv) < 0) {
52  std::cout << std::system_error(errno, std::system_category()).what() << std::endl;
53  std::exit(EXIT_FAILURE);
54  } else {
55  return process_id;
56  }
57 }
58 } // namespace concealer
59 
60 #endif // CONCEALER__LAUNCH_HPP_
Definition: autoware_universe.hpp:40
auto ros2_launch(const std::string &package, const std::string &file, const Parameters &parameters)
Definition: launch.hpp:30
auto dollar(const std::string &command) -> std::string
Definition: execute.cpp:57
auto execute(const std::vector< std::string > &) -> int
Definition: execute.cpp:36
pid_t process_id
Definition: record.cpp:25
std::string string
Definition: junit5.hpp:26