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

Allow usage of lambdas as transport subscription callbacks #3309

Merged
merged 4 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 30 additions & 0 deletions gazebo/transport/Node.hh
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,36 @@ namespace gazebo
return result;
}

/// \brief Subscribe to a topic using a bost::function as the callback,
/// allowing to pass lambdas.
/// \param[in] _topic The topic to subscribe to
/// \param[in] _cb Function to be called on receipt of new message
/// \param[in] _latching If true, latch latest incoming message;
/// otherwise don't latch
/// \return Pointer to new Subscriber object
public: template<typename M>
SubscriberPtr Subscribe(const std::string &_topic,
const boost::function<void (const boost::shared_ptr<M const> &)> &_cb,
bool _latching = false)
{
SubscribeOptions ops;
std::string decodedTopic = this->DecodeTopicName(_topic);
ops.template Init<M>(decodedTopic, shared_from_this(), _latching);

{
boost::recursive_mutex::scoped_lock lock(this->incomingMutex);
this->callbacks[decodedTopic].push_back(
CallbackHelperPtr(new CallbackHelperT<M>(_cb, _latching)));
}

SubscriberPtr result =
transport::TopicManager::Instance()->Subscribe(ops);

result->SetCallbackId(this->callbacks[decodedTopic].back()->GetId());

return result;
}

/// \brief Subscribe to a topic using a bare function as the callback
/// \param[in] _topic The topic to subscribe to
/// \param[in] _fp Function to be called on receipt of new message
Expand Down
29 changes: 29 additions & 0 deletions test/integration/transport.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,35 @@ TEST_F(TransportTest, PubSub)
subs.clear();
}

// Standard pub/sub using lambdas
TEST_F(TransportTest, PubSubNoncapturingLambda)
{
Load("worlds/empty.world");

transport::NodePtr node = transport::NodePtr(new transport::Node());
node->Init();
transport::PublisherPtr scenePub = node->Advertise<msgs::Scene>("~/scene");
transport::SubscriberPtr sceneSub = node->Subscribe<msgs::Scene>("~/scene",
+[](ConstScenePtr & _msg) -> void {
g_sceneMsg=true;
}
);
}

TEST_F(TransportTest, PubSubCapturingLambda)
{
Load("worlds/empty.world");

transport::NodePtr node = transport::NodePtr(new transport::Node());
node->Init();
transport::PublisherPtr scenePub = node->Advertise<msgs::Scene>("~/scene");
transport::SubscriberPtr sceneSub = node->Subscribe<msgs::Scene>("~/scene",
[this](ConstScenePtr & _msg) -> void {
g_sceneMsg=true;
}
);
}

/////////////////////////////////////////////////
TEST_F(TransportTest, DirectPublish)
{
Expand Down