summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorBruno de Oliveira Abinader <bruno@mapbox.com>2015-11-25 13:30:32 +0200
committerBruno de Oliveira Abinader <bruno@mapbox.com>2015-11-27 17:47:02 +0200
commit31fd2822f516337f084f85db7e369d0633113b73 (patch)
tree9ad07f1bc0b3924f225ecfc628955bbd683e1d99 /platform
parent4475f5486000bab8f1121f5cec6091bc04f165f1 (diff)
downloadqtlocation-mapboxgl-31fd2822f516337f084f85db7e369d0633113b73.tar.gz
[core] Replace time_t with std::chrono::seconds
Added aliases for std::chrono typedefs (eg. 'Seconds' for std::chrono::seconds). These aliases are used together with templated helper functions to replace time_t with std::chrono::seconds for most cases, in particular for 'modified' and 'expires' values in Response.
Diffstat (limited to 'platform')
-rw-r--r--platform/android/http_request_android.cpp8
-rw-r--r--platform/darwin/http_request_nsurl.mm8
-rw-r--r--platform/default/asset_request_fs.cpp6
-rw-r--r--platform/default/asset_request_zip.cpp3
-rw-r--r--platform/default/http_request_curl.cpp8
-rw-r--r--platform/default/sqlite_cache.cpp12
-rw-r--r--platform/default/sqlite_cache_impl.hpp3
-rw-r--r--platform/node/src/node_request.cpp7
8 files changed, 30 insertions, 25 deletions
diff --git a/platform/android/http_request_android.cpp b/platform/android/http_request_android.cpp
index 1c00c5ee5a..c4deb6d11e 100644
--- a/platform/android/http_request_android.cpp
+++ b/platform/android/http_request_android.cpp
@@ -129,8 +129,8 @@ HTTPAndroidRequest::HTTPAndroidRequest(HTTPAndroidContext* context_, const Resou
if (existingResponse) {
if (!existingResponse->etag.empty()) {
etagStr = existingResponse->etag;
- } else if (existingResponse->modified) {
- modifiedStr = util::rfc1123(existingResponse->modified);
+ } else if (existingResponse->modified != Seconds::zero()) {
+ modifiedStr = util::rfc1123(existingResponse->modified.count());
}
}
@@ -195,11 +195,11 @@ void HTTPAndroidRequest::onResponse(int code, std::string message, std::string e
response = std::make_unique<Response>();
using Error = Response::Error;
- response->modified = parse_date(modified.c_str());
+ response->modified = Seconds(parse_date(modified.c_str()));
response->etag = etag;
response->expires = parseCacheControl(cacheControl.c_str());
if (!expires.empty()) {
- response->expires = parse_date(expires.c_str());
+ response->expires = Seconds(parse_date(expires.c_str()));
}
response->data = std::make_shared<std::string>(body);
diff --git a/platform/darwin/http_request_nsurl.mm b/platform/darwin/http_request_nsurl.mm
index 5c78bfc90f..71cccabacd 100644
--- a/platform/darwin/http_request_nsurl.mm
+++ b/platform/darwin/http_request_nsurl.mm
@@ -114,8 +114,8 @@ HTTPNSURLRequest::HTTPNSURLRequest(HTTPNSURLContext* context_,
if (!existingResponse->etag.empty()) {
[req addValue:@(existingResponse->etag.c_str())
forHTTPHeaderField:@"If-None-Match"];
- } else if (existingResponse->modified) {
- const std::string time = util::rfc1123(existingResponse->modified);
+ } else if (existingResponse->modified != Seconds::zero()) {
+ const std::string time = util::rfc1123(existingResponse->modified.count());
[req addValue:@(time.c_str()) forHTTPHeaderField:@"If-Modified-Since"];
}
}
@@ -214,12 +214,12 @@ void HTTPNSURLRequest::handleResult(NSData *data, NSURLResponse *res, NSError *e
NSString *expires = [headers objectForKey:@"Expires"];
if (expires) {
- response->expires = parse_date([expires UTF8String]);
+ response->expires = Seconds(parse_date([expires UTF8String]));
}
NSString *last_modified = [headers objectForKey:@"Last-Modified"];
if (last_modified) {
- response->modified = parse_date([last_modified UTF8String]);
+ response->modified = Seconds(parse_date([last_modified UTF8String]));
}
NSString *etag = [headers objectForKey:@"ETag"];
diff --git a/platform/default/asset_request_fs.cpp b/platform/default/asset_request_fs.cpp
index c169abf2b8..955f8d8020 100644
--- a/platform/default/asset_request_fs.cpp
+++ b/platform/default/asset_request_fs.cpp
@@ -130,12 +130,12 @@ void AssetRequest::fileStated(uv_fs_t *req) {
self->response = std::make_unique<Response>();
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
#ifdef __APPLE__
- self->response->modified = stat->st_mtimespec.tv_sec;
+ self->response->modified = Seconds(stat->st_mtimespec.tv_sec);
#else
- self->response->modified = stat->st_mtime;
+ self->response->modified = Seconds(stat->st_mtime);
#endif
#else
- self->response->modified = stat->st_mtim.tv_sec;
+ self->response->modified = Seconds(stat->st_mtim.tv_sec);
#endif
self->response->etag = util::toString(stat->st_ino);
const auto size = (unsigned int)(stat->st_size);
diff --git a/platform/default/asset_request_zip.cpp b/platform/default/asset_request_zip.cpp
index 2c0c54b6a0..b8007b5693 100644
--- a/platform/default/asset_request_zip.cpp
+++ b/platform/default/asset_request_zip.cpp
@@ -3,6 +3,7 @@
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/platform/log.hpp>
+#include <mbgl/util/chrono.hpp>
#include <mbgl/util/util.hpp>
#include <mbgl/util/uv.hpp>
@@ -187,7 +188,7 @@ void AssetRequest::fileStated(uv_zip_t *zip) {
// Get the modification time in case we have one.
if (zip->stat->valid & ZIP_STAT_MTIME) {
- response->modified = zip->stat->mtime;
+ response->modified = Seconds(zip->stat->mtime);
}
if (zip->stat->valid & ZIP_STAT_INDEX) {
diff --git a/platform/default/http_request_curl.cpp b/platform/default/http_request_curl.cpp
index 6d4011dd2b..eead6872ae 100644
--- a/platform/default/http_request_curl.cpp
+++ b/platform/default/http_request_curl.cpp
@@ -425,9 +425,9 @@ HTTPCURLRequest::HTTPCURLRequest(HTTPCURLContext* context_, const Resource& reso
if (!existingResponse->etag.empty()) {
const std::string header = std::string("If-None-Match: ") + existingResponse->etag;
headers = curl_slist_append(headers, header.c_str());
- } else if (existingResponse->modified) {
+ } else if (existingResponse->modified != Seconds::zero()) {
const std::string time =
- std::string("If-Modified-Since: ") + util::rfc1123(existingResponse->modified);
+ std::string("If-Modified-Since: ") + util::rfc1123(existingResponse->modified.count());
headers = curl_slist_append(headers, time.c_str());
}
}
@@ -525,7 +525,7 @@ size_t HTTPCURLRequest::headerCallback(char *const buffer, const size_t size, co
// Always overwrite the modification date; We might already have a value here from the
// Date header, but this one is more accurate.
const std::string value { buffer + begin, length - begin - 2 }; // remove \r\n
- baton->response->modified = curl_getdate(value.c_str(), nullptr);
+ baton->response->modified = Seconds(curl_getdate(value.c_str(), nullptr));
} else if ((begin = headerMatches("etag: ", buffer, length)) != std::string::npos) {
baton->response->etag = { buffer + begin, length - begin - 2 }; // remove \r\n
} else if ((begin = headerMatches("cache-control: ", buffer, length)) != std::string::npos) {
@@ -533,7 +533,7 @@ size_t HTTPCURLRequest::headerCallback(char *const buffer, const size_t size, co
baton->response->expires = parseCacheControl(value.c_str());
} else if ((begin = headerMatches("expires: ", buffer, length)) != std::string::npos) {
const std::string value { buffer + begin, length - begin - 2 }; // remove \r\n
- baton->response->expires = curl_getdate(value.c_str(), nullptr);
+ baton->response->expires = Seconds(curl_getdate(value.c_str(), nullptr));
}
return length;
diff --git a/platform/default/sqlite_cache.cpp b/platform/default/sqlite_cache.cpp
index 126e915e9b..1ca278b8f5 100644
--- a/platform/default/sqlite_cache.cpp
+++ b/platform/default/sqlite_cache.cpp
@@ -118,9 +118,9 @@ void SQLiteCache::Impl::get(const Resource &resource, Callback callback) {
// Status codes > 1 indicate an error
response->error = std::make_unique<Response::Error>(Response::Error::Reason(status));
}
- response->modified = getStmt->get<int64_t>(1);
+ response->modified = Seconds(getStmt->get<Seconds::rep>(1));
response->etag = getStmt->get<std::string>(2);
- response->expires = getStmt->get<int64_t>(3);
+ response->expires = Seconds(getStmt->get<Seconds::rep>(3));
response->data = std::make_shared<std::string>(std::move(getStmt->get<std::string>(4)));
if (getStmt->get<int>(5)) { // == compressed
response->data = std::make_shared<std::string>(std::move(util::decompress(*response->data)));
@@ -177,9 +177,9 @@ void SQLiteCache::Impl::put(const Resource& resource, std::shared_ptr<const Resp
putStmt->bind(2 /* status */, 1 /* success */);
}
putStmt->bind(3 /* kind */, int(resource.kind));
- putStmt->bind(4 /* modified */, response->modified);
+ putStmt->bind(4 /* modified */, response->modified.count());
putStmt->bind(5 /* etag */, response->etag.c_str());
- putStmt->bind(6 /* expires */, response->expires);
+ putStmt->bind(6 /* expires */, response->expires.count());
std::string data;
if (resource.kind != Resource::SpriteImage && response->data) {
@@ -208,7 +208,7 @@ void SQLiteCache::Impl::put(const Resource& resource, std::shared_ptr<const Resp
}
}
-void SQLiteCache::Impl::refresh(const Resource& resource, int64_t expires) {
+void SQLiteCache::Impl::refresh(const Resource& resource, Seconds expires) {
try {
if (!db) {
createDatabase();
@@ -226,7 +226,7 @@ void SQLiteCache::Impl::refresh(const Resource& resource, int64_t expires) {
}
const auto canonicalURL = util::mapbox::canonicalURL(resource.url);
- refreshStmt->bind(1, int64_t(expires));
+ refreshStmt->bind(1, expires.count());
refreshStmt->bind(2, canonicalURL.c_str());
refreshStmt->run();
} catch (mapbox::sqlite::Exception& ex) {
diff --git a/platform/default/sqlite_cache_impl.hpp b/platform/default/sqlite_cache_impl.hpp
index 700771f2e8..f557666e3f 100644
--- a/platform/default/sqlite_cache_impl.hpp
+++ b/platform/default/sqlite_cache_impl.hpp
@@ -2,6 +2,7 @@
#define MBGL_STORAGE_DEFAULT_SQLITE_CACHE_IMPL
#include <mbgl/storage/sqlite_cache.hpp>
+#include <mbgl/util/chrono.hpp>
namespace mapbox {
namespace sqlite {
@@ -19,7 +20,7 @@ public:
void get(const Resource&, Callback);
void put(const Resource& resource, std::shared_ptr<const Response> response);
- void refresh(const Resource& resource, int64_t expires);
+ void refresh(const Resource& resource, Seconds expires);
private:
void createDatabase();
diff --git a/platform/node/src/node_request.cpp b/platform/node/src/node_request.cpp
index 4575d42a7a..dd8f2336ca 100644
--- a/platform/node/src/node_request.cpp
+++ b/platform/node/src/node_request.cpp
@@ -1,5 +1,6 @@
#include "node_request.hpp"
#include <mbgl/storage/response.hpp>
+#include <mbgl/util/chrono.hpp>
#include <cmath>
#include <iostream>
@@ -43,6 +44,8 @@ v8::Handle<v8::Object> NodeRequest::Create(const mbgl::Resource& resource, mbgl:
NAN_METHOD(NodeRequest::Respond) {
using Error = mbgl::Response::Error;
+ using Milliseconds = mbgl::Milliseconds;
+
mbgl::Response response;
if (info.Length() < 1) {
@@ -63,14 +66,14 @@ NAN_METHOD(NodeRequest::Respond) {
if (Nan::Has(res, Nan::New("modified").ToLocalChecked()).FromJust()) {
const double modified = Nan::Get(res, Nan::New("modified").ToLocalChecked()).ToLocalChecked()->ToNumber()->Value();
if (!std::isnan(modified)) {
- response.modified = modified / 1000; // JS timestamps are milliseconds
+ response.modified = mbgl::asSeconds(Milliseconds(Milliseconds::rep(modified)));
}
}
if (Nan::Has(res, Nan::New("expires").ToLocalChecked()).FromJust()) {
const double expires = Nan::Get(res, Nan::New("expires").ToLocalChecked()).ToLocalChecked()->ToNumber()->Value();
if (!std::isnan(expires)) {
- response.expires = expires / 1000; // JS timestamps are milliseconds
+ response.expires = mbgl::asSeconds(Milliseconds(Milliseconds::rep(expires)));
}
}