2021-03-26 23:18:51 +01:00
|
|
|
#include "cpr/timeout.h"
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
2021-08-22 19:28:37 +02:00
|
|
|
#include <type_traits>
|
2021-03-26 23:18:51 +01:00
|
|
|
|
|
|
|
namespace cpr {
|
|
|
|
|
|
|
|
// No way around since curl uses a long here.
|
|
|
|
// NOLINTNEXTLINE(google-runtime-int)
|
|
|
|
long Timeout::Milliseconds() const {
|
2021-08-22 19:28:37 +02:00
|
|
|
static_assert(std::is_same<std::chrono::milliseconds, decltype(ms)>::value, "Following casting expects milliseconds.");
|
2021-03-26 23:18:51 +01:00
|
|
|
|
|
|
|
// No way around since curl uses a long here.
|
|
|
|
// NOLINTNEXTLINE(google-runtime-int)
|
|
|
|
if (ms.count() > std::numeric_limits<long>::max()) {
|
2021-08-22 19:28:37 +02:00
|
|
|
throw std::overflow_error("cpr::Timeout: timeout value overflow: " + std::to_string(ms.count()) + " ms.");
|
2021-03-26 23:18:51 +01:00
|
|
|
}
|
|
|
|
// No way around since curl uses a long here.
|
|
|
|
// NOLINTNEXTLINE(google-runtime-int)
|
|
|
|
if (ms.count() < std::numeric_limits<long>::min()) {
|
2021-08-22 19:28:37 +02:00
|
|
|
throw std::underflow_error("cpr::Timeout: timeout value underflow: " + std::to_string(ms.count()) + " ms.");
|
2021-03-26 23:18:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// No way around since curl uses a long here.
|
|
|
|
// NOLINTNEXTLINE(google-runtime-int)
|
|
|
|
return static_cast<long>(ms.count());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cpr
|