From 0ccac1e3bd0f4314e11c86202c0c3ade57aed8ed Mon Sep 17 00:00:00 2001 From: Carl Delsey Date: Fri, 28 Jun 2019 09:43:09 -0700 Subject: [PATCH] Adding a factory method to create a Duration from seconds (#567) * Adding a factory method to create a Duration from seconds There are many places in the ROS codebase where a time duration is specified as a floating point number of seconds. A factory function to create a Duration object from these values makes the code a bit simpler in many cases. Signed-off-by: Carl Delsey * Adding some comments to clarify which constructors get matched. Signed-off-by: Carl Delsey --- rclcpp/include/rclcpp/duration.hpp | 6 ++++++ rclcpp/src/rclcpp/duration.cpp | 6 ++++++ rclcpp/test/test_duration.cpp | 17 +++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/rclcpp/include/rclcpp/duration.hpp b/rclcpp/include/rclcpp/duration.hpp index 7a80cb3..5e09cc5 100644 --- a/rclcpp/include/rclcpp/duration.hpp +++ b/rclcpp/include/rclcpp/duration.hpp @@ -28,10 +28,12 @@ class RCLCPP_PUBLIC Duration public: Duration(int32_t seconds, uint32_t nanoseconds); + // This constructor matches any numeric value - ints or floats explicit Duration(rcl_duration_value_t nanoseconds); explicit Duration(std::chrono::nanoseconds nanoseconds); + // This constructor matches any std::chrono value other than nanoseconds // intentionally not using explicit to create a conversion constructor template // cppcheck-suppress noExplicitConstructor @@ -94,6 +96,10 @@ public: double seconds() const; + // Create a duration object from a floating point number representing seconds + static Duration + from_seconds(double seconds); + template DurationT to_chrono() const diff --git a/rclcpp/src/rclcpp/duration.cpp b/rclcpp/src/rclcpp/duration.cpp index ca6007f..35db016 100644 --- a/rclcpp/src/rclcpp/duration.cpp +++ b/rclcpp/src/rclcpp/duration.cpp @@ -233,4 +233,10 @@ Duration::to_rmw_time() const return result; } +Duration +Duration::from_seconds(double seconds) +{ + return Duration(static_cast(RCL_S_TO_NS(seconds))); +} + } // namespace rclcpp diff --git a/rclcpp/test/test_duration.cpp b/rclcpp/test/test_duration.cpp index 2fbc701..b77f046 100644 --- a/rclcpp/test/test_duration.cpp +++ b/rclcpp/test/test_duration.cpp @@ -136,3 +136,20 @@ TEST(TestDuration, maximum_duration) { EXPECT_EQ(max_duration, max); } + +static const int64_t HALF_SEC_IN_NS = 500 * 1000 * 1000; +static const int64_t ONE_AND_HALF_SEC_IN_NS = 3 * HALF_SEC_IN_NS; + +TEST(TestDuration, from_seconds) { + EXPECT_EQ(rclcpp::Duration(0), rclcpp::Duration::from_seconds(0.0)); + EXPECT_EQ(rclcpp::Duration(0), rclcpp::Duration::from_seconds(0)); + EXPECT_EQ(rclcpp::Duration(1, HALF_SEC_IN_NS), rclcpp::Duration::from_seconds(1.5)); + EXPECT_EQ(rclcpp::Duration(-ONE_AND_HALF_SEC_IN_NS), rclcpp::Duration::from_seconds(-1.5)); +} + +TEST(TestDuration, std_chrono_constructors) { + EXPECT_EQ(rclcpp::Duration(0), rclcpp::Duration(0.0s)); + EXPECT_EQ(rclcpp::Duration(0), rclcpp::Duration(0s)); + EXPECT_EQ(rclcpp::Duration(1, HALF_SEC_IN_NS), rclcpp::Duration(1.5s)); + EXPECT_EQ(rclcpp::Duration(-1, 0), rclcpp::Duration(-1s)); +}