Program Listing for File callback_wrapper.hpp

Return to documentation for file (/tmp/ws/src/fuse/fuse_core/include/fuse_core/callback_wrapper.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__CALLBACK_WRAPPER_HPP_
#define FUSE_CORE__CALLBACK_WRAPPER_HPP_

#include <deque>
#include <functional>
#include <future>
#include <memory>

#include <rclcpp/rclcpp.hpp>

namespace fuse_core
{

class CallbackWrapperBase
{
public:
  virtual void call() = 0;
};

template<typename T>
class CallbackWrapper : public CallbackWrapperBase
{
public:
  using CallbackFunction = std::function<T(void)>;

  explicit CallbackWrapper(CallbackFunction callback)
  : callback_(callback)
  {
  }

  std::future<T> getFuture()
  {
    return promise_.get_future();
  }

  void call() override
  {
    promise_.set_value(callback_());
  }

private:
  CallbackFunction callback_;
  std::promise<T> promise_;
};

// Specialization to handle 'void' return types
// Specifically, promise_.set_value(callback_()) does not work if callback_() returns void.
template<>
inline void CallbackWrapper<void>::call()
{
  callback_();
  promise_.set_value();
}


class CallbackAdapter : public rclcpp::Waitable
{
public:
  explicit CallbackAdapter(std::shared_ptr<rclcpp::Context> context_ptr);

  size_t get_number_of_ready_guard_conditions() override;


  bool is_ready(rcl_wait_set_t * wait_set) override;


  void add_to_wait_set(rcl_wait_set_t * wait_set) override;

  std::shared_ptr<void> take_data() override;

  void execute(std::shared_ptr<void> & data) override;

  void addCallback(const std::shared_ptr<CallbackWrapperBase> & callback);

  void addCallback(std::shared_ptr<CallbackWrapperBase> && callback);

  void removeAllCallbacks();

private:
  rcl_guard_condition_t gc_;

  std::mutex queue_mutex_;
  std::deque<std::shared_ptr<CallbackWrapperBase>> callback_queue_;
};


}  // namespace fuse_core

#endif  // FUSE_CORE__CALLBACK_WRAPPER_HPP_