Skip to content

Commit

Permalink
Merge b031bb6 into f6c39f4
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixVanorder committed Apr 29, 2016
2 parents f6c39f4 + b031bb6 commit e8ab309
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions cpr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_library(${CPR_LIBRARIES}
payload.cpp
proxies.cpp
session.cpp
timeout.cpp
util.cpp

# Header files (useful in IDEs)
Expand Down
3 changes: 2 additions & 1 deletion cpr/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ void Session::Impl::SetHeader(const Header& header) {
void Session::Impl::SetTimeout(const Timeout& timeout) {
auto curl = curl_->handle;
if (curl) {
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout.ms);
long milliseconds = timeout.Milliseconds();
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, milliseconds);
}
}

Expand Down
26 changes: 26 additions & 0 deletions cpr/timeout.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "cpr/timeout.h"

#include <limits>
#include <stdexcept>
#include <string>
#include <type_traits>

namespace cpr {

long Timeout::Milliseconds() const {
static_assert(std::is_same<std::chrono::milliseconds, decltype(ms)>::value,
"Following casting expects milliseconds.");

if (ms.count() > std::numeric_limits<long>::max()) {
throw std::overflow_error("cpr::Timeout: timeout value overflow: " +
std::to_string(ms.count()) + " ms.");
}
if (ms.count() < std::numeric_limits<long>::min()) {
throw std::underflow_error("cpr::Timeout: timeout value underflow: " +
std::to_string(ms.count()) + " ms.");
}

return static_cast<long>(ms.count());
}

} // namespace cpr
9 changes: 7 additions & 2 deletions include/cpr/timeout.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#ifndef CPR_TIMEOUT_H
#define CPR_TIMEOUT_H

#include <chrono>
#include <cstdint>

namespace cpr {

class Timeout {
public:
Timeout(const std::int32_t& timeout) : ms(timeout) {}
Timeout(const std::chrono::milliseconds& duration) : ms{duration} {}
Timeout(const std::int32_t& milliseconds)
: Timeout{std::chrono::milliseconds(milliseconds)} {}

std::int32_t ms;
long Milliseconds() const;

std::chrono::milliseconds ms;
};

} // namespace cpr
Expand Down

0 comments on commit e8ab309

Please sign in to comment.