Program Listing for File message_buffer.hpp

Return to documentation for file (/tmp/ws/src/fuse/fuse_core/include/fuse_core/message_buffer.hpp)

/*
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2018, Locus Robotics
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of the copyright holder nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef FUSE_CORE__MESSAGE_BUFFER_HPP_
#define FUSE_CORE__MESSAGE_BUFFER_HPP_

#include <deque>
#include <utility>

#include <boost/range/any_range.hpp>
#include <fuse_core/fuse_macros.hpp>
#include <rclcpp/duration.hpp>
#include <rclcpp/time.hpp>

namespace fuse_core
{

template<typename Message>
class MessageBuffer
{
public:
  FUSE_SMART_PTR_DEFINITIONS(MessageBuffer<Message>)


  using message_range = boost::any_range<const std::pair<rclcpp::Time, Message>,
      boost::forward_traversal_tag>;

  using stamp_range = boost::any_range<const rclcpp::Time, boost::forward_traversal_tag>;

  explicit MessageBuffer(const rclcpp::Duration & buffer_length = rclcpp::Duration::max());

  virtual ~MessageBuffer() = default;

  const rclcpp::Duration & bufferLength() const
  {
    return buffer_length_;
  }

  void bufferLength(const rclcpp::Duration & buffer_length)
  {
    buffer_length_ = buffer_length;
  }

  void insert(const rclcpp::Time & stamp, const Message & msg);

  message_range query(
    const rclcpp::Time & beginning_stamp, const rclcpp::Time & ending_stamp,
    bool extended_range = true);

  stamp_range stamps() const;

protected:
  using Buffer = std::deque<std::pair<rclcpp::Time, Message>>;
  Buffer buffer_;
  rclcpp::Duration buffer_length_;

  static const rclcpp::Time & extractStamp(const typename Buffer::value_type & element)
  {
    return element.first;
  }

  void purgeHistory();
};

}  // namespace fuse_core

#include <fuse_core/message_buffer_impl.hpp>

#endif  // FUSE_CORE__MESSAGE_BUFFER_HPP_