#ifndef Magnum_Math_TimeStl_h #define Magnum_Math_TimeStl_h /* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024 Vladimír Vondruš Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** @file @brief STL @ref std::chrono compatibility for @ref Magnum::Math::Nanoseconds @m_since_latest Including this header allows you to convert a @ref Magnum::Math::Nanoseconds from and to @ref std::chrono::duration and @ref std::chrono::time_point. See @ref Math-Nanoseconds-stl for more information. */ #include #include "Magnum/Math/Time.h" namespace Magnum { namespace Math { namespace Implementation { /* There's no NanosecondsConverter because this is a typedef to some integral type, which when simply picks the Nanoseconds(Long) constructor and not a NanosecondsConverter. C types yay. */ template struct NanosecondsConverter>> { constexpr static Nanoseconds from(std::chrono::duration> other) { /* The Rep can be floating-point, truncate just the integral part but only after converting to nanoseconds */ return Nanoseconds{Long(other.count()*num*(1000000000ll/denom))}; } /* No to() because it can be an integer type, losing precision */ /** @todo add a floating-point-only to() once desirable */ }; template struct NanosecondsConverter> { constexpr static Nanoseconds from(std::chrono::duration other) { /* The Rep can be floating-point, truncate just the integral part -- we don't have anything for sub-nanosecond precision */ return Nanoseconds{Long(other.count())}; } constexpr static std::chrono::duration to(Nanoseconds other) { return std::chrono::duration{Long(other)}; } }; template struct NanosecondsConverter>>> { constexpr static Nanoseconds from(std::chrono::time_point>> other) { return Nanoseconds{other.time_since_epoch().count()*num*(1000000000ll/denom)}; } /* No to() because it's an integer type, losing precision */ /** @todo is there even any std::chrono::time_point that would use a FP duration type? */ }; template struct NanosecondsConverter>> { constexpr static Nanoseconds from(std::chrono::time_point> other) { return Nanoseconds{other.time_since_epoch().count()}; } constexpr static std::chrono::time_point> to(Nanoseconds other) { return std::chrono::time_point>{std::chrono::duration{Long(other)}}; } }; }}} #endif