Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace const char * with a std::array<char, TOPIC_NAME_LENGTH> as the key of IPM IDTopicMap #671

Merged
merged 7 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions rclcpp/include/rclcpp/intra_process_manager_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef RCLCPP__INTRA_PROCESS_MANAGER_IMPL_HPP_
#define RCLCPP__INTRA_PROCESS_MANAGER_IMPL_HPP_

#include <rmw/validate_full_topic_name.h>
wjwwood marked this conversation as resolved.
Show resolved Hide resolved

#include <algorithm>
#include <atomic>
#include <cstring>
Expand Down Expand Up @@ -87,6 +89,20 @@ class IntraProcessManagerImplBase
RCLCPP_DISABLE_COPY(IntraProcessManagerImplBase)
};

class FixedSizeString
{
public:
explicit FixedSizeString(const char * str);
const char * c_str() const;

private:
char str_[RMW_TOPIC_MAX_NAME_LENGTH + 1];
};

RCLCPP_PUBLIC
bool
operator<(const FixedSizeString & lhs, const FixedSizeString & rhs);

template<typename Allocator = std::allocator<void>>
class IntraProcessManagerImpl : public IntraProcessManagerImplBase
{
Expand All @@ -98,9 +114,7 @@ class IntraProcessManagerImpl : public IntraProcessManagerImplBase
add_subscription(uint64_t id, SubscriptionBase::SharedPtr subscription)
{
subscriptions_[id] = subscription;
// subscription->get_topic_name() -> const char * can be used as the key,
// since subscriptions_ shares the ownership of subscription
subscription_ids_by_topic_[subscription->get_topic_name()].insert(id);
subscription_ids_by_topic_[FixedSizeString(subscription->get_topic_name())].insert(id);
}

void
Expand Down Expand Up @@ -175,7 +189,8 @@ class IntraProcessManagerImpl : public IntraProcessManagerImplBase
}

// Figure out what subscriptions should receive the message.
auto & destined_subscriptions = subscription_ids_by_topic_[publisher->get_topic_name()];
auto & destined_subscriptions =
subscription_ids_by_topic_[FixedSizeString(publisher->get_topic_name())];
wjwwood marked this conversation as resolved.
Show resolved Hide resolved
// Store the list for later comparison.
if (info.target_subscriptions_by_message_sequence.count(message_seq) == 0) {
info.target_subscriptions_by_message_sequence.emplace(
Expand Down Expand Up @@ -263,7 +278,7 @@ class IntraProcessManagerImpl : public IntraProcessManagerImplBase
if (!publisher) {
throw std::runtime_error("publisher has unexpectedly gone out of scope");
}
auto sub_map_it = subscription_ids_by_topic_.find(publisher->get_topic_name());
auto sub_map_it = subscription_ids_by_topic_.find(FixedSizeString(publisher->get_topic_name()));
if (sub_map_it == subscription_ids_by_topic_.end()) {
// No intraprocess subscribers
return 0;
Expand All @@ -285,19 +300,11 @@ class IntraProcessManagerImpl : public IntraProcessManagerImplBase
std::hash<uint64_t>, std::equal_to<uint64_t>,
RebindAlloc<std::pair<const uint64_t, SubscriptionBase::WeakPtr>>>;

struct strcmp_wrapper
{
bool
operator()(const char * lhs, const char * rhs) const
{
return std::strcmp(lhs, rhs) < 0;
}
};
using IDTopicMap = std::map<
const char *,
FixedSizeString,
AllocSet,
strcmp_wrapper,
RebindAlloc<std::pair<const char * const, AllocSet>>>;
std::less<FixedSizeString>,
RebindAlloc<std::pair<const FixedSizeString, AllocSet>>>;

SubscriptionMap subscriptions_;

Expand Down
22 changes: 21 additions & 1 deletion rclcpp/src/rclcpp/intra_process_manager_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,28 @@

#include <memory>

rclcpp::intra_process_manager::IntraProcessManagerImplBase::SharedPtr
using rclcpp::intra_process_manager::FixedSizeString;
using rclcpp::intra_process_manager::IntraProcessManagerImplBase;

IntraProcessManagerImplBase::SharedPtr
rclcpp::intra_process_manager::create_default_impl()
{
return std::make_shared<IntraProcessManagerImpl<>>();
}

FixedSizeString::FixedSizeString(const char * str)
{
std::strncpy(str_, str, RMW_TOPIC_MAX_NAME_LENGTH + 1);
}

const char * FixedSizeString::c_str() const
{
return str_;
}

bool rclcpp::intra_process_manager::operator<(
const FixedSizeString & lhs,
const FixedSizeString & rhs)
{
return std::strcmp(lhs.c_str(), rhs.c_str());
}