Skip to content

Commit

Permalink
split into interface specific compilation units
Browse files Browse the repository at this point in the history
  • Loading branch information
dirk-thomas committed Apr 12, 2019
1 parent 2bd9679 commit d511f92
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 328 deletions.
45 changes: 34 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,44 @@ set(generated_path "${CMAKE_BINARY_DIR}/generated")
set(generated_files "${generated_path}/get_factory.cpp")
list(APPEND generated_files "${generated_path}/get_mappings.cpp")

# generate per package compilation units to keep the memory usage low
ament_index_get_resources(ros2_message_packages "rosidl_interfaces")
foreach(message_package ${ros2_message_packages})
find_package(${message_package} QUIET REQUIRED)
message(STATUS "Found ${message_package}: ${${message_package}_VERSION} (${${message_package}_DIR})")
if(NOT "${message_package}" STREQUAL "builtin_interfaces")
list(APPEND generated_files "${generated_path}/${message_package}_factories.cpp")
# generate per interface compilation units to keep the memory usage low
ament_index_get_resources(ros2_interface_packages "rosidl_interfaces")
foreach(package_name ${ros2_interface_packages})
find_package(${package_name} QUIET REQUIRED)
message(STATUS "Found ${package_name}: ${${package_name}_VERSION} (${${package_name}_DIR})")
if(NOT "${package_name}" STREQUAL "builtin_interfaces")
list(APPEND generated_files "${generated_path}/${package_name}_factories.cpp")
list(APPEND generated_files "${generated_path}/${package_name}_factories.hpp")
foreach(interface_file ${${package_name}_INTERFACE_FILES})
file(TO_CMAKE_PATH "${interface_file}" interface_name)
get_filename_component(interface_basename "${interface_name}" NAME_WE)
# skipping actions and request and response messages of services
if(NOT "${interface_name}" MATCHES "^(msg|srv)/" OR "${interface_basename}" MATCHES "_(Request|Response)$")
continue()
endif()
string(REPLACE "/" "__" interface_name "${interface_name}")
get_filename_component(interface_name "${interface_name}" NAME_WE)
list(APPEND generated_files "${generated_path}/${package_name}__${interface_name}__factories.cpp")
endforeach()
endif()
endforeach()

set(target_dependencies
"bin/ros1_bridge_generate_factories"
"resource/get_factory.cpp.em"
"resource/get_mappings.cpp.em"
"resource/interface_factories.cpp.em"
"resource/pkg_factories.cpp.em"
"resource/pkg_factories.hpp.em"
"ros1_bridge/__init__.py")

add_custom_command(
OUTPUT ${generated_files}
COMMAND ${PYTHON_EXECUTABLE} bin/ros1_bridge_generate_factories
--output-path "${generated_path}" --template-dir resource
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
DEPENDS ${target_dependencies}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Generating factories for interface types")

if(NOT WIN32)
# ignore warning in ROS 1 message headers
Expand Down Expand Up @@ -152,7 +175,7 @@ add_library(${PROJECT_NAME} SHARED
${generated_files})
ament_target_dependencies(${PROJECT_NAME}
${prefixed_ros1_message_packages}
${ros2_message_packages}
${ros2_interface_packages}
"rclcpp"
"ros1_roscpp"
"ros1_std_msgs")
Expand All @@ -165,7 +188,7 @@ install(TARGETS ${PROJECT_NAME}
custom_executable(static_bridge
"src/static_bridge.cpp"
ROS1_DEPENDENCIES
TARGET_DEPENDENCIES ${ros2_message_packages})
TARGET_DEPENDENCIES ${ros2_interface_packages})
target_link_libraries(static_bridge
${PROJECT_NAME})

Expand All @@ -179,7 +202,7 @@ target_link_libraries(parameter_bridge
custom_executable(dynamic_bridge
"src/dynamic_bridge.cpp"
ROS1_DEPENDENCIES
TARGET_DEPENDENCIES ${ros2_message_packages})
TARGET_DEPENDENCIES ${ros2_interface_packages})
target_link_libraries(dynamic_bridge
${PROJECT_NAME})

Expand Down
86 changes: 44 additions & 42 deletions resource/interface_factories.cpp.em
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// generated from ros1_bridge/resource/pkg_factories.cpp.em
// generated from ros1_bridge/resource/interface_factories.cpp.em

@###############################################
@#
@# Factory template specializations based on
@# message types of a single ROS 2 package
@# Factory template specializations for a
@# specific message type
@#
@# EmPy template for generating <pkgname>_factories.cpp
@# EmPy template for generating
@# <pkgname>__<type>__<interfacename>__factories.cpp
@#
@###############################################
@# Start of Template
Expand All @@ -27,27 +28,28 @@ from ros1_bridge import camel_case_to_lower_case_underscore
#include <ros1_bridge/convert_builtin_interfaces.hpp>

// include ROS 1 services
@[for service in services]@
@[for service in mapped_services]@
#include <@(service["ros1_package"])/@(service["ros1_name"]).h>
@[end for]@

// include ROS 2 services
@[for service in services]@
@[for service in mapped_services]@
#include <@(service["ros2_package"])/srv/@(camel_case_to_lower_case_underscore(service["ros2_name"])).hpp>
@[end for]@

namespace ros1_bridge
{

std::shared_ptr<FactoryInterface>
get_factory_@(ros2_package_name)(const std::string & ros1_type_name, const std::string & ros2_type_name)
get_factory_@(ros2_package_name)__@(interface_type)__@(interface.message_name)(const std::string & ros1_type_name, const std::string & ros2_type_name)
{
@[if not mappings]@
@[if not mapped_msgs]@
(void)ros1_type_name;
(void)ros2_type_name;
@[end if]@
@[else]@
// mapping from string to specialized template
@[for m in mappings]@
@[end if]@
@[for m in mapped_msgs]@
if (
(ros1_type_name == "@(m.ros1_msg.package_name)/@(m.ros1_msg.message_name)" ||
ros1_type_name == "") &&
Expand All @@ -64,8 +66,37 @@ get_factory_@(ros2_package_name)(const std::string & ros1_type_name, const std::
return std::shared_ptr<FactoryInterface>();
}

std::unique_ptr<ServiceFactoryInterface>
get_service_factory_@(ros2_package_name)__@(interface_type)__@(interface.message_name)(const std::string & ros_id, const std::string & package_name, const std::string & service_name)
{
@[if not mapped_services]@
(void)ros_id;
(void)package_name;
(void)service_name;
@[end if]@
@[for service in mapped_services]@
if (
(
ros_id == "ros1" &&
package_name == "@(service["ros1_package"])" &&
service_name == "@(service["ros1_name"])"
) || (
ros_id == "ros2" &&
package_name == "@(service["ros2_package"])" &&
service_name == "@(service["ros2_name"])"
)
) {
return std::unique_ptr<ServiceFactoryInterface>(new ServiceFactory<
@(service["ros1_package"])::@(service["ros1_name"]),
@(service["ros2_package"])::srv::@(service["ros2_name"])
>);
}
@[end for]@
return nullptr;
}
@
// conversion functions for available interfaces
@[for m in mappings]@
@[for m in mapped_msgs]@

template<>
void
Expand Down Expand Up @@ -214,10 +245,10 @@ Factory<
@[ end if]@
@[ end for]@
}

@[end for]@
@
@[for service in mapped_services]@

@[for service in services]@
@[ for frm, to in [("1", "2"), ("2", "1")]]@
@[ for type in ["Request", "Response"]]@
template <>
Expand Down Expand Up @@ -263,33 +294,4 @@ void ServiceFactory<
@[ end for]@
@[ end for]@
@[end for]@

std::unique_ptr<ServiceFactoryInterface>
get_service_factory_@(ros2_package_name)(const std::string & ros_id, const std::string & package_name, const std::string & service_name)
{
@[if not services]@
(void)ros_id;
(void)package_name;
(void)service_name;
@[end if]@
@[for service in services]@
if (
(
ros_id == "ros1" &&
package_name == "@(service["ros1_package"])" &&
service_name == "@(service["ros1_name"])"
) || (
ros_id == "ros2" &&
package_name == "@(service["ros2_package"])" &&
service_name == "@(service["ros2_name"])"
)
) {
return std::unique_ptr<ServiceFactoryInterface>(new ServiceFactory<
@(service["ros1_package"])::@(service["ros1_name"]),
@(service["ros2_package"])::srv::@(service["ros2_name"])
>);
}
@[end for]@
return nullptr;
}
} // namespace ros1_bridge
Loading

0 comments on commit d511f92

Please sign in to comment.