|
20 | 20 | #include <unordered_map> |
21 | 21 | #include <vector> |
22 | 22 |
|
| 23 | +#include "rcpputils/scope_exit.hpp" |
| 24 | + |
23 | 25 | #include "rclcpp/exceptions.hpp" |
24 | 26 | #include "rclcpp/expand_topic_or_service_name.hpp" |
25 | 27 | #include "rclcpp/experimental/intra_process_manager.hpp" |
@@ -354,3 +356,93 @@ SubscriptionBase::set_on_new_message_callback( |
354 | 356 | throw_from_rcl_error(ret, "failed to set the on new message callback for subscription"); |
355 | 357 | } |
356 | 358 | } |
| 359 | + |
| 360 | +#ifdef CONTENT_FILTER_ENABLE |
| 361 | +bool |
| 362 | +SubscriptionBase::is_cft_enabled() const |
| 363 | +{ |
| 364 | + return rcl_subscription_is_cft_enabled(subscription_handle_.get()); |
| 365 | +} |
| 366 | + |
| 367 | +void |
| 368 | +SubscriptionBase::set_content_filter( |
| 369 | + const std::string & filter_expression, |
| 370 | + const std::vector<std::string> & expression_parameters) |
| 371 | +{ |
| 372 | + rcl_subscription_content_filter_options_t options = |
| 373 | + rcl_get_zero_initialized_subscription_content_filter_options(); |
| 374 | + |
| 375 | + std::vector<const char *> cstrings = |
| 376 | + get_c_vector_string(expression_parameters); |
| 377 | + rcl_ret_t ret = rcl_subscription_content_filter_options_init( |
| 378 | + subscription_handle_.get(), |
| 379 | + get_c_string(filter_expression), |
| 380 | + cstrings.size(), |
| 381 | + cstrings.data(), |
| 382 | + &options); |
| 383 | + if (RCL_RET_OK != ret) { |
| 384 | + rclcpp::exceptions::throw_from_rcl_error( |
| 385 | + ret, "failed to init subscription content_filtered_topic option"); |
| 386 | + } |
| 387 | + RCPPUTILS_SCOPE_EXIT( |
| 388 | + { |
| 389 | + rcl_ret_t ret = rcl_subscription_content_filter_options_fini( |
| 390 | + subscription_handle_.get(), &options); |
| 391 | + if (RCL_RET_OK != ret) { |
| 392 | + RCLCPP_ERROR( |
| 393 | + rclcpp::get_logger("rclcpp"), |
| 394 | + "Failed to fini subscription content_filtered_topic option: %s", |
| 395 | + rcl_get_error_string().str); |
| 396 | + rcl_reset_error(); |
| 397 | + } |
| 398 | + }); |
| 399 | + |
| 400 | + ret = rcl_subscription_set_content_filter( |
| 401 | + subscription_handle_.get(), |
| 402 | + &options); |
| 403 | + |
| 404 | + if (RCL_RET_OK != ret) { |
| 405 | + rclcpp::exceptions::throw_from_rcl_error(ret, "failed to set cft expression parameters"); |
| 406 | + } |
| 407 | +} |
| 408 | + |
| 409 | +rclcpp::ContentFilterOptions |
| 410 | +SubscriptionBase::get_content_filter() const |
| 411 | +{ |
| 412 | + rclcpp::ContentFilterOptions ret_options; |
| 413 | + rcl_subscription_content_filter_options_t options = |
| 414 | + rcl_get_zero_initialized_subscription_content_filter_options(); |
| 415 | + |
| 416 | + rcl_ret_t ret = rcl_subscription_get_content_filter( |
| 417 | + subscription_handle_.get(), |
| 418 | + &options); |
| 419 | + |
| 420 | + if (RCL_RET_OK != ret) { |
| 421 | + rclcpp::exceptions::throw_from_rcl_error(ret, "failed to get cft expression parameters"); |
| 422 | + } |
| 423 | + |
| 424 | + RCPPUTILS_SCOPE_EXIT( |
| 425 | + { |
| 426 | + rcl_ret_t ret = rcl_subscription_content_filter_options_fini( |
| 427 | + subscription_handle_.get(), &options); |
| 428 | + if (RCL_RET_OK != ret) { |
| 429 | + RCLCPP_ERROR( |
| 430 | + rclcpp::get_logger("rclcpp"), |
| 431 | + "Failed to fini subscription content_filtered_topic option: %s", |
| 432 | + rcl_get_error_string().str); |
| 433 | + rcl_reset_error(); |
| 434 | + } |
| 435 | + }); |
| 436 | + |
| 437 | + rmw_subscription_content_filter_options_t & content_filter_options = |
| 438 | + options.rmw_subscription_content_filter_options; |
| 439 | + ret_options.filter_expression = content_filter_options.filter_expression; |
| 440 | + |
| 441 | + for (size_t i = 0; i < content_filter_options.expression_parameters.size; ++i) { |
| 442 | + ret_options.expression_parameters.push_back( |
| 443 | + content_filter_options.expression_parameters.data[i]); |
| 444 | + } |
| 445 | + |
| 446 | + return ret_options; |
| 447 | +} |
| 448 | +#endif // CONTENT_FILTER_ENABLE |
0 commit comments