1#ifndef CPR_CALLBACK_H
2#define CPR_CALLBACK_H
3
4#include "cprtypes.h"
5
6#include <atomic>
7#include <functional>
8#include <optional>
9#include <utility>
10
11namespace cpr {
12
13class ReadCallback {
14 public:
15 ReadCallback() = default;
16 // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
17 ReadCallback(std::function<bool(char* buffer, size_t& size, intptr_t userdata)> p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), size{-1}, callback{std::move(p_callback)} {}
18 ReadCallback(cpr_off_t p_size, std::function<bool(char* buffer, size_t& size, intptr_t userdata)> p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), size{p_size}, callback{std::move(p_callback)} {}
19 bool operator()(char* buffer, size_t& buffer_size) const {
20 return callback(buffer, buffer_size, userdata);
21 }
22
23 intptr_t userdata{};
24 cpr_off_t size{};
25 std::function<bool(char* buffer, size_t& size, intptr_t userdata)> callback;
26};
27
28class HeaderCallback {
29 public:
30 HeaderCallback() = default;
31 // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
32 HeaderCallback(std::function<bool(std::string header, intptr_t userdata)> p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), callback(std::move(p_callback)) {}
33 bool operator()(std::string header) const {
34 return callback(std::move(header), userdata);
35 }
36
37 intptr_t userdata{};
38 std::function<bool(std::string header, intptr_t userdata)> callback;
39};
40
41class WriteCallback {
42 public:
43 WriteCallback() = default;
44 // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
45 WriteCallback(std::function<bool(std::string data, intptr_t userdata)> p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), callback(std::move(p_callback)) {}
46 bool operator()(std::string data) const {
47 return callback(std::move(data), userdata);
48 }
49
50 intptr_t userdata{};
51 std::function<bool(std::string data, intptr_t userdata)> callback;
52};
53
54class ProgressCallback {
55 public:
56 ProgressCallback() = default;
57 // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
58 ProgressCallback(std::function<bool(cpr_pf_arg_t downloadTotal, cpr_pf_arg_t downloadNow, cpr_pf_arg_t uploadTotal, cpr_pf_arg_t uploadNow, intptr_t userdata)> p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), callback(std::move(p_callback)) {}
59 bool operator()(cpr_pf_arg_t downloadTotal, cpr_pf_arg_t downloadNow, cpr_pf_arg_t uploadTotal, cpr_pf_arg_t uploadNow) const {
60 return callback(downloadTotal, downloadNow, uploadTotal, uploadNow, userdata);
61 }
62
63 intptr_t userdata{};
64 std::function<bool(cpr_pf_arg_t downloadTotal, cpr_pf_arg_t downloadNow, cpr_pf_arg_t uploadTotal, cpr_pf_arg_t uploadNow, intptr_t userdata)> callback;
65};
66
67class DebugCallback {
68 public:
69 enum class InfoType {
70 TEXT = 0,
71 HEADER_IN = 1,
72 HEADER_OUT = 2,
73 DATA_IN = 3,
74 DATA_OUT = 4,
75 SSL_DATA_IN = 5,
76 SSL_DATA_OUT = 6,
77 };
78 DebugCallback() = default;
79 // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
80 DebugCallback(std::function<void(InfoType type, std::string data, intptr_t userdata)> p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), callback(std::move(p_callback)) {}
81 void operator()(InfoType type, std::string data) const {
82 callback(type, std::move(data), userdata);
83 }
84
85 intptr_t userdata{};
86 std::function<void(InfoType type, std::string data, intptr_t userdata)> callback;
87};
88
89/**
90 * Functor class for progress functions that will be used in cancellable requests.
91 */
92class CancellationCallback {
93 public:
94 CancellationCallback() = default;
95 explicit CancellationCallback(std::shared_ptr<std::atomic_bool>&& cs) : cancellation_state{std::move(cs)} {}
96
97 CancellationCallback(std::shared_ptr<std::atomic_bool>&& cs, ProgressCallback& u_cb) : cancellation_state{std::move(cs)}, user_cb{std::reference_wrapper{u_cb}} {}
98
99 bool operator()(cpr_pf_arg_t dltotal, cpr_pf_arg_t dlnow, cpr_pf_arg_t ultotal, cpr_pf_arg_t ulnow) const;
100
101 void SetProgressCallback(ProgressCallback& u_cb);
102
103 private:
104 std::shared_ptr<std::atomic_bool> cancellation_state;
105 std::optional<std::reference_wrapper<ProgressCallback>> user_cb;
106};
107
108
109} // namespace cpr
110
111#endif
112