Program Listing for File console.hpp

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

/*
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2020, Clearpath 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__CONSOLE_HPP_
#define FUSE_CORE__CONSOLE_HPP_

#include <chrono>

namespace fuse_core
{

// To replicate ROS 1 behavior, the throttle checking conditions were adapted from the logic here:
// https://github.com/ros/rosconsole/blob/c9503279e932a04b3d2667cca3d28a8133cacc22/include/ros/console.h
#if defined(_MSC_VER)
  #define FUSE_LIKELY(x)       (x)
  #define FUSE_UNLIKELY(x)     (x)
#else
  #define FUSE_LIKELY(x)       __builtin_expect((x), 1)
  #define FUSE_UNLIKELY(x)     __builtin_expect((x), 0)
#endif

class DelayedThrottleFilter
{
public:
  explicit DelayedThrottleFilter(const double period)
  : period_(std::chrono::duration<double, std::ratio<1>>(period))
  {
    reset();
  }

  bool isEnabled()
  {
    const auto now = std::chrono::time_point_cast<std::chrono::milliseconds>(
      std::chrono::system_clock::now());

    if (last_hit_.time_since_epoch().count() < 0.0) {
      last_hit_ = now;
      return true;
    }

    if (FUSE_UNLIKELY(last_hit_ + period_ <= now) || FUSE_UNLIKELY(now < last_hit_)) {
      last_hit_ = now;
      return true;
    }

    return false;
  }

  void reset()
  {
    last_hit_ = std::chrono::time_point_cast<std::chrono::milliseconds>(
      std::chrono::system_clock::from_time_t(-1));
  }

private:
  std::chrono::duration<double, std::ratio<1>> period_;

  std::chrono::time_point<std::chrono::system_clock> last_hit_;
};

}  // namespace fuse_core

#endif  // FUSE_CORE__CONSOLE_HPP_