stdex Additional custom or not Standard C++ covered algorithms

Loading...
Searching...
No Matches
curl.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2016-2024 Amebis
4*/
5
6#pragma once
7
8#include "compat.hpp"
9#include <curl/curl.h>
10#include <string>
11#include <stdexcept>
12
13 namespace stdex
14{
18 class curl_runtime_error : public std::runtime_error
19 {
20 public:
26 curl_runtime_error(_In_ CURLcode num) :
27 runtime_error(curl_easy_strerror(num)),
28 m_num(num)
29 {}
30
37 curl_runtime_error(_In_ CURLcode num, _In_ const std::string& msg) :
38 runtime_error(msg + ": " + curl_easy_strerror(num)),
39 m_num(num)
40 {}
41
48 curl_runtime_error(_In_ CURLcode num, _In_z_ const char *msg) :
49 runtime_error(std::string(msg) + ": " + curl_easy_strerror(num)),
50 m_num(num)
51 {}
52
56 CURLcode number() const
57 {
58 return m_num;
59 }
60
61 protected:
62 CURLcode m_num;
63 };
64
68 struct curl_easy_cleanup_delete
69 {
73 void operator()(_In_ CURL* ptr) const
74 {
75 curl_easy_cleanup(ptr);
76 }
77 };
78
82 using curl = std::unique_ptr<CURL, curl_easy_cleanup_delete>;
83
87 struct curl_slist_free_all_delete
88 {
92 void operator()(_In_ struct ::curl_slist* ptr) const
93 {
94 curl_slist_free_all(ptr);
95 }
96 };
97
101 using curl_slist = std::unique_ptr<struct ::curl_slist, curl_slist_free_all_delete>;
102
106 class curl_initializer
107 {
108 public:
114 curl_initializer(_In_ long flags)
115 {
116 auto code = curl_global_init(flags);
117 if (code != CURLE_OK) _Unlikely_
118 throw curl_runtime_error(code, "CURL failed to initialize");
119 }
120
126 virtual ~curl_initializer()
127 {
128 curl_global_cleanup();
129 }
130 };
131}
stdex::curl_initializer
Context scope automatic CURL (un)initialization.
Definition curl.hpp:107
stdex::curl_initializer::~curl_initializer
virtual ~curl_initializer()
Uninitializes CURL.
Definition curl.hpp:126
stdex::curl_initializer::curl_initializer
curl_initializer(long flags)
Initializes the CURL library.
Definition curl.hpp:114
stdex::curl_runtime_error
CURL runtime error.
Definition curl.hpp:19
stdex::curl_runtime_error::curl_runtime_error
curl_runtime_error(CURLcode num, const char *msg)
Constructs an exception.
Definition curl.hpp:48
stdex::curl_runtime_error::curl_runtime_error
curl_runtime_error(CURLcode num)
Constructs an exception.
Definition curl.hpp:26
stdex::curl_runtime_error::m_num
CURLcode m_num
Numeric error code.
Definition curl.hpp:62
stdex::curl_runtime_error::number
CURLcode number() const
Returns the error number.
Definition curl.hpp:56
stdex::curl_runtime_error::curl_runtime_error
curl_runtime_error(CURLcode num, const std::string &msg)
Constructs an exception.
Definition curl.hpp:37
stdex::curl_easy_cleanup_delete
Deleter for unique_ptr using curl_easy_cleanup.
Definition curl.hpp:69
stdex::curl_easy_cleanup_delete::operator()
void operator()(CURL *ptr) const
Delete a pointer.
Definition curl.hpp:73
stdex::curl_slist_free_all_delete
Deleter for unique_ptr using curl_slist_free_all.
Definition curl.hpp:88
stdex::curl_slist_free_all_delete::operator()
void operator()(struct ::curl_slist *ptr) const
Delete a pointer.
Definition curl.hpp:92