summaryrefslogtreecommitdiffstats
path: root/include/curlhandle.h
blob: 83d3d101165ab31a9ca3b251380188e78b7d094a (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
53
54
55
56
57
58
#ifndef NEWSBOAT_CURLHANDLE_H_
#define NEWSBOAT_CURLHANDLE_H_

#include <curl/curl.h>
#include <stdexcept>

namespace newsboat {

// wrapped curl handle for exception safety and so on
// see also: https://github.com/gsauthof/ccurl
class CurlHandle {
private:
	CURL* h;
	CurlHandle(const CurlHandle&) = delete;
	CurlHandle& operator=(const CurlHandle&) = delete;

	void cleanup()
	{
		if (h != nullptr) {
			curl_easy_cleanup(h);
		}
	}

public:
	CurlHandle()
		: h(curl_easy_init())
	{
		if (!h) {
			throw std::runtime_error("Can't obtain curl handle");
		}
	}
	~CurlHandle()
	{
		cleanup();
	}
	CurlHandle(CurlHandle&& other)
		: h(other.h)
	{
		other.h = nullptr;
	}
	CurlHandle& operator=(CurlHandle&& other)
	{
		cleanup();
		h = other.h;
		other.h = nullptr;
		return *this;
	}

	CURL* ptr()
	{
		return h;
	}
};

} // namespace newsboat

#endif /* NEWSBOAT_CURLHANDLE_H_ */