Improvements to rclcpp::Time (#375)

* enable Time to be trivially constructible

This is required to use it in a Qt Signal/Slot.

* operator= should return T &

See: https://stackoverflow.com/questions/9072169/why-should-the-assignment-operator-return-a-reference-to-the-object

* add operator!=
This commit is contained in:
William Woodall 2017-09-19 08:32:10 -07:00 committed by GitHub
parent 4a2e9d8af9
commit ca5fb57126
3 changed files with 20 additions and 5 deletions

View file

@ -36,7 +36,7 @@ public:
Time(int32_t seconds, uint32_t nanoseconds, rcl_time_source_type_t clock = RCL_SYSTEM_TIME); Time(int32_t seconds, uint32_t nanoseconds, rcl_time_source_type_t clock = RCL_SYSTEM_TIME);
RCLCPP_PUBLIC RCLCPP_PUBLIC
explicit Time(uint64_t nanoseconds, rcl_time_source_type_t clock = RCL_SYSTEM_TIME); explicit Time(uint64_t nanoseconds = 0, rcl_time_source_type_t clock = RCL_SYSTEM_TIME);
RCLCPP_PUBLIC RCLCPP_PUBLIC
Time(const Time & rhs); Time(const Time & rhs);
@ -51,17 +51,21 @@ public:
operator builtin_interfaces::msg::Time() const; operator builtin_interfaces::msg::Time() const;
RCLCPP_PUBLIC RCLCPP_PUBLIC
void Time &
operator=(const Time & rhs); operator=(const Time & rhs);
RCLCPP_PUBLIC RCLCPP_PUBLIC
void Time &
operator=(const builtin_interfaces::msg::Time & time_msg); operator=(const builtin_interfaces::msg::Time & time_msg);
RCLCPP_PUBLIC RCLCPP_PUBLIC
bool bool
operator==(const rclcpp::Time & rhs) const; operator==(const rclcpp::Time & rhs) const;
RCLCPP_PUBLIC
bool
operator!=(const rclcpp::Time & rhs) const;
RCLCPP_PUBLIC RCLCPP_PUBLIC
bool bool
operator<(const rclcpp::Time & rhs) const; operator<(const rclcpp::Time & rhs) const;

View file

@ -139,15 +139,16 @@ Time::operator builtin_interfaces::msg::Time() const
return msg_time; return msg_time;
} }
void Time &
Time::operator=(const Time & rhs) Time::operator=(const Time & rhs)
{ {
rcl_time_source_ = init_time_source(rhs.rcl_time_source_.type); rcl_time_source_ = init_time_source(rhs.rcl_time_source_.type);
rcl_time_ = init_time_point(rcl_time_source_); rcl_time_ = init_time_point(rcl_time_source_);
rcl_time_.nanoseconds = rhs.rcl_time_.nanoseconds; rcl_time_.nanoseconds = rhs.rcl_time_.nanoseconds;
return *this;
} }
void Time &
Time::operator=(const builtin_interfaces::msg::Time & time_msg) Time::operator=(const builtin_interfaces::msg::Time & time_msg)
{ {
if (time_msg.sec < 0) { if (time_msg.sec < 0) {
@ -159,6 +160,7 @@ Time::operator=(const builtin_interfaces::msg::Time & time_msg)
rcl_time_.nanoseconds = RCL_S_TO_NS(static_cast<uint64_t>(time_msg.sec)); rcl_time_.nanoseconds = RCL_S_TO_NS(static_cast<uint64_t>(time_msg.sec));
rcl_time_.nanoseconds += time_msg.nanosec; rcl_time_.nanoseconds += time_msg.nanosec;
return *this;
} }
bool bool
@ -171,6 +173,12 @@ Time::operator==(const rclcpp::Time & rhs) const
return rcl_time_.nanoseconds == rhs.rcl_time_.nanoseconds; return rcl_time_.nanoseconds == rhs.rcl_time_.nanoseconds;
} }
bool
Time::operator!=(const rclcpp::Time & rhs) const
{
return !(*this == rhs);
}
bool bool
Time::operator<(const rclcpp::Time & rhs) const Time::operator<(const rclcpp::Time & rhs) const
{ {

View file

@ -93,6 +93,7 @@ TEST(TestTime, operators) {
EXPECT_TRUE(old <= young); EXPECT_TRUE(old <= young);
EXPECT_TRUE(young >= old); EXPECT_TRUE(young >= old);
EXPECT_FALSE(young == old); EXPECT_FALSE(young == old);
EXPECT_TRUE(young != old);
rclcpp::Time add = old + young; rclcpp::Time add = old + young;
EXPECT_EQ(add.nanoseconds(), old.nanoseconds() + young.nanoseconds()); EXPECT_EQ(add.nanoseconds(), old.nanoseconds() + young.nanoseconds());
@ -106,6 +107,7 @@ TEST(TestTime, operators) {
rclcpp::Time steady_time(0, 0, RCL_STEADY_TIME); rclcpp::Time steady_time(0, 0, RCL_STEADY_TIME);
EXPECT_ANY_THROW((void)(system_time == steady_time)); EXPECT_ANY_THROW((void)(system_time == steady_time));
EXPECT_ANY_THROW((void)(system_time != steady_time));
EXPECT_ANY_THROW((void)(system_time <= steady_time)); EXPECT_ANY_THROW((void)(system_time <= steady_time));
EXPECT_ANY_THROW((void)(system_time >= steady_time)); EXPECT_ANY_THROW((void)(system_time >= steady_time));
EXPECT_ANY_THROW((void)(system_time < steady_time)); EXPECT_ANY_THROW((void)(system_time < steady_time));
@ -117,6 +119,7 @@ TEST(TestTime, operators) {
rclcpp::Time later = rclcpp::Time::now(RCL_STEADY_TIME); rclcpp::Time later = rclcpp::Time::now(RCL_STEADY_TIME);
EXPECT_ANY_THROW((void)(now == later)); EXPECT_ANY_THROW((void)(now == later));
EXPECT_ANY_THROW((void)(now != later));
EXPECT_ANY_THROW((void)(now <= later)); EXPECT_ANY_THROW((void)(now <= later));
EXPECT_ANY_THROW((void)(now >= later)); EXPECT_ANY_THROW((void)(now >= later));
EXPECT_ANY_THROW((void)(now < later)); EXPECT_ANY_THROW((void)(now < later));