cplusplus.py
Go to the documentation of this file.
1 import re
2 
3 from ros_introspection.resource_list import MESSAGES, SERVICES
4 
5 from roscompile.util import convert_to_caps_notation, convert_to_underscore_notation
6 
7 ROS2_INCLUDE_PATTERN = '#include <%s/%s/%s.hpp>'
8 
9 LOGGERS = {
10  'ROS_DEBUG': 'RCLCPP_DEBUG',
11  'ROS_INFO': 'RCLCPP_INFO',
12  'ROS_ERROR': 'RCLCPP_ERROR',
13  'ROS_WARN': 'RCLCPP_WARN'
14 }
15 
16 
18  return r'#include\s*[<\\"]' + s + '[>\\"]'
19 
20 
21 FIRST_PASS = {
22  r'( *)ros::init\(argc, argv, "([^"]*)"\);(\s+)ros::NodeHandle (\w+);':
23  '$0rclcpp::init(argc, argv);$2auto $3 = rclcpp::Node::make_shared("$1");'
24 }
25 
26 CPP_CODE_REPLACEMENTS = {
27  make_include_pattern('ros/ros.h'): '#include "rclcpp/rclcpp.hpp"',
28  'ros::Time': 'rclcpp::Time',
29  'std_msgs::Time': 'builtin_interfaces::msg::Time',
30  'std_msgs/time.h': 'builtin_interfaces/msg/time.hpp',
31  'ros::Rate': 'rclcpp::Rate',
32  'ros::Duration': 'rclcpp::Duration',
33  r'ros::ok\(\)': 'rclcpp::ok()',
34  r'( *)ros::init\(argc, argv, "([^"]*)"\);':
35  '$0rclcpp::init(argc, argv);\n$0auto node = rclcpp::Node::make_shared("$1");',
36  r'ros::spinOnce\(\);': 'rclcpp::spin_some(node);',
37  r'ros::spin\(\);': 'rclcpp::spin(node);',
38  'ros::NodeHandle': 'rclcpp::Node',
39  'ros::Publisher (.*) = (.*)advertise(.*);': 'auto $0 = $1advertise$2;',
40  'ros::Subscriber (.*) = (.*)subscribe(.*);': 'auto $0 = $1subscribe$2;',
41 
42  # boost stuff
43  make_include_pattern('boost/shared_ptr.hpp'): '#include <memory>',
44  'boost::shared_ptr': 'std::shared_ptr',
45  make_include_pattern('boost/weak_ptr.hpp'): '#include <memory>',
46  'boost::weak_ptr': 'std::weak_ptr',
47  make_include_pattern('boost/thread/mutex.hpp'): '#include <mutex>',
48  'boost::mutex': 'std::mutex',
49  'boost::mutex::scoped_lock': 'std::unique_lock<std::mutex>',
50  make_include_pattern('boost/unordered_map.hpp'): '#include <unordered_map>',
51  'boost::unordered_map': 'std::unordered_map',
52  make_include_pattern('boost/function.hpp'): '#include <functional>',
53  'boost::function': 'std::function',
54 
55  # tf stuff
56  make_include_pattern('tf/transform_listener.h'):
57  '#include "tf2_ros/buffer.h"\n#include "tf2_ros/transform_listener.h"',
58  'tf::TransformListener': 'tf2_ros::TransformListener',
59  'tf::Stamped': 'tf2::Stamped',
60  'tf::Pose': 'tf2::Pose',
61  'tf::get': 'tf2::get',
62 }
63 
64 
66  messages = set()
67  for gen_type, full_list in [('msg', MESSAGES), ('srv', SERVICES)]:
68  for pkg, gen_name in full_list:
69  gen_pattern = re.compile(pkg + '.*' + gen_name)
70  if package.source_code.search_for_pattern(gen_pattern):
71  messages.add((pkg, gen_name, gen_type))
72  return messages
73 
74 
76  service_replacements = {}
77  generator_replacements = {}
78  for pkg, msg, gen_type in get_full_msg_dependencies_from_source(package):
79  key = make_include_pattern('%s/%s.h' % (pkg, msg))
80  value = ROS2_INCLUDE_PATTERN % (pkg, gen_type, convert_to_underscore_notation(msg))
81  generator_replacements[key] = value
82 
83  two_colons = '%s::%s' % (pkg, msg)
84  four_colons = '%s::%s::%s' % (pkg, gen_type, msg)
85  generator_replacements[two_colons] = four_colons
86 
87  generator_replacements['(' + msg + ')ConstPtr'] = '$0::ConstSharedPtr'
88  generator_replacements['(' + msg + ')::ConstPtr'] = '$0::ConstSharedPtr'
89 
90  if gen_type == 'srv':
91  key = r'bool ([^\(]+)\(\s*' + two_colons + r'::Request\s+&\s+([^,]+),\s+'
92  key += two_colons + r'::Response\s+&\s+([^\)]+)\)'
93  value = 'void $1(const std::shared_ptr<' + four_colons + '::Request> $2, '
94  value += 'std::shared_ptr<' + four_colons + '::Response> $3)'
95  service_replacements[key] = value
96  return generator_replacements, service_replacements
97 
98 
100  LOGGER_REPLACEMENTS = {}
101  PackageName = convert_to_caps_notation(package.name)
102  for old_logger, new_logger in LOGGERS.items():
103  LOGGER_REPLACEMENTS[old_logger + r'\('] = new_logger + '(rclcpp::get_logger("' + PackageName + '"), '
104  # old_pattern = old_logger + '([_A-Z]*)\('
105  LOGGER_REPLACEMENTS[old_logger + r'_NAMED\(([^,]+),'] = new_logger + '(rclcpp::get_logger($0),'
106  return LOGGER_REPLACEMENTS
107 
108 
109 def update_cplusplus(package):
110  generator_replacements, service_replacements = get_generator_based_replacements(package)
111  package.source_code.modify_with_patterns(service_replacements)
112  package.source_code.modify_with_patterns(generator_replacements)
113  package.source_code.modify_with_patterns(get_logger_replacements(package))
114  package.source_code.modify_with_patterns(FIRST_PASS)
115  package.source_code.modify_with_patterns(CPP_CODE_REPLACEMENTS)
def get_full_msg_dependencies_from_source(package)
Definition: cplusplus.py:65


magical_ros2_conversion_tool
Author(s):
autogenerated on Wed Mar 3 2021 03:56:02