Skip to content

Commit

Permalink
iox-eclipse-iceoryx#190 First step to refactor Duration by removing l…
Browse files Browse the repository at this point in the history
…ong double from user defined literals

Signed-off-by: Mathias Kraus <mathias.kraus@apex.ai>
  • Loading branch information
elBoberido committed Jan 16, 2021
1 parent f1c4b31 commit cd9d1ed
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 109 deletions.
71 changes: 27 additions & 44 deletions iceoryx_utils/include/iceoryx_utils/internal/units/duration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,45 +37,24 @@ class Duration;

inline namespace duration_literals
{
/// @brief constructs a new Duration object in nanoseconds
constexpr Duration operator"" _ns(long double);

/// @brief constructs a new Duration object in nanoseconds
constexpr Duration operator"" _ns(unsigned long long int); // PRQA S 48

/// @brief constructs a new Duration object in microseconds
constexpr Duration operator"" _us(long double);

/// @brief constructs a new Duration object in microseconds
constexpr Duration operator"" _us(unsigned long long int); // PRQA S 48

/// @brief constructs a new Duration object in milliseconds
constexpr Duration operator"" _ms(long double);

/// @brief constructs a new Duration object in milliseconds
constexpr Duration operator"" _ms(unsigned long long int); // PRQA S 48

/// @brief constructs a new Duration object in seconds
constexpr Duration operator"" _s(long double);

/// @brief constructs a new Duration object in seconds
constexpr Duration operator"" _s(unsigned long long int); // PRQA S 48

/// @brief constructs a new Duration object in minutes
constexpr Duration operator"" _m(long double);

/// @brief constructs a new Duration object in minutes
constexpr Duration operator"" _m(unsigned long long int); // PRQA S 48

/// @brief constructs a new Duration object in hours
constexpr Duration operator"" _h(long double);

/// @brief constructs a new Duration object in hours
constexpr Duration operator"" _h(unsigned long long int); // PRQA S 48

/// @brief constructs a new Duration object in days
constexpr Duration operator"" _d(long double);

/// @brief constructs a new Duration object in days
constexpr Duration operator"" _d(unsigned long long int); // PRQA S 48
} // namespace duration_literals
Expand Down Expand Up @@ -126,18 +105,30 @@ class Duration
/// @brief Assigns a std::chrono::milliseconds to an duration object
Duration& operator=(const std::chrono::milliseconds& right);

/// @brief return true if durationInSeconds is larger or equal than right
/// @brief Equal to operator
/// @return true if duration equal to right
constexpr bool operator==(const Duration& right) const;

/// @brief Not equal to operator
/// @return true if duration not equal to right
constexpr bool operator!=(const Duration& right) const;

/// @brief Less than operator
/// @return true if duration is less than right
constexpr bool operator<(const Duration& right) const;

/// @brief return true if durationInSeconds is larger or equal than right
/// @brief Less than or equal to operator
/// @return true if duration is less than or equal to right
constexpr bool operator<=(const Duration& right) const;

/// @brief Greater than operator
/// @return true if duration is greater than right
constexpr bool operator>(const Duration& right) const;

/// @brief return true if durationInSeconds is larger or equal than right
/// @brief Greater than or equal to operator
/// @return true if duration is greater than or equal to right
constexpr bool operator>=(const Duration& right) const;

/// @brief returns true if right is larger or equal than durationInSeconds
constexpr bool operator<=(const Duration& right) const;

/// @brief creates Duration object by adding right and durationInSeconds
constexpr Duration operator+(const Duration& right) const;

Expand Down Expand Up @@ -194,34 +185,26 @@ class Duration
/// @brief converts time in a timespec c struct
struct timespec timespec(const TimeSpecReference& reference = TimeSpecReference::None) const;

//@ brief Make operators accessible, that have to be defined outside the class
// template <typename T>
// friend constexpr Duration operator*(const T& left, const Duration& right);
// template <typename T>
// friend constexpr Duration operator/(const T& left, const Duration& right);
template <typename T>
friend constexpr Duration operator*(const T& left, const Duration& right);
template <typename T>
friend constexpr Duration operator/(const T& left, const Duration& right);
friend std::ostream& operator<<(std::ostream& stream, const Duration& t);
friend constexpr Duration duration_literals::operator"" _ns(long double);
friend constexpr Duration duration_literals::operator"" _ns(unsigned long long int); // PRQA S 48
friend constexpr Duration duration_literals::operator"" _us(long double);
friend constexpr Duration duration_literals::operator"" _us(unsigned long long int); // PRQA S 48
friend constexpr Duration duration_literals::operator"" _ms(long double);
friend constexpr Duration duration_literals::operator"" _ms(unsigned long long int); // PRQA S 48
friend constexpr Duration duration_literals::operator"" _s(long double);
friend constexpr Duration duration_literals::operator"" _s(unsigned long long int); // PRQA S 48
friend constexpr Duration duration_literals::operator"" _m(long double);
friend constexpr Duration duration_literals::operator"" _m(unsigned long long int); // PRQA S 48
friend constexpr Duration duration_literals::operator"" _h(long double);
friend constexpr Duration duration_literals::operator"" _h(unsigned long long int); // PRQA S 48
friend constexpr Duration duration_literals::operator"" _d(long double);
friend constexpr Duration duration_literals::operator"" _d(unsigned long long int); // PRQA S 48
friend constexpr Duration duration_literals::operator"" _s(unsigned long long int); // PRQA S 48
friend constexpr Duration duration_literals::operator"" _m(unsigned long long int); // PRQA S 48
friend constexpr Duration duration_literals::operator"" _h(unsigned long long int); // PRQA S 48
friend constexpr Duration duration_literals::operator"" _d(unsigned long long int); // PRQA S 48

private:
/// @brief constructor needs to be private to ensure a unit safe usage of duration
constexpr explicit Duration(const long double durationInSeconds);
long double durationInSeconds{0.0};
};

/// @brief creates Duration object multplying T with durationInSeconds
/// @brief creates Duration object multiplying T with durationInSeconds
template <typename T>
constexpr Duration operator*(const T& left, const Duration& right);

Expand Down
73 changes: 31 additions & 42 deletions iceoryx_utils/include/iceoryx_utils/internal/units/duration.inl
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,50 @@ namespace units
template <typename T>
constexpr Duration Duration::nanoseconds(const T ns)
{
static_assert(std::numeric_limits<T>::is_integer && std::is_unsigned<T>::value,
"only unsigned integer are supported");
return operator"" _ns(ns);
}
template <typename T>
constexpr Duration Duration::microseconds(const T us)
{
static_assert(std::numeric_limits<T>::is_integer && std::is_unsigned<T>::value,
"only unsigned integer are supported");
return operator"" _us(us);
}
template <typename T>
constexpr Duration Duration::milliseconds(const T ms)
{
static_assert(std::numeric_limits<T>::is_integer && std::is_unsigned<T>::value,
"only unsigned integer are supported");
return operator"" _ms(ms);
}
template <typename T>
constexpr Duration Duration::seconds(const T seconds)
{
static_assert(std::numeric_limits<T>::is_integer && std::is_unsigned<T>::value,
"only unsigned integer are supported");
return operator"" _s(seconds);
}
template <typename T>
constexpr Duration Duration::minutes(const T min)
{
static_assert(std::numeric_limits<T>::is_integer && std::is_unsigned<T>::value,
"only unsigned integer are supported");
return operator"" _m(min);
}
template <typename T>
constexpr Duration Duration::hours(const T hours)
{
static_assert(std::numeric_limits<T>::is_integer && std::is_unsigned<T>::value,
"only unsigned integer are supported");
return operator"" _h(hours);
}
template <typename T>
constexpr Duration Duration::days(const T days)
{
static_assert(std::numeric_limits<T>::is_integer && std::is_unsigned<T>::value,
"only unsigned integer are supported");
return operator"" _d(days);
}
inline constexpr Duration::Duration(const struct timeval& value)
Expand Down Expand Up @@ -140,11 +154,26 @@ inline constexpr Duration::operator timeval() const
return {this->seconds<SEC_TYPE>(), this->microSeconds<USEC_TYPE>() - this->seconds<USEC_TYPE>() * 1000000};
}

inline constexpr bool Duration::operator==(const Duration& right) const
{
return durationInSeconds == right.durationInSeconds;
}

inline constexpr bool Duration::operator!=(const Duration& right) const
{
return !(*this == right);
}

inline constexpr bool Duration::operator<(const Duration& right) const
{
return durationInSeconds < right.durationInSeconds;
}

inline constexpr bool Duration::operator<=(const Duration& right) const
{
return durationInSeconds <= right.durationInSeconds;
}

inline constexpr bool Duration::operator>(const Duration& right) const
{
return durationInSeconds > right.durationInSeconds;
Expand All @@ -155,11 +184,6 @@ inline constexpr bool Duration::operator>=(const Duration& right) const
return durationInSeconds >= right.durationInSeconds;
}

inline constexpr bool Duration::operator<=(const Duration& right) const
{
return durationInSeconds <= right.durationInSeconds;
}

inline constexpr Duration Duration::operator+(const Duration& right) const
{
return Duration{durationInSeconds + right.durationInSeconds};
Expand Down Expand Up @@ -194,71 +218,36 @@ inline constexpr Duration Duration::operator/(const T& right) const

inline namespace duration_literals
{
inline constexpr Duration operator"" _ns(long double value)
{
return Duration{value / 1000000000.0};
}

inline constexpr Duration operator"" _ns(unsigned long long int value) // PRQA S 48
{
return Duration{static_cast<long double>(value) / 1000000000.0};
}

inline constexpr Duration operator"" _us(long double value)
{
return Duration{value / 1000000.0};
}

inline constexpr Duration operator"" _us(unsigned long long int value) // PRQA S 48
{
return Duration{static_cast<long double>(value) / 1000000.0};
}

inline constexpr Duration operator"" _ms(long double value)
{
return Duration{value / 1000.0};
}

inline constexpr Duration operator"" _ms(unsigned long long int value) // PRQA S 48
{
return Duration{static_cast<long double>(value) / 1000.0};
}

inline constexpr Duration operator"" _s(long double value)
{
return Duration{value};
}

inline constexpr Duration operator"" _s(unsigned long long int value) // PRQA S 48
{
return Duration{static_cast<long double>(value)};
}

inline constexpr Duration operator"" _m(long double value)
{
return Duration{value * 60.0};
}

inline constexpr Duration operator"" _m(unsigned long long int value) // PRQA S 48
{
return Duration{static_cast<long double>(value) * 60.0};
}

inline constexpr Duration operator"" _h(long double value)
{
return Duration{value * 3600.0};
}

inline constexpr Duration operator"" _h(unsigned long long int value) // PRQA S 48
{
return Duration{static_cast<long double>(value) * 3600.0};
}

inline constexpr Duration operator"" _d(long double value)
{
return Duration{value * 24.0 * 3600.0};
}

inline constexpr Duration operator"" _d(unsigned long long int value) // PRQA S 48
{
return Duration{static_cast<long double>(value) * 24.0 * 3600.0};
Expand All @@ -269,13 +258,13 @@ inline constexpr Duration operator"" _d(unsigned long long int value) // PRQA S
template <typename T>
inline constexpr Duration operator*(const T& left, const Duration& right)
{
return Duration::seconds(static_cast<long double>(left) * right.seconds<long double>());
return Duration(static_cast<long double>(left) * right.seconds<long double>());
}

template <typename T>
inline constexpr Duration operator/(const T& left, const Duration& right)
{
return Duration::seconds(static_cast<long double>(left) / right.seconds<long double>());
return Duration(static_cast<long double>(left) / right.seconds<long double>());
}

} // namespace units
Expand Down
Loading

0 comments on commit cd9d1ed

Please sign in to comment.