Skip to content

iceoryx_utils/internal/units/duration.hpp🔗

Namespaces🔗

Name
iox
building block to easily create free function for logging in a library context
iox::units
iox::units::duration_literals

Classes🔗

Name
class iox::units::Duration

Source code🔗

// Copyright (c) 2019, 2021 by Robert Bosch GmbH, Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_UTILS_UNITS_DURATION_HPP
#define IOX_UTILS_UNITS_DURATION_HPP

#include "iceoryx_utils/cxx/expected.hpp"
#include "iceoryx_utils/cxx/smart_c.hpp"
#include "iceoryx_utils/platform/time.hpp" // required for QNX

#include <chrono>
#include <cmath>
#include <iostream>
#include <numeric>

namespace iox
{
namespace units
{
enum class TimeSpecReference
{
    None,
    Epoch,
    Monotonic
};

class Duration;

inline namespace duration_literals
{
constexpr Duration operator"" _ns(unsigned long long int) noexcept; // PRQA S 48

constexpr Duration operator"" _us(unsigned long long int) noexcept; // PRQA S 48

constexpr Duration operator"" _ms(unsigned long long int) noexcept; // PRQA S 48

constexpr Duration operator"" _s(unsigned long long int) noexcept; // PRQA S 48

constexpr Duration operator"" _m(unsigned long long int) noexcept; // PRQA S 48

constexpr Duration operator"" _h(unsigned long long int) noexcept; // PRQA S 48

constexpr Duration operator"" _d(unsigned long long int) noexcept; // PRQA S 48
} // namespace duration_literals

class Duration
{
  public:
    // BEGIN CREATION FROM STATIC FUNCTIONS

    template <typename T>
    static constexpr Duration fromNanoseconds(const T value) noexcept;

    template <typename T>
    static constexpr Duration fromMicroseconds(const T value) noexcept;

    template <typename T>
    static constexpr Duration fromMilliseconds(const T value) noexcept;

    template <typename T>
    static constexpr Duration fromSeconds(const T value) noexcept;

    template <typename T>
    static constexpr Duration fromMinutes(const T value) noexcept;

    template <typename T>
    static constexpr Duration fromHours(const T value) noexcept;

    template <typename T>
    static constexpr Duration fromDays(const T value) noexcept;

    // END CREATION FROM STATIC FUNCTIONS

    // BEGIN CONSTRUCTORS AND ASSIGNMENT

    constexpr explicit Duration(const struct timeval& value) noexcept;

    constexpr explicit Duration(const struct timespec& value) noexcept;

    constexpr explicit Duration(const struct itimerspec& value) noexcept;

    constexpr explicit Duration(const std::chrono::milliseconds& value) noexcept;

    constexpr explicit Duration(const std::chrono::nanoseconds& value) noexcept;

    Duration& operator=(const std::chrono::milliseconds& rhs) noexcept;

    // END CONSTRUCTORS AND ASSIGNMENT

    // BEGIN COMPARISON

    constexpr bool operator==(const Duration& rhs) const noexcept;

    constexpr bool operator!=(const Duration& rhs) const noexcept;

    constexpr bool operator<(const Duration& rhs) const noexcept;

    constexpr bool operator<=(const Duration& rhs) const noexcept;

    constexpr bool operator>(const Duration& rhs) const noexcept;

    constexpr bool operator>=(const Duration& rhs) const noexcept;

    // END COMPARISON

    // BEGIN ARITHMETIC

    constexpr Duration operator+(const Duration& rhs) const noexcept;

    constexpr Duration operator-(const Duration& rhs) const noexcept;

    template <typename T>
    constexpr Duration operator*(const T& rhs) const noexcept;

    // END ARITHMETIC

    // BEGIN CONVERSION

    constexpr uint64_t toNanoseconds() const noexcept;

    constexpr uint64_t toMicroseconds() const noexcept;

    constexpr uint64_t toMilliseconds() const noexcept;

    constexpr uint64_t toSeconds() const noexcept;

    constexpr uint64_t toMinutes() const noexcept;

    constexpr uint64_t toHours() const noexcept;

    constexpr uint64_t toDays() const noexcept;

    struct timespec timespec(const TimeSpecReference& reference = TimeSpecReference::None) const noexcept;

    constexpr operator struct timeval() const noexcept;

    // END CONVERSION

    friend constexpr Duration duration_literals::operator"" _ns(unsigned long long int) noexcept; // PRQA S 48
    friend constexpr Duration duration_literals::operator"" _us(unsigned long long int) noexcept; // PRQA S 48
    friend constexpr Duration duration_literals::operator"" _ms(unsigned long long int) noexcept; // PRQA S 48
    friend constexpr Duration duration_literals::operator"" _s(unsigned long long int) noexcept;  // PRQA S 48
    friend constexpr Duration duration_literals::operator"" _m(unsigned long long int) noexcept;  // PRQA S 48
    friend constexpr Duration duration_literals::operator"" _h(unsigned long long int) noexcept;  // PRQA S 48
    friend constexpr Duration duration_literals::operator"" _d(unsigned long long int) noexcept;  // PRQA S 48

    template <typename T>
    friend constexpr Duration operator*(const T& lhs, const Duration& rhs) noexcept;

    friend std::ostream& operator<<(std::ostream& stream, const Duration& t) noexcept;

    static constexpr uint32_t SECS_PER_MINUTE{60U};
    static constexpr uint32_t SECS_PER_HOUR{3600U};
    static constexpr uint32_t HOURS_PER_DAY{24U};

    static constexpr uint32_t MILLISECS_PER_SEC{1000U};
    static constexpr uint32_t MICROSECS_PER_SEC{MILLISECS_PER_SEC * 1000U};

    static constexpr uint32_t NANOSECS_PER_MICROSEC{1000U};
    static constexpr uint32_t NANOSECS_PER_MILLISEC{NANOSECS_PER_MICROSEC * 1000U};
    static constexpr uint32_t NANOSECS_PER_SEC{NANOSECS_PER_MILLISEC * 1000U};

  protected:
    using Seconds_t = uint64_t;
    using Nanoseconds_t = uint32_t;

    constexpr Duration(const Seconds_t seconds, const Nanoseconds_t nanoseconds) noexcept;

    static constexpr Duration createDuration(const Seconds_t seconds, const Nanoseconds_t nanoseconds) noexcept;

    static constexpr Duration max() noexcept;
    static constexpr Duration zero() noexcept;

  private:
    template <typename T, typename String>
    static constexpr unsigned long long int positiveValueOrClampToZero(const T value, const String fromMethod) noexcept;

    template <typename T>
    constexpr Duration fromFloatingPointSeconds(const T floatingPointSeconds) const noexcept;
    template <typename From, typename To>
    constexpr bool wouldCastFromFloatingPointProbablyOverflow(const From floatingPoint) const noexcept;

    template <typename T>
    constexpr Duration multiplyWith(const std::enable_if_t<!std::is_floating_point<T>::value, T>& rhs) const noexcept;

    template <typename T>
    constexpr Duration multiplyWith(const std::enable_if_t<std::is_floating_point<T>::value, T>& rhs) const noexcept;

  private:
    Seconds_t m_seconds{0U};
    Nanoseconds_t m_nanoseconds{0U};
};

template <typename T>
constexpr Duration operator*(const T& lhs, const Duration& rhs) noexcept;

std::ostream& operator<<(std::ostream& stream, const Duration& t) noexcept;

} // namespace units
} // namespace iox

#include "iceoryx_utils/internal/units/duration.inl"

#endif // IOX_UTILS_UNITS_DURATION_HPP

Updated on 31 May 2022 at 15:29:15 CEST