Skip to content

Commit 81049c4

Browse files
authored
clean up publisher and subscription creation logic (#867)
* streamline creation of publishers after removing deprecated API Signed-off-by: William Woodall <william@osrfoundation.org> * use deduced template arguments to cleanup create_subscription Signed-off-by: William Woodall <william@osrfoundation.org> * add missing file Signed-off-by: William Woodall <william@osrfoundation.org> * streamline creation of subscriptions after removing deprecated API Signed-off-by: William Woodall <william@osrfoundation.org> * small subscription code cleanup to match publisher's style Signed-off-by: William Woodall <william@osrfoundation.org> * some fixes to rclcpp_lifecycle to match rclcpp Signed-off-by: William Woodall <william@osrfoundation.org> * add README to the rclcpp/detail include directory Signed-off-by: William Woodall <william@osrfoundation.org> * fixup SubscriptionBase's use of visibility macros Signed-off-by: William Woodall <william@osrfoundation.org> * reapply function to create default options, as it is still needed on Windows Signed-off-by: William Woodall <william@osrfoundation.org> * address review comments Signed-off-by: William Woodall <william@osrfoundation.org> * workaround cppcheck 1.89 syntax error Signed-off-by: William Woodall <william@osrfoundation.org>
1 parent 7728d8a commit 81049c4

22 files changed

+381
-297
lines changed

rclcpp/include/rclcpp/callback_group.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
#include <vector>
2222

2323
#include "rclcpp/client.hpp"
24-
#include "rclcpp/publisher.hpp"
24+
#include "rclcpp/publisher_base.hpp"
2525
#include "rclcpp/service.hpp"
26-
#include "rclcpp/subscription.hpp"
26+
#include "rclcpp/subscription_base.hpp"
2727
#include "rclcpp/timer.hpp"
2828
#include "rclcpp/visibility_control.hpp"
2929
#include "rclcpp/waitable.hpp"

rclcpp/include/rclcpp/create_publisher.hpp

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace rclcpp
3737
template<
3838
typename MessageT,
3939
typename AllocatorT = std::allocator<void>,
40-
typename PublisherT = ::rclcpp::Publisher<MessageT, AllocatorT>,
40+
typename PublisherT = rclcpp::Publisher<MessageT, AllocatorT>,
4141
typename NodeT>
4242
std::shared_ptr<PublisherT>
4343
create_publisher(
@@ -46,43 +46,23 @@ create_publisher(
4646
const rclcpp::QoS & qos,
4747
const rclcpp::PublisherOptionsWithAllocator<AllocatorT> & options = (
4848
rclcpp::PublisherOptionsWithAllocator<AllocatorT>()
49-
))
49+
)
50+
)
5051
{
52+
// Extract the NodeTopicsInterface from the NodeT.
5153
using rclcpp::node_interfaces::get_node_topics_interface;
5254
auto node_topics = get_node_topics_interface(node);
5355

54-
std::shared_ptr<AllocatorT> allocator = options.allocator;
55-
if (!allocator) {
56-
allocator = std::make_shared<AllocatorT>();
57-
}
58-
59-
bool use_intra_process;
60-
switch (options.use_intra_process_comm) {
61-
case IntraProcessSetting::Enable:
62-
use_intra_process = true;
63-
break;
64-
case IntraProcessSetting::Disable:
65-
use_intra_process = false;
66-
break;
67-
case IntraProcessSetting::NodeDefault:
68-
use_intra_process = node_topics->get_node_base_interface()->get_use_intra_process_default();
69-
break;
70-
default:
71-
throw std::runtime_error("Unrecognized IntraProcessSetting value");
72-
break;
73-
}
74-
75-
// TODO(wjwwood): convert all of the interfaces to use QoS and PublisherOptionsBase
56+
// Create the publisher.
7657
auto pub = node_topics->create_publisher(
7758
topic_name,
78-
rclcpp::create_publisher_factory<MessageT, AllocatorT, PublisherT>(
79-
options.event_callbacks,
80-
allocator
81-
),
82-
options.template to_rcl_publisher_options<MessageT>(qos),
83-
use_intra_process
59+
rclcpp::create_publisher_factory<MessageT, AllocatorT, PublisherT>(options),
60+
qos
8461
);
62+
63+
// Add the publisher to the node topics interface.
8564
node_topics->add_publisher(pub, options.callback_group);
65+
8666
return std::dynamic_pointer_cast<PublisherT>(pub);
8767
}
8868

rclcpp/include/rclcpp/create_subscription.hpp

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ template<
4242
typename CallbackMessageT =
4343
typename rclcpp::subscription_traits::has_message_type<CallbackT>::type,
4444
typename SubscriptionT = rclcpp::Subscription<CallbackMessageT, AllocatorT>,
45+
typename MessageMemoryStrategyT = rclcpp::message_memory_strategy::MessageMemoryStrategy<
46+
CallbackMessageT,
47+
AllocatorT
48+
>,
4549
typename NodeT>
4650
typename std::shared_ptr<SubscriptionT>
4751
create_subscription(
@@ -52,48 +56,21 @@ create_subscription(
5256
const rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> & options = (
5357
rclcpp::SubscriptionOptionsWithAllocator<AllocatorT>()
5458
),
55-
typename rclcpp::message_memory_strategy::MessageMemoryStrategy<
56-
CallbackMessageT, AllocatorT>::SharedPtr
57-
msg_mem_strat = nullptr)
59+
typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = (
60+
MessageMemoryStrategyT::create_default()
61+
)
62+
)
5863
{
5964
using rclcpp::node_interfaces::get_node_topics_interface;
6065
auto node_topics = get_node_topics_interface(std::forward<NodeT>(node));
6166

62-
if (!msg_mem_strat) {
63-
using rclcpp::message_memory_strategy::MessageMemoryStrategy;
64-
msg_mem_strat = MessageMemoryStrategy<CallbackMessageT, AllocatorT>::create_default();
65-
}
67+
auto factory = rclcpp::create_subscription_factory<MessageT>(
68+
std::forward<CallbackT>(callback),
69+
options,
70+
msg_mem_strat
71+
);
6672

67-
std::shared_ptr<AllocatorT> allocator = options.allocator;
68-
if (!allocator) {
69-
allocator = std::make_shared<AllocatorT>();
70-
}
71-
auto factory = rclcpp::create_subscription_factory
72-
<MessageT, CallbackT, AllocatorT, CallbackMessageT, SubscriptionT>(
73-
std::forward<CallbackT>(callback), options.event_callbacks, msg_mem_strat, allocator);
74-
75-
bool use_intra_process;
76-
switch (options.use_intra_process_comm) {
77-
case IntraProcessSetting::Enable:
78-
use_intra_process = true;
79-
break;
80-
case IntraProcessSetting::Disable:
81-
use_intra_process = false;
82-
break;
83-
case IntraProcessSetting::NodeDefault:
84-
use_intra_process = node_topics->get_node_base_interface()->get_use_intra_process_default();
85-
break;
86-
default:
87-
throw std::runtime_error("Unrecognized IntraProcessSetting value");
88-
break;
89-
}
90-
91-
// TODO(wjwwood): convert all of the interfaces to use QoS and SubscriptionOptionsBase
92-
auto sub = node_topics->create_subscription(
93-
topic_name,
94-
factory,
95-
options.template to_rcl_subscription_options<MessageT>(qos),
96-
use_intra_process);
73+
auto sub = node_topics->create_subscription(topic_name, factory, qos);
9774
node_topics->add_subscription(sub, options.callback_group);
9875
return std::dynamic_pointer_cast<SubscriptionT>(sub);
9976
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Notice that headers in this folder should only provide symbols in the rclcpp::detail namespace.
2+
3+
Also that these headers are not considered part of the public API and are subject to change without notice.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2019 Open Source Robotics Foundation, Inc.
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 RCLCPP__DETAIL__RESOLVE_USE_INTRA_PROCESS_HPP_
16+
#define RCLCPP__DETAIL__RESOLVE_USE_INTRA_PROCESS_HPP_
17+
18+
#include <stdexcept>
19+
20+
#include "rclcpp/intra_process_setting.hpp"
21+
22+
namespace rclcpp
23+
{
24+
25+
namespace detail
26+
{
27+
28+
template<typename OptionsT, typename NodeBaseT>
29+
bool
30+
resolve_use_intra_process(const OptionsT & options, const NodeBaseT & node_base)
31+
{
32+
bool use_intra_process;
33+
switch (options.use_intra_process_comm) {
34+
case IntraProcessSetting::Enable:
35+
use_intra_process = true;
36+
break;
37+
case IntraProcessSetting::Disable:
38+
use_intra_process = false;
39+
break;
40+
case IntraProcessSetting::NodeDefault:
41+
use_intra_process = node_base.get_use_intra_process_default();
42+
break;
43+
default:
44+
throw std::runtime_error("Unrecognized IntraProcessSetting value");
45+
break;
46+
}
47+
48+
return use_intra_process;
49+
}
50+
51+
} // namespace detail
52+
53+
} // namespace rclcpp
54+
55+
#endif // RCLCPP__DETAIL__RESOLVE_USE_INTRA_PROCESS_HPP_

rclcpp/include/rclcpp/node.hpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,27 +192,29 @@ class Node : public std::enable_shared_from_this<Node>
192192
* \param[in] msg_mem_strat The message memory strategy to use for allocating messages.
193193
* \return Shared pointer to the created subscription.
194194
*/
195-
/* TODO(jacquelinekay):
196-
Windows build breaks when static member function passed as default
197-
argument to msg_mem_strat, nullptr is a workaround.
198-
*/
199195
template<
200196
typename MessageT,
201197
typename CallbackT,
202198
typename AllocatorT = std::allocator<void>,
203-
typename SubscriptionT = rclcpp::Subscription<
204-
typename rclcpp::subscription_traits::has_message_type<CallbackT>::type, AllocatorT>>
199+
typename CallbackMessageT =
200+
typename rclcpp::subscription_traits::has_message_type<CallbackT>::type,
201+
typename SubscriptionT = rclcpp::Subscription<CallbackMessageT, AllocatorT>,
202+
typename MessageMemoryStrategyT = rclcpp::message_memory_strategy::MessageMemoryStrategy<
203+
CallbackMessageT,
204+
AllocatorT
205+
>
206+
>
205207
std::shared_ptr<SubscriptionT>
206208
create_subscription(
207209
const std::string & topic_name,
208210
const rclcpp::QoS & qos,
209211
CallbackT && callback,
210212
const SubscriptionOptionsWithAllocator<AllocatorT> & options =
211213
SubscriptionOptionsWithAllocator<AllocatorT>(),
212-
typename rclcpp::message_memory_strategy::MessageMemoryStrategy<
213-
typename rclcpp::subscription_traits::has_message_type<CallbackT>::type, AllocatorT
214-
>::SharedPtr
215-
msg_mem_strat = nullptr);
214+
typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = (
215+
MessageMemoryStrategyT::create_default()
216+
)
217+
);
216218

217219
/// Create a timer.
218220
/**

rclcpp/include/rclcpp/node_impl.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,16 @@ template<
8383
typename MessageT,
8484
typename CallbackT,
8585
typename AllocatorT,
86-
typename SubscriptionT>
86+
typename CallbackMessageT,
87+
typename SubscriptionT,
88+
typename MessageMemoryStrategyT>
8789
std::shared_ptr<SubscriptionT>
8890
Node::create_subscription(
8991
const std::string & topic_name,
9092
const rclcpp::QoS & qos,
9193
CallbackT && callback,
9294
const SubscriptionOptionsWithAllocator<AllocatorT> & options,
93-
typename rclcpp::message_memory_strategy::MessageMemoryStrategy<
94-
typename rclcpp::subscription_traits::has_message_type<CallbackT>::type, AllocatorT>::SharedPtr
95-
msg_mem_strat)
95+
typename MessageMemoryStrategyT::SharedPtr msg_mem_strat)
9696
{
9797
return rclcpp::create_subscription<MessageT>(
9898
*this,

rclcpp/include/rclcpp/node_interfaces/node_topics.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ class NodeTopics : public NodeTopicsInterface
4949
create_publisher(
5050
const std::string & topic_name,
5151
const rclcpp::PublisherFactory & publisher_factory,
52-
const rcl_publisher_options_t & publisher_options,
53-
bool use_intra_process) override;
52+
const rclcpp::QoS & qos) override;
5453

5554
RCLCPP_PUBLIC
5655
void
@@ -63,8 +62,7 @@ class NodeTopics : public NodeTopicsInterface
6362
create_subscription(
6463
const std::string & topic_name,
6564
const rclcpp::SubscriptionFactory & subscription_factory,
66-
const rcl_subscription_options_t & subscription_options,
67-
bool use_intra_process) override;
65+
const rclcpp::QoS & qos) override;
6866

6967
RCLCPP_PUBLIC
7068
void

rclcpp/include/rclcpp/node_interfaces/node_topics_interface.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ class NodeTopicsInterface
5050
create_publisher(
5151
const std::string & topic_name,
5252
const rclcpp::PublisherFactory & publisher_factory,
53-
const rcl_publisher_options_t & publisher_options,
54-
bool use_intra_process) = 0;
53+
const rclcpp::QoS & qos) = 0;
5554

5655
RCLCPP_PUBLIC
5756
virtual
@@ -66,8 +65,7 @@ class NodeTopicsInterface
6665
create_subscription(
6766
const std::string & topic_name,
6867
const rclcpp::SubscriptionFactory & subscription_factory,
69-
const rcl_subscription_options_t & subscription_options,
70-
bool use_intra_process) = 0;
68+
const rclcpp::QoS & qos) = 0;
7169

7270
RCLCPP_PUBLIC
7371
virtual

0 commit comments

Comments
 (0)