summaryrefslogtreecommitdiff
path: root/include/mbgl/storage/request.hpp
blob: 1fb15c5a92b8705aca69befc0404705d371e0c60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifndef MBGL_STORAGE_DEFAULT_REQUEST
#define MBGL_STORAGE_DEFAULT_REQUEST

#include <mbgl/storage/resource.hpp>

#include <mbgl/util/util.hpp>
#include <mbgl/util/noncopyable.hpp>

#include <mutex>
#include <thread>
#include <functional>
#include <memory>

typedef struct uv_loop_s uv_loop_t;
namespace uv { class async; }

namespace mbgl {

class Response;

class Request : private util::noncopyable {
public:
    using Callback = std::function<void(const Response &)>;
    Request(const Resource &resource, uv_loop_t *loop, Callback callback);

public:
    // May be called from any thread.
    void notify(const std::shared_ptr<const Response> &response);
    void destruct();

    // May be called only from the thread the Request was created in.
    void cancel();

private:
    ~Request();
    void notifyCallback();

private:
    std::mutex mtx;
    bool canceled = false;
    bool confirmed = false;
    const std::unique_ptr<uv::async> async;
    Callback callback;
    std::shared_ptr<const Response> response;

public:
    const Resource resource;
};

}

#endif