Template Class Buffer
Defined in File buffer.hpp
Nested Relationships
Nested Types
Inheritance Relationships
Base Type
public fkie_message_filters::Filter< IO< Inputs... >, IO< Inputs... > >(Template Class Filter)
Class Documentation
-
template<class ...Inputs>
class Buffer : public fkie_message_filters::Filter<IO<Inputs...>, IO<Inputs...>> Store and forward data.
The buffer acts as a decoupler in the filter pipeline. Data can be stored and processed at a later time. The pipeline is effectively split into independent upstream and downstream parts, and it becomes possible to run the downstream data processing asynchronously. For instance, you can run computationally expensive algorithms on ROS messages in a different thread without blocking the ROS subscriber callback queue.
The buffer can be used for at least three different use cases:
Use the buffer as a valve You can toggle between the BufferPolicy::Discard and BufferPolicy::Passthru modes to selectively disable or enable data processing at specific times. This is the simplest use case without any asynchronous processing.
Run multiple ROS callback groups You can let the buffer processing happen in a dedicated ROS 2 callback group. This is the simplest way to deal with computationally expensive message processing:
namespace mf = fkie_message_filters; rclcpp::Node::SharedPtr node = ...; mf::Buffer<M> buf1(node, 10); // will create a callback group from node
Run your own thread(s) for data processing This is the most flexible option for advanced users. You can set up your worker threads as you desire and then call spin(), spin_once() or process_one() as you see fit.
namespace mf = fkie_message_filters; mf::Subscriber<M> sub; mf::Buffer<mf::Subscriber<M>::Output> buffer(mf::BufferPolicy::Queue, 10); mf::SimpleUserFilter<mf::Subscriber<M>::Output> flt; std::thread t([&buffer]{ buffer.spin(); }); flt.set_processing_function(...); mf::chain(sub, buffer, flt);
Public Types
Public Functions
-
Buffer(BufferPolicy policy = BufferPolicy::Discard, std::size_t max_queue_size = 1) noexcept
Constructor.
policythe buffer policymax_queue_sizefor the BufferPolicy::Queue policy, the maximum number of queued data items.
See also
set_policy(), set_callback_queue()
Does not throw any exceptions.
-
template<class NodeT>
Buffer(NodeT &&node, std::size_t max_queue_size) noexcept Constructor.
Constructs a buffer with BufferPolicy::Queue policy and data processing via ROS callbacks.
nodeROS node instance which will host a dedicated callback group for data processingmax_queue_sizethe maximum number of queued data items
Does not throw any exceptions.
-
virtual ~Buffer()
-
Connection connect_to_sink(Sink<Outputs...> &dst) noexcept
Connect this source to a sink.
Can be called multiple times to connect multiple sinks; in that case, the sinks receive data in the same order as they have been connected. This function does basically the same thing as Sink::connect_to_source(), only from the opposite point of view.
dstthe sink that is to be connected
Does not throw any exceptions.
- Returns:
a connection object that can be used to monitor or sever the created connection
-
Connection connect_to_source(Source<Inputs...> &src) noexcept
Connect this sink to a source.
Can be called multiple times to connect multiple sources; in that case, the sink receives data from all connected sources. This function does basically the same thing as Source::connect_to_sink(), only from the opposite point of view.
srcthe source that is to be connected
Does not throw any exceptions.
- Returns:
a connection object that can be used to monitor or sever the created connection
-
virtual void disconnect() noexcept override
Disconnect from all connected sources and sinks.
Convenience function that calls disconnect_from_all_sources() and disonnect_from_all_sinks().
Does not throw any exceptions.
-
void disconnect_from_all_sinks() noexcept
Disconnect from all connected sinks.
Severs the connection to all sinks, turning the send() method into a no-op.
Does not throw any exceptions.
-
void disconnect_from_all_sources() noexcept
Disconnect from all connected sources.
Severs the connection to all sources. The receive() method will not be called any more.
Does not throw any exceptions.
-
bool has_some() const noexcept
Check if the buffer has pending data.
Does not throw any exceptions.
- Return values:
true – if the current policy is BufferPolicy::Queue and a subsequent call to process_one() or spin_once() will process data.
false – otherwise
-
bool process_one()
Wait for and process one data item.
Does not throw any exceptions, but will propagate uncaught exceptions from filter callbacks.
- Return values:
true – data has been processed successfully
false – either the current buffer policy is not BufferPolicy::Queue, or the policy has been changed to something other than BufferPolicy::Queue while the function was waiting, or the ROS node has been shut down.
-
template<class Rep, class Period>
bool process_one(const std::chrono::duration<Rep, Period> &timeout) Wait for and process one data item.
timeoutmaximum duration to wait for new data if none is available
Does not throw any exceptions, but will propagate uncaught exceptions from filter callbacks.
- Return values:
true – data has been processed successfully
false – either the current buffer policy is not BufferPolicy::Queue, or the policy has been changed to something other than BufferPolicy::Queue while the function was waiting, or time timeout expired, or the ROS node has been shut down.
-
virtual void reset() noexcept override
Reset filter.
If the buffer policy is BufferPolicy::Queue, this will clear the internal queue and discard all pending data. Otherwise, this function has no effect.
Does not throw any exceptions.
-
template<class NodeT>
void set_node(NodeT &&node) Process data in a dedicated ROS callback group.
Instead of running your own processing threads, you can use the ROS callback system to schedule data processing whenever new data arrives.
nodethe ROS node ornullptrto disable ROS callbacks.
namespace mf = fkie_message_filters; mf::Buffer<...> buf; rclcpp::Node::SharedPtr node = ...; buf.set_node(node); rclcpp::spin(node);
Does not throw any exceptions, but will propagate exceptions from the ROS middleware layer.
-
void set_policy(BufferPolicy policy, std::size_t max_queue_size = 0)
Modify the buffer policy.
If the new buffer policy is not BufferPolicy::Queue, any pending call to wait(), process_one(), or spin() will return. If the buffer policy is changed to BufferPolicy::Passthru, all pending data is processed immediately before the function returns. If the buffer policy is changed to BufferPolicy::Discard, all pending data is discarded immediately.
policythe buffer policymax_queue_sizefor the BufferPolicy::Queue policy, the maximum number of queued data items. If zero, the previously set queue size remains unchanged.
Does not throw any exceptions, but will propagate uncaught exceptions from filter callbacks.
Warning
If you change the policy from BufferPolicy::Queue to BufferPolicy::Passthru and there is still a pending call to process_one(), spin_once(), or spin() in a different thread, some data might be processed in parallel or out of order when the queue is flushed.
-
void spin()
Process all data indefinitely.
Blocks and processes all incoming data until the buffer policy is changed to something other than BufferPolicy::Queue or the ROS node is shut down.
You can call the function from multiple threads at once, and the workload will be shared among all participating threads.
See also
Does not throw any exceptions, but will propagate uncaught exceptions from filter callbacks.
-
void spin_once()
Process pending data.
Does nothing if the buffer policy is not BufferPolicy::Queue. The method is guaranteed to return as it will only process data which is pending at invocation time. This also means that there may be new data pending already when this method returns.
Does not throw any exceptions, but will propagate uncaught exceptions from filter callbacks.
-
bool wait() noexcept
Wait for pending data.
Does not throw any exceptions.
- Return values:
true – there is data available for consumption by spin_once()
false – either the current buffer policy is not BufferPolicy::Queue, or the policy has been changed to something other than BufferPolicy::Queue while the function was waiting, or the ROS node has been shut down.
-
template<class Rep, class Period>
bool wait_for(const std::chrono::duration<Rep, Period> &timeout) noexcept Wait for pending data until timeout expires.
timeoutmaximum duration to wait for new data if none is available
Does not throw any exceptions.
- Return values:
true – there is data available for consumption by spin_once()
false – either the current buffer policy is not BufferPolicy::Queue, or the policy has been changed to something other than BufferPolicy::Queue while the function was waiting, or the timeout expired, or the ROS node has been shut down.
Public Static Attributes
-
static std::size_t NUM_INPUTS
Number of input arguments.
-
static std::size_t NUM_OUTPUTS
Number of output arguments.
Protected Functions
-
virtual void receive(helpers::argument_t<Inputs>... in) override
Process incoming data.
Derived classes need to override this method to handle all data that is to be consumed by the sink.
Depends on the implementation.
-
void send(helpers::argument_t<Outputs>... out)
Pass data to all connected sinks.
outdata
Does not throw any exceptions, but will propagate uncaught exceptions from filter callbacks.
-
struct Impl
Public Functions
-
inline Impl(Buffer *parent, BufferPolicy policy, std::size_t max_queue_size) noexcept
-
inline void adjust_capacity(std::size_t max_queue_size)
-
inline void insert_queue_element(std::unique_lock<std::mutex> &lock, QueueElement &e)
-
inline bool wait_for_queue_element(std::unique_lock<std::mutex> &lock) noexcept
Public Members
-
rclcpp::CallbackGroup::SharedPtr callback_group_
-
std::condition_variable cond_
-
std::size_t max_queue_size_
-
std::mutex mutex_
-
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_
-
rclcpp::node_interfaces::NodeWaitablesInterface::SharedPtr node_waitable_
-
BufferPolicy policy_
-
std::deque<QueueElement> queue_
-
class BufferCB : public rclcpp::Waitable
Public Types
Public Functions
-
inline ~BufferCB()
-
inline void add_to_wait_set(rcl_wait_set_t &wait_set) override
-
inline void clear_on_ready_callback() override
-
inline std::size_t get_number_of_ready_guard_conditions() override
-
inline std::vector<std::shared_ptr<rclcpp::TimerBase>> get_timers() const override
-
inline bool is_ready(const rcl_wait_set_t&) override
-
inline void set_on_ready_callback(std::function<void(std::size_t, int)>) override
-
inline std::shared_ptr<void> take_data() override
-
inline std::shared_ptr<void> take_data_by_entity_id(std::size_t id) override
-
inline void trigger()
-
inline ~BufferCB()
-
inline Impl(Buffer *parent, BufferPolicy policy, std::size_t max_queue_size) noexcept