Skip to content

Commit

Permalink
iox-#1394 Implement 'operator*=' with assignment to the 'Duration'
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Feb 15, 2023
1 parent e98a120 commit 19784fa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
12 changes: 12 additions & 0 deletions iceoryx_hoofs/test/moduletests/test_time_unit_duration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2072,6 +2072,18 @@ TEST(Duration_test, MultiplyDurationMoreThanOneSecondWithFloatResultsInMoreThanO
multiply(duration, MULTIPLICATOR, EXPECTED_DURATION);
}

TEST(Duration_test, MultiplyDurationWithSelfAssignOperatorWorks)
{
::testing::Test::RecordProperty("TEST_ID", "ac7e2f7e-984b-4aca-a472-9dc1f1c1f30c");
constexpr int64_t MULTIPLICATOR{3};
constexpr Duration EXPECTED_DURATION{6_s + 36_ns};
auto duration = 2_s + 12_ns;

duration *= MULTIPLICATOR;

EXPECT_THAT(duration, Eq(EXPECTED_DURATION));
}

TEST(Duration_test, MultiplyDurationWithFractionalFloat)
{
::testing::Test::RecordProperty("TEST_ID", "3adcaec4-06fb-4ae5-a05b-70764fc00d64");
Expand Down
10 changes: 10 additions & 0 deletions iceoryx_hoofs/time/include/iox/detail/duration.inl
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,16 @@ inline constexpr Duration Duration::operator*(const T rhs) const noexcept
return multiplyWith<T>(rhs);
}

template <typename T>
inline constexpr Duration& Duration::operator*=(const T rhs) noexcept
{
static_assert(std::is_arithmetic<T>::value, "non arithmetic types are not supported for multiplication");

*this = multiplyWith<T>(rhs);

return *this;
}

// AXIVION Next Construct AutosarC++19_03-A8.4.7 : Each argument is larger than two words
template <typename T>
inline constexpr Duration operator*(const T& lhs, const Duration& rhs) noexcept
Expand Down
17 changes: 14 additions & 3 deletions iceoryx_hoofs/time/include/iox/duration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class Duration

/// @brief Adds a Duration to itself. On overflow duration saturates to Duration::max().
/// @param[in] rhs is the second summand
/// @return a new Duration object
/// @return a reference to itself
// AXIVION Next Line AutosarC++19_03-A8.4.7 : Argument is larger than two words
constexpr Duration& operator+=(const Duration& rhs) noexcept;

Expand All @@ -220,7 +220,7 @@ class Duration

/// @brief Subtracts a Duration from itself. On underflow duration saturates to Duration::zero().
/// @param[in] rhs is the subtrahend
/// @return a new Duration object
/// @return a reference to itself
/// @attention Since negative durations are not allowed, the duration will be clamped to 0
// AXIVION Next Line AutosarC++19_03-A8.4.7 : Argument is larger than two words
constexpr Duration& operator-=(const Duration& rhs) noexcept;
Expand All @@ -234,10 +234,21 @@ class Duration
/// @note There is no explicit division operator! This can be achieved by multiplication with the inverse of the
/// divisor.
/// @note Multiplication of a non-zero duration with NaN and +Inf results in a saturated max duration
// AXIVION Next Construct AutosarC++19_03-M5.17.1 : Self assignment with 'operator*=' is not supported for now
template <typename T>
constexpr Duration operator*(const T rhs) const noexcept;

/// @brief Multiplies a Duration with an arithmetic type and assigns the result to itself.
/// @tparam T is an arithmetic type for the multiplicator
/// @param[in] rhs is the multiplicator
/// @return a reference to itself
/// @attention Since negative durations are not allowed, the duration will be clamped to 0
/// @note A duration of 0 will always result in 0, no matter if multiplied with NaN or +Inf
/// @note There is no explicit division operator! This can be achieved by multiplication with the inverse of the
/// divisor.
/// @note Multiplication of a non-zero duration with NaN and +Inf results in a saturated max duration
template <typename T>
constexpr Duration& operator*=(const T rhs) noexcept;

// END ARITHMETIC

// BEGIN CONVERSION
Expand Down

0 comments on commit 19784fa

Please sign in to comment.