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:
parent
4a2e9d8af9
commit
ca5fb57126
3 changed files with 20 additions and 5 deletions
|
@ -36,7 +36,7 @@ public:
|
|||
Time(int32_t seconds, uint32_t nanoseconds, rcl_time_source_type_t clock = RCL_SYSTEM_TIME);
|
||||
|
||||
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
|
||||
Time(const Time & rhs);
|
||||
|
@ -51,17 +51,21 @@ public:
|
|||
operator builtin_interfaces::msg::Time() const;
|
||||
|
||||
RCLCPP_PUBLIC
|
||||
void
|
||||
Time &
|
||||
operator=(const Time & rhs);
|
||||
|
||||
RCLCPP_PUBLIC
|
||||
void
|
||||
Time &
|
||||
operator=(const builtin_interfaces::msg::Time & time_msg);
|
||||
|
||||
RCLCPP_PUBLIC
|
||||
bool
|
||||
operator==(const rclcpp::Time & rhs) const;
|
||||
|
||||
RCLCPP_PUBLIC
|
||||
bool
|
||||
operator!=(const rclcpp::Time & rhs) const;
|
||||
|
||||
RCLCPP_PUBLIC
|
||||
bool
|
||||
operator<(const rclcpp::Time & rhs) const;
|
||||
|
|
|
@ -139,15 +139,16 @@ Time::operator builtin_interfaces::msg::Time() const
|
|||
return msg_time;
|
||||
}
|
||||
|
||||
void
|
||||
Time &
|
||||
Time::operator=(const Time & rhs)
|
||||
{
|
||||
rcl_time_source_ = init_time_source(rhs.rcl_time_source_.type);
|
||||
rcl_time_ = init_time_point(rcl_time_source_);
|
||||
rcl_time_.nanoseconds = rhs.rcl_time_.nanoseconds;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void
|
||||
Time &
|
||||
Time::operator=(const builtin_interfaces::msg::Time & time_msg)
|
||||
{
|
||||
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 += time_msg.nanosec;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool
|
||||
|
@ -171,6 +173,12 @@ Time::operator==(const rclcpp::Time & rhs) const
|
|||
return rcl_time_.nanoseconds == rhs.rcl_time_.nanoseconds;
|
||||
}
|
||||
|
||||
bool
|
||||
Time::operator!=(const rclcpp::Time & rhs) const
|
||||
{
|
||||
return !(*this == rhs);
|
||||
}
|
||||
|
||||
bool
|
||||
Time::operator<(const rclcpp::Time & rhs) const
|
||||
{
|
||||
|
|
|
@ -93,6 +93,7 @@ TEST(TestTime, operators) {
|
|||
EXPECT_TRUE(old <= young);
|
||||
EXPECT_TRUE(young >= old);
|
||||
EXPECT_FALSE(young == old);
|
||||
EXPECT_TRUE(young != old);
|
||||
|
||||
rclcpp::Time add = old + young;
|
||||
EXPECT_EQ(add.nanoseconds(), old.nanoseconds() + young.nanoseconds());
|
||||
|
@ -106,6 +107,7 @@ TEST(TestTime, operators) {
|
|||
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));
|
||||
|
@ -117,6 +119,7 @@ TEST(TestTime, operators) {
|
|||
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));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue