Skip to content

Commit 0137559

Browse files
committed
Updating callback wrapper to use Waitables interface
1 parent 640dab8 commit 0137559

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

fuse_core/include/fuse_core/callback_wrapper.h

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#ifndef FUSE_CORE_CALLBACK_WRAPPER_H
3535
#define FUSE_CORE_CALLBACK_WRAPPER_H
3636

37-
#include <ros/callback_queue_interface.h>
37+
#include <rclcpp/waitable.hpp>
3838

3939
#include <functional>
4040
#include <future>
@@ -80,14 +80,14 @@ namespace fuse_core
8080
* std::vector<double> really_big_data(1000000);
8181
* auto callback = boost::make_shared<CallbackWrapper<double> >(
8282
* std::bind(&MyClass::processData, &my_object, std::ref(really_big_data)));
83-
* std::future<double> result = callback->getFuture();
83+
* std::future<double> result = callback->get_future();
8484
* ros::getGlobalCallbackQueue()->addCallback(callback);
8585
* result.wait();
8686
* ROS_INFO_STREAM("The result is: " << result.get());
8787
* @endcode
8888
*/
8989
template <typename T>
90-
class CallbackWrapper : public ros::CallbackInterface
90+
class CallbackWrapper : public rclcpp::Waitable
9191
{
9292
public:
9393
using CallbackFunction = std::function<T(void)>;
@@ -98,40 +98,64 @@ class CallbackWrapper : public ros::CallbackInterface
9898
* @param[in] callback The function to be called from the callback queue
9999
*/
100100
explicit CallbackWrapper(CallbackFunction callback) :
101+
executed_(false),
101102
callback_(callback)
102103
{
103104
}
104105

105106
/**
106-
* @brief Get a future<T> object that represents the function's return value
107+
* @brief Override of the Waitable method
107108
*/
108-
std::future<T> getFuture()
109+
void add_to_wait_set(rcl_wait_set_t *) override
109110
{
110-
return promise_.get_future();
111111
}
112112

113113
/**
114114
* @brief Call this function. This is used by the callback queue.
115115
*/
116-
CallResult call() override
116+
void execute(std::shared_ptr<void> &) override
117117
{
118+
executed_ = true;
118119
promise_.set_value(callback_());
119-
return Success;
120+
}
121+
122+
/**
123+
* @brief Get a future<T> object that represents the function's return value
124+
*/
125+
std::future<T> get_future()
126+
{
127+
return promise_.get_future();
128+
}
129+
130+
/**
131+
* @brief Override of the Waitable method
132+
*/
133+
bool is_ready(rcl_wait_set_t *) override
134+
{
135+
return executed_;
136+
}
137+
138+
/**
139+
* @brief Override of the Waitable method
140+
*/
141+
std::shared_ptr<void> take_data() override
142+
{
143+
return nullptr;
120144
}
121145

122146
private:
147+
std::atomic<bool> executed_;
123148
CallbackFunction callback_; //!< The function to execute within the
124149
std::promise<T> promise_; //!< Promise/Future used to return data after the callback is executed
125150
};
126151

127152
// Specialization to handle 'void' return types
128153
// Specifically, promise_.set_value(callback_()) does not work if callback_() returns void.
129154
template <>
130-
inline ros::CallbackInterface::CallResult CallbackWrapper<void>::call()
155+
inline void CallbackWrapper<void>::execute(std::shared_ptr<void> &)
131156
{
132157
callback_();
133158
promise_.set_value();
134-
return Success;
135159
}
136160

137161
} // namespace fuse_core

0 commit comments

Comments
 (0)