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{
116117private:
117118 RCLCPP_DISABLE_COPY (IntraProcessManager);
119+
118120public:
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 */
0 commit comments