curl-cpp
static c++17 wrapper for curl with -fno-exceptions support
curl_slist.cc
1 #include "curl_slist.hpp"
2 #include <curl/curl.h>
3 #include <utility>
4 
5 namespace curl::utils {
6 slist::slist(void *l) noexcept:
7  list{l}
8 {}
9 slist::slist(slist &&other) noexcept:
10  slist{}
11 {
12  (*this).swap(other);
13 }
14 slist& slist::operator = (slist &&other) noexcept
15 {
16  clear();
17  (*this).swap(other);
18 
19  return *this;
20 }
21 
22 void slist::swap(slist &other) noexcept
23 {
24  std::swap(list, other.list);
25 }
26 void slist::clear() noexcept
27 {
28  if (list)
29  curl_slist_free_all(static_cast<struct curl_slist*>(list));
30  list = nullptr;
31 }
32 
33 slist::~slist()
34 {
35  if (list)
36  curl_slist_free_all(static_cast<struct curl_slist*>(list));
37 }
38 
39 auto slist::const_iterator::operator ++ () noexcept -> const_iterator&
40 {
41  list_ptr = static_cast<const curl_slist*>(list_ptr)->next;
42  return *this;
43 }
44 auto slist::const_iterator::operator ++ (int) noexcept -> const_iterator
45 {
46  auto ret = *this;
47  ++(*this);
48  return ret;
49 }
50 auto slist::const_iterator::operator * () const noexcept -> value_type
51 {
52  return static_cast<const curl_slist*>(list_ptr)->data;
53 }
54 bool operator == (const slist::const_iterator &x, const slist::const_iterator &y) noexcept
55 {
56  return x.list_ptr == y.list_ptr;
57 }
58 bool operator != (const slist::const_iterator &x, const slist::const_iterator &y) noexcept
59 {
60  return x.list_ptr != y.list_ptr;
61 }
62 
63 auto slist::begin() const noexcept -> const_iterator
64 {
65  return {static_cast<struct curl_slist*>(list)};
66 }
67 auto slist::end() const noexcept -> const_iterator
68 {
69  return {};
70 }
71 
72 auto slist::cbegin() const noexcept -> const_iterator
73 {
74  return {static_cast<struct curl_slist*>(list)};
75 }
76 auto slist::cend() const noexcept -> const_iterator
77 {
78  return {};
79 }
80 
81 auto slist::get_underlying_ptr() const noexcept -> void*
82 {
83  return list;
84 }
85 
86 bool slist::is_empty() const noexcept
87 {
88  return list == nullptr;
89 }
90 
91 auto slist::push_back(const char *str) noexcept -> Ret_except<void, std::bad_alloc>
92 {
93  auto temp = curl_slist_append(static_cast<struct curl_slist*>(list), str);
94  if (!temp)
95  return {std::bad_alloc{}};
96 
97  list = temp;
98 
99  return {};
100 }
101 } /* namespace curl::utils */
curl::utils::slist::const_iterator
Definition: curl_slist.hpp:37
curl::utils::slist
Definition: curl_slist.hpp:21
curl::utils::slist::get_underlying_ptr
auto get_underlying_ptr() const noexcept -> void *
Definition: curl_slist.cc:81
curl::utils::slist::push_back
auto push_back(const char *str) noexcept -> Ret_except< void, std::bad_alloc >
Definition: curl_slist.cc:91
curl::utils::slist::clear
void clear() noexcept
Definition: curl_slist.cc:26