cmDuration.cxx 985 B

123456789101112131415161718192021222324252627
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #define CMDURATION_CPP
  4. #include "cmDuration.h"
  5. template <typename T>
  6. T cmDurationTo(const cmDuration& duration)
  7. {
  8. /* This works because the comparison operators for duration rely on
  9. * std::common_type.
  10. * So for example duration<int>::max() gets promoted to a duration<double>,
  11. * which can then be safely compared.
  12. */
  13. if (duration >= std::chrono::duration<T>::max()) {
  14. return std::chrono::duration<T>::max().count();
  15. }
  16. if (duration <= std::chrono::duration<T>::min()) {
  17. return std::chrono::duration<T>::min().count();
  18. }
  19. // Ensure number of seconds by defining ratio<1>
  20. return std::chrono::duration_cast<std::chrono::duration<T, std::ratio<1>>>(
  21. duration)
  22. .count();
  23. }
  24. template int cmDurationTo<int>(const cmDuration&);
  25. template unsigned int cmDurationTo<unsigned int>(const cmDuration&);