curl-cpp
static c++17 wrapper for curl with -fno-exceptions support
curl_slist.hpp
1 #ifndef __curl_cpp_utils_curl_slist_HPP__
2 # define __curl_cpp_utils_curl_slist_HPP__
3 
4 # include <cstddef>
5 # include <memory>
6 # include <iterator>
7 
8 # include "../return-exception/ret-exception.hpp"
9 
10 namespace curl::utils {
11 /**
12  * Container wrapper for curl's struct curl_slist,
13  * which is a single-linked list storing
14  * string.
15  *
16  * Thread-safety:
17  * - If all threads access only const member function, then it is thread-safe;
18  * - Only one thread can call non-const member function at a time;
19  * - If another thread is modifing this class, other reader should wait until it is done.
20  */
21 class slist {
22 protected:
23  void *list = nullptr;
24 
25 public:
26  using value_type = const char*;
27 
28  using size_type = std::size_t;
29  using difference_type = std::ptrdiff_t;
30 
31  using reference = value_type&;
32  using const_reference = const value_type&;
33 
34  using pointer = value_type*;
35  using const_pointer = const value_type*;
36 
37  struct const_iterator {
38  const void *list_ptr = nullptr;
39 
40  using value_type = slist::value_type;
41  using pointer = value_type*;
42  using reference = value_type&;
43  using difference_type = std::ptrdiff_t;
44  using iterator_category = std::forward_iterator_tag;
45 
46  auto operator ++ () noexcept -> const_iterator&;
47  auto operator ++ (int) noexcept -> const_iterator;
48 
49  auto operator * () const noexcept -> value_type;
50 
51  friend bool operator == (const const_iterator &x, const const_iterator &y) noexcept;
52  friend bool operator != (const const_iterator &x, const const_iterator &y) noexcept;
53  };
54 
55  slist() = default;
56 
57  slist(void *l) noexcept;
58 
59  slist(const slist&) = delete;
60  slist(slist &&other) noexcept;
61 
62  slist& operator = (const slist&) = delete;
63  slist& operator = (slist &&other) noexcept;
64 
65  void swap(slist &other) noexcept;
66  /**
67  * Free all elements stored in the slist and the slist
68  * itself.
69  *
70  * Set protected member slist::list to nullptr.
71  */
72  void clear() noexcept;
73 
74  ~slist();
75 
76  bool is_empty() const noexcept;
77 
78  auto begin() const noexcept -> const_iterator;
79  auto end() const noexcept -> const_iterator;
80 
81  auto cbegin() const noexcept -> const_iterator;
82  auto cend() const noexcept -> const_iterator;
83 
84  /**
85  * Get underlying struct curl_slist* pointer.
86  */
87  auto get_underlying_ptr() const noexcept -> void*;
88 
89  /**
90  * @param str is dupped before adding to the list.
91  * Must not be CRLF-terminated for use in curl::Easy_ref_t::set_http_header.
92  */
93  auto push_back(const char *str) noexcept -> Ret_except<void, std::bad_alloc>;
94 };
95 } /* namespace curl::utils */
96 
97 #endif
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