summaryrefslogtreecommitdiff
path: root/include/mbgl/storage/base_request.hpp
blob: c903742f4b193166580efdd3706ab4f1ae04ec67 (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_BASE_REQUEST
#define MBGL_STORAGE_BASE_REQUEST

#include <mbgl/util/ptr.hpp>

#include <string>
#include <forward_list>
#include <functional>


typedef struct uv_loop_s uv_loop_t;
typedef struct uv_async_s uv_async_t;

namespace mbgl {

class Response;
class Request;
using Callback = std::function<void(const Response &)>;


class BaseRequest {
private:
    // Make noncopyable and immovable
    BaseRequest(const BaseRequest &) = delete;
    BaseRequest(BaseRequest &&) = delete;
    BaseRequest& operator=(const BaseRequest &) = delete;
    BaseRequest& operator=(BaseRequest &&) = delete;

public:
    BaseRequest(const std::string &path);
    virtual ~BaseRequest();

    Callback *add(Callback &&callback, const util::ptr<BaseRequest> &request);
    void remove(Callback *callback);
    void notify();

public:
    const unsigned long thread_id;
    const std::string path;
    std::unique_ptr<Response> response;

private:
    // This object may hold a shared_ptr to itself. It does this to prevent destruction of this object
    // while a request is in progress.
    util::ptr<BaseRequest> self;

    std::forward_list<std::unique_ptr<Callback>> callbacks;
};

}

#endif