Skip to content

Commit 12b939c

Browse files
committed
fixes to address comments and CI failures
1 parent aedc494 commit 12b939c

10 files changed

Lines changed: 264 additions & 181 deletions

rclcpp/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ if(AMENT_ENABLE_TESTING)
2222

2323
ament_add_gtest(test_mapped_ring_buffer test/test_mapped_ring_buffer.cpp)
2424
ament_add_gtest(test_intra_process_manager test/test_intra_process_manager.cpp)
25-
target_include_directories(test_intra_process_manager PUBLIC "${rcl_interfaces_INCLUDE_DIRS}")
25+
if(TARGET test_intra_process_manager)
26+
target_include_directories(test_intra_process_manager PUBLIC "${rcl_interfaces_INCLUDE_DIRS}")
27+
endif()
2628
endif()
2729

2830
ament_package(

rclcpp/include/rclcpp/context.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ class Context
5151
auto it = sub_contexts_.find(type_i);
5252
if (it == sub_contexts_.end()) {
5353
// It doesn't exist yet, make it
54+
// *INDENT-OFF* (prevent uncrustify from making unecessary indents here)
5455
sub_context = std::shared_ptr<SubContext>(
5556
new SubContext(std::forward<Args>(args) ...),
5657
[] (SubContext * sub_context_ptr) {
5758
delete sub_context_ptr;
5859
});
60+
// *INDENT-ON*
5961
sub_contexts_[type_i] = sub_context;
6062
} else {
6163
// It exists, get it out and cast it.

rclcpp/include/rclcpp/intra_process_manager.hpp

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
#include <rclcpp/subscription.hpp>
2222

2323
#include <algorithm>
24-
#include <cassert>
24+
#include <atomic>
2525
#include <cstdint>
26+
#include <exception>
2627
#include <limits>
2728
#include <map>
2829
#include <unordered_map>
@@ -115,6 +116,7 @@ class IntraProcessManager
115116
{
116117
private:
117118
RCLCPP_DISABLE_COPY(IntraProcessManager);
119+
118120
public:
119121
RCLCPP_SMART_PTR_DEFINITIONS(IntraProcessManager);
120122

@@ -129,8 +131,8 @@ class IntraProcessManager
129131
*
130132
* This method will allocate memory.
131133
*
132-
* /param subscription the Subscription to register.
133-
* /return an unsigned 64-bit integer which is the subscription's unique id.
134+
* \param subscription the Subscription to register.
135+
* \return an unsigned 64-bit integer which is the subscription's unique id.
134136
*/
135137
uint64_t
136138
add_subscription(subscription::SubscriptionBase::SharedPtr subscription)
@@ -144,7 +146,7 @@ class IntraProcessManager
144146
/// Unregister a subscription using the subscription's unique id.
145147
/* This method does not allocate memory.
146148
*
147-
* /param intra_process_subscription_id id of the subscription to remove.
149+
* \param intra_process_subscription_id id of the subscription to remove.
148150
*/
149151
void
150152
remove_subscription(uint64_t intra_process_subscription_id)
@@ -180,19 +182,21 @@ class IntraProcessManager
180182
*
181183
* This method will allocate memory.
182184
*
183-
* /param publisher publisher to be registered with the manager.
184-
* /param buffer_size if 0 (default) a size is calculated based on the QoS.
185-
* /return an unsigned 64-bit integer which is the publisher's unique id.
185+
* \param publisher publisher to be registered with the manager.
186+
* \param buffer_size if 0 (default) a size is calculated based on the QoS.
187+
* \return an unsigned 64-bit integer which is the publisher's unique id.
186188
*/
187189
template<typename MessageT>
188190
uint64_t
189-
add_publisher(publisher::Publisher::SharedPtr publisher, size_t buffer_size=0)
191+
add_publisher(publisher::Publisher::SharedPtr publisher, size_t buffer_size = 0)
190192
{
191193
auto id = IntraProcessManager::get_next_unique_id();
192194
publishers_[id].publisher = publisher;
193195
size_t size = buffer_size > 0 ? buffer_size : publisher->get_queue_size();
194196
// As long as the size of the ring buffer is less than the max sequence number, we're safe.
195-
assert(size <= std::numeric_limits<uint64_t>::max());
197+
if (size > std::numeric_limits<uint64_t>::max()) {
198+
throw std::invalid_argument("the calculated buffer size is too large");
199+
}
196200
publishers_[id].sequence_number.store(0);
197201
publishers_[id].buffer = mapped_ring_buffer::MappedRingBuffer<MessageT>::make_shared(size);
198202
publishers_[id].target_subscriptions_by_message_sequence.reserve(size);
@@ -202,7 +206,7 @@ class IntraProcessManager
202206
/// Unregister a publisher using the publisher's unique id.
203207
/* This method does not allocate memory.
204208
*
205-
* /param intra_process_publisher_id id of the publisher to remove.
209+
* \param intra_process_publisher_id id of the publisher to remove.
206210
*/
207211
void
208212
remove_publisher(uint64_t intra_process_publisher_id)
@@ -236,9 +240,9 @@ class IntraProcessManager
236240
*
237241
* This method does allocate memory.
238242
*
239-
* /param intra_process_publisher_id the id of the publisher of this message.
240-
* /param message the message that is being stored.
241-
* /return the message sequence number.
243+
* \param intra_process_publisher_id the id of the publisher of this message.
244+
* \param message the message that is being stored.
245+
* \return the message sequence number.
242246
*/
243247
template<typename MessageT>
244248
uint64_t
@@ -250,7 +254,7 @@ class IntraProcessManager
250254
if (it == publishers_.end()) {
251255
throw std::runtime_error("store_intra_process_message called with invalid publisher id");
252256
}
253-
publisher_info & info = it->second;
257+
PublisherInfo & info = it->second;
254258
// Calculate the next message sequence number.
255259
uint64_t message_seq = info.sequence_number.fetch_add(1, std::memory_order_relaxed);
256260
// Insert the message into the ring buffer using the message_seq to identify it.
@@ -309,10 +313,10 @@ class IntraProcessManager
309313
*
310314
* This method may allocate memory to copy the stored message.
311315
*
312-
* /param intra_process_publisher_id the id of the message's publisher.
313-
* /param message_sequence_number the sequence number of the message.
314-
* /param requesting_subscriptions_intra_process_id the subscription's id.
315-
* /param message the message typed unique_ptr used to return the message.
316+
* \param intra_process_publisher_id the id of the message's publisher.
317+
* \param message_sequence_number the sequence number of the message.
318+
* \param requesting_subscriptions_intra_process_id the subscription's id.
319+
* \param message the message typed unique_ptr used to return the message.
316320
*/
317321
template<typename MessageT>
318322
void
@@ -323,7 +327,7 @@ class IntraProcessManager
323327
std::unique_ptr<MessageT> & message)
324328
{
325329
message = nullptr;
326-
publisher_info * info;
330+
PublisherInfo * info;
327331
{
328332
auto it = publishers_.find(intra_process_publisher_id);
329333
if (it == publishers_.end()) {
@@ -377,9 +381,11 @@ class IntraProcessManager
377381
// So around 585 million years. Even at 1 GHz, it would take 585 years.
378382
// I think it's safe to avoid trying to handle overflow.
379383
// If we roll over then it's most likely a bug.
384+
// *INDENT-OFF* (prevent uncrustify from making unecessary indents here)
380385
throw std::overflow_error(
381386
"exhausted the unique id's for publishers and subscribers in this process "
382387
"(congratulations your computer is either extremely fast or extremely old)");
388+
// *INDENT-ON*
383389
}
384390
return next_id;
385391
}
@@ -389,23 +395,23 @@ class IntraProcessManager
389395
std::unordered_map<uint64_t, subscription::SubscriptionBase::WeakPtr> subscriptions_;
390396
std::map<std::string, std::set<uint64_t>> subscription_ids_by_topic_;
391397

392-
struct publisher_info
398+
struct PublisherInfo
393399
{
394-
RCLCPP_DISABLE_COPY(publisher_info);
400+
RCLCPP_DISABLE_COPY(PublisherInfo);
395401

396-
publisher_info() = default;
402+
PublisherInfo() = default;
397403

398404
publisher::Publisher::WeakPtr publisher;
399405
std::atomic<uint64_t> sequence_number;
400406
mapped_ring_buffer::MappedRingBufferBase::SharedPtr buffer;
401407
std::unordered_map<uint64_t, std::set<uint64_t>> target_subscriptions_by_message_sequence;
402408
};
403409

404-
std::unordered_map<uint64_t, publisher_info> publishers_;
410+
std::unordered_map<uint64_t, PublisherInfo> publishers_;
405411

406412
};
407413

408-
std::atomic<uint64_t> IntraProcessManager::next_unique_id_{1};
414+
std::atomic<uint64_t> IntraProcessManager::next_unique_id_ {1};
409415

410416
} /* namespace intra_process_manager */
411417
} /* namespace rclcpp */

rclcpp/include/rclcpp/mapped_ring_buffer.hpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ class MappedRingBuffer : public MappedRingBufferBase
5959
/// Constructor.
6060
/* The constructor will allocate memory while reserving space.
6161
*
62-
* /param size size of the ring buffer; must be positive and non-zero.
62+
* \param size size of the ring buffer; must be positive and non-zero.
6363
*/
64-
MappedRingBuffer(size_t size) : elements_(size), head_(0)
64+
MappedRingBuffer(size_t size)
65+
: elements_(size), head_(0)
6566
{
6667
if (size == 0) {
6768
throw std::invalid_argument("size must be a positive, non-zero value");
@@ -75,8 +76,10 @@ class MappedRingBuffer : public MappedRingBufferBase
7576
*
7677
* The key is not guaranteed to be unique, see the class docs for more.
7778
*
78-
* /param key the key associated with the stored value
79-
* /param value if the key is found, the value is stored in this parameter
79+
* The contents of value before the method is called are discarded.
80+
*
81+
* \param key the key associated with the stored value
82+
* \param value if the key is found, the value is stored in this parameter
8083
*/
8184
void
8285
get_copy_at_key(uint64_t key, std::unique_ptr<T> & value)
@@ -102,8 +105,10 @@ class MappedRingBuffer : public MappedRingBufferBase
102105
* originally stored object, since it was returned by the first call to this
103106
* method.
104107
*
105-
* /param key the key associated with the stored value
106-
* /param value if the key is found, the value is stored in this parameter
108+
* The contents of value before the method is called are discarded.
109+
*
110+
* \param key the key associated with the stored value
111+
* \param value if the key is found, the value is stored in this parameter
107112
*/
108113
void
109114
get_ownership_at_key(uint64_t key, std::unique_ptr<T> & value)
@@ -125,8 +130,10 @@ class MappedRingBuffer : public MappedRingBufferBase
125130
*
126131
* The key is not guaranteed to be unique, see the class docs for more.
127132
*
128-
* /param key the key associated with the stored value
129-
* /param value if the key is found, the value is stored in this parameter
133+
* The contents of value before the method is called are discarded.
134+
*
135+
* \param key the key associated with the stored value
136+
* \param value if the key is found, the value is stored in this parameter
130137
*/
131138
void
132139
pop_at_key(uint64_t key, std::unique_ptr<T> & value)
@@ -147,8 +154,8 @@ class MappedRingBuffer : public MappedRingBufferBase
147154
* After insertion, if a pair was replaced, then value will contain ownership
148155
* of that displaced value. Otherwise it will be a nullptr.
149156
*
150-
* /param key the key associated with the value to be stored
151-
* /param value the value to store, and optionally the value displaced
157+
* \param key the key associated with the value to be stored
158+
* \param value the value to store, and optionally the value displaced
152159
*/
153160
bool
154161
push_and_replace(uint64_t key, std::unique_ptr<T> & value)
@@ -188,9 +195,11 @@ class MappedRingBuffer : public MappedRingBufferBase
188195
typename std::vector<element>::iterator
189196
get_iterator_of_key(uint64_t key)
190197
{
191-
auto it = std::find_if(elements_.begin(), elements_.end(), [key] (element & e) -> bool {
198+
// *INDENT-OFF* (prevent uncrustify from making unecessary indents here)
199+
auto it = std::find_if(elements_.begin(), elements_.end(), [key](element & e) -> bool {
192200
return e.key == key && e.in_use;
193201
});
202+
// *INDENT-ON*
194203
return it;
195204
}
196205

rclcpp/include/rclcpp/node.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class Node
200200
private:
201201
RCLCPP_DISABLE_COPY(Node);
202202

203-
static const rosidl_message_type_support_t * ipm_ts;
203+
static const rosidl_message_type_support_t * ipm_ts_;
204204

205205
bool
206206
group_in_node(callback_group::CallbackGroup::SharedPtr & group);
@@ -312,10 +312,8 @@ class Node
312312
}
313313
};
314314

315-
const rosidl_message_type_support_t * Node::ipm_ts =
316-
rosidl_generator_cpp::get_message_type_support_handle<
317-
rcl_interfaces::msg::IntraProcessMessage
318-
>();
315+
const rosidl_message_type_support_t * Node::ipm_ts_ =
316+
rosidl_generator_cpp::get_message_type_support_handle<rcl_interfaces::msg::IntraProcessMessage>();
319317

320318
} /* namespace node */
321319
} /* namespace rclcpp */

rclcpp/include/rclcpp/node_impl.hpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ using namespace rclcpp::node;
4343

4444
Node::Node(const std::string & node_name, bool use_intra_process_comms)
4545
: Node(
46-
node_name,
47-
rclcpp::contexts::default_context::get_global_default_context(),
48-
use_intra_process_comms)
46+
node_name,
47+
rclcpp::contexts::default_context::get_global_default_context(),
48+
use_intra_process_comms)
4949
{}
5050

5151
Node::Node(
@@ -87,13 +87,15 @@ Node::Node(
8787
// *INDENT-ON*
8888
}
8989
// Initialize node handle shared_ptr with custom deleter.
90+
// *INDENT-OFF*
9091
node_handle_.reset(node, [](rmw_node_t * node) {
9192
auto ret = rmw_destroy_node(node);
9293
if (ret != RMW_RET_OK) {
9394
fprintf(
9495
stderr, "Error in destruction of rmw node handle: %s\n", rmw_get_error_string_safe());
9596
}
9697
});
98+
// *INDENT-ON*
9799

98100
using rclcpp::callback_group::CallbackGroupType;
99101
default_callback_group_ = create_callback_group(
@@ -135,7 +137,7 @@ Node::create_publisher(
135137

136138
if (use_intra_process_comms_) {
137139
rmw_publisher_t * intra_process_publisher_handle = rmw_create_publisher(
138-
node_handle_.get(), ipm_ts, (topic_name + "__intra").c_str(), qos_profile);
140+
node_handle_.get(), ipm_ts_, (topic_name + "__intra").c_str(), qos_profile);
139141
if (!intra_process_publisher_handle) {
140142
// *INDENT-OFF* (prevent uncrustify from making unecessary indents here)
141143
throw std::runtime_error(
@@ -149,8 +151,9 @@ Node::create_publisher(
149151
uint64_t intra_process_publisher_id =
150152
intra_process_manager->add_publisher<MessageT>(publisher);
151153
rclcpp::intra_process_manager::IntraProcessManager::WeakPtr weak_ipm = intra_process_manager;
154+
// *INDENT-OFF*
152155
auto shared_publish_callback =
153-
[weak_ipm] (uint64_t publisher_id, std::shared_ptr<void> msg) -> uint64_t
156+
[weak_ipm](uint64_t publisher_id, std::shared_ptr<void> msg) -> uint64_t
154157
{
155158
auto ipm = weak_ipm.lock();
156159
if (!ipm) {
@@ -163,6 +166,7 @@ Node::create_publisher(
163166
uint64_t message_seq = ipm->store_intra_process_message(publisher_id, unique_msg);
164167
return message_seq;
165168
};
169+
// *INDENT-ON*
166170
publisher->setup_intra_process(
167171
intra_process_publisher_id,
168172
shared_publish_callback,
@@ -225,7 +229,7 @@ Node::create_subscription(
225229
// Setup intra process.
226230
if (use_intra_process_comms_) {
227231
rmw_subscription_t * intra_process_subscriber_handle = rmw_create_subscription(
228-
node_handle_.get(), ipm_ts,
232+
node_handle_.get(), ipm_ts_,
229233
(topic_name + "__intra").c_str(), qos_profile, false);
230234
if (!subscriber_handle) {
231235
// *INDENT-OFF* (prevent uncrustify from making unecessary indents here)
@@ -238,10 +242,11 @@ Node::create_subscription(
238242
rclcpp::intra_process_manager::IntraProcessManager::WeakPtr weak_ipm = intra_process_manager;
239243
uint64_t intra_process_subscription_id =
240244
intra_process_manager->add_subscription(sub_base_ptr);
245+
// *INDENT-OFF*
241246
sub->setup_intra_process(
242247
intra_process_subscription_id,
243248
intra_process_subscriber_handle,
244-
[weak_ipm] (
249+
[weak_ipm](
245250
uint64_t publisher_id,
246251
uint64_t message_sequence,
247252
uint64_t subscription_id,
@@ -255,6 +260,7 @@ Node::create_subscription(
255260
}
256261
ipm->take_intra_process_message(publisher_id, message_sequence, subscription_id, message);
257262
});
263+
// *INDENT-ON*
258264
}
259265
// Assign to a group.
260266
if (group) {

rclcpp/include/rclcpp/publisher.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ namespace publisher
4242
class Publisher
4343
{
4444
friend rclcpp::node::Node;
45+
4546
public:
4647
RCLCPP_SMART_PTR_DEFINITIONS(Publisher);
4748

@@ -107,7 +108,7 @@ class Publisher
107108
}
108109
}
109110

110-
std::string
111+
const std::string &
111112
get_topic_name() const
112113
{
113114
return topic_;
@@ -119,7 +120,8 @@ class Publisher
119120
return queue_size_;
120121
}
121122

122-
typedef std::function<uint64_t (uint64_t, std::shared_ptr<void>)> StoreSharedMessageCallbackT;
123+
typedef std::function<uint64_t(uint64_t, std::shared_ptr<void>)> StoreSharedMessageCallbackT;
124+
123125
protected:
124126
void
125127
setup_intra_process(

0 commit comments

Comments
 (0)