From 305e688a3a1d255fe34269cc327284275a0a3044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Mon, 14 Apr 2014 11:47:57 -0400 Subject: clean up os x + ios request code fixes #129 --- linux/main.cpp | 28 ++++++++++++++-------------- linux/request.cpp | 4 ++-- linux/request.hpp | 4 ++-- 3 files changed, 18 insertions(+), 18 deletions(-) (limited to 'linux') diff --git a/linux/main.cpp b/linux/main.cpp index 51d31bd0b1..bb32029bb8 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -11,7 +11,7 @@ MapView *mapView = nullptr; -std::forward_list requests; +std::forward_list> requests; @@ -74,14 +74,12 @@ int main(int argc, char *argv[]) { } namespace llmr { -namespace platform { -void cleanup() { +void platform::cleanup() { bool& dirty = mapView->dirty; - requests.remove_if([&dirty](llmr::platform::Request * req) { + requests.remove_if([&dirty](std::shared_ptr &req) { if (req->done) { req->foreground_callback(); - delete req; dirty = true; return true; } else { @@ -90,25 +88,27 @@ void cleanup() { }); } -void restart() { +void platform::restart() { if (mapView) { mapView->dirty = true; } } -Request *request_http(std::string url, std::function background_function, std::function foreground_callback) { - Request *req = new Request(url, background_function, foreground_callback); +std::shared_ptr +platform::request_http(const std::string &url, std::function background_function, + std::function foreground_callback) { + std::shared_ptr req = + std::make_shared(url, background_function, foreground_callback); requests.push_front(req); return req; } -void cancel_request_http(Request *request) { - for (Request *req : requests) { - if (req == request) { - req->cancel(); - } +// Cancels an HTTP request. +void platform::cancel_request_http(const std::shared_ptr &req) { + if (req) { + req->cancel(); } } -} + } diff --git a/linux/request.cpp b/linux/request.cpp index 651dcd2d15..5581957604 100644 --- a/linux/request.cpp +++ b/linux/request.cpp @@ -44,7 +44,7 @@ void Request::finish() { curl_share_cleanup(curl_share); } -Request::Request(std::string url, std::function bg, std::function fg) +Request::Request(std::string url, std::function bg, std::function fg) : done(false), cancelled(false), url(url), @@ -102,7 +102,7 @@ void Request::request(void *ptr) { curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &res.code); if (code != CURLE_ABORTED_BY_CALLBACK) { - req->background_function(res); + req->background_function(&res); } req->done = true; diff --git a/linux/request.hpp b/linux/request.hpp index 78348d4bfd..8350d9d459 100644 --- a/linux/request.hpp +++ b/linux/request.hpp @@ -14,7 +14,7 @@ struct Response; class Request { public: - Request(std::string url, std::function bg, std::function fg); + Request(std::string url, std::function bg, std::function fg); static void initialize(); static void finish(); @@ -36,7 +36,7 @@ public: std::atomic done; std::atomic cancelled; const std::string url; - const std::function background_function; + const std::function background_function; const std::function foreground_callback; private: -- cgit v1.2.1