Thoth
A functional, expressive, asynchronous C++26 webdev library
Loading...
Searching...
No Matches
ListProxy.hpp
Go to the documentation of this file.
1#pragma once
2#include <expected>
6
7namespace Thoth::Http {
8 struct Headers;
9}
10
11namespace Thoth::Http::NHeaders {
12
13 template<bool IsConst, Serializable ...Ts>
14 struct ListProxy {
15 static constexpr std::size_t Count{ sizeof...(Ts) };
16 static constexpr bool Single{ sizeof...(Ts) == 1 };
17
18 static_assert(sizeof...(Ts) >= 1, "At least 1 type must be provided.");
19
20 using HeaderType = std::conditional_t<IsConst, const Headers, Headers>;
21 using PatternType = std::conditional_t<Single, std::string_view, std::array<std::string_view, Count>>;
22#ifdef __cpp_pack_indexing
23 using ElemType = std::conditional_t<Single, Ts...[0], std::variant<Ts...>>;
24 // Lets go Microslop, where is my pack indexing?
25#else
26 template<class F, class...> struct First { using Type = F; };
27 using ElemType = std::conditional_t<Single, typename First<Ts...>::Type, std::variant<Ts...>>;
28#endif
29
30 using Type = std::vector<ElemType>;
31
32 ListProxy(ListProxy&&) = delete;
33 ListProxy(const ListProxy&) = delete;
34
35 ListProxy(std::string_view key, HeaderType& headers, PatternType inPattern = {});
36
37 std::optional<Type> GetAsOpt() &&;
38 std::expected<Type, HeaderErrorEnum> Get() &&;
39 std::expected<Type, InvalidHeaderFormat> GetWithDefault(Type defaultValue) &&;
40
41 template<std::ranges::range R>
42 requires (!IsConst)
43 void operator=(R&& newValue) &&; // NOLINT(*-unconventional-assign-operator)
44
45 template<std::ranges::range R>
46 requires (!IsConst)
47 void Set(R&& newValue) &&;
48
49 template<class = void>
50 requires (!IsConst)
51 void Add(const ElemType& newItem) &&;
52
53 template<class = void>
54 requires (!IsConst)
55 bool TrySet(std::string_view newValue) &&;
56
57 template<class U>
58 static std::optional<std::vector<U>> ParseList(HeaderValue* val, std::string_view pattern);
59
60 private:
61 const PatternType inPattern;
62 const std::string_view key;
63 HeaderType& headers;
64 };
65}
66
67#include <Thoth/Http/NHeaders/Proxy/ListProxy.tpp>
Definition _base.hpp:5
std::string HeaderValue
Definition _base.hpp:9
Definition Client.hpp:12
Definition ListProxy.hpp:26
F Type
Definition ListProxy.hpp:26
Definition ListProxy.hpp:14
std::conditional_t< Single, std::string_view, std::array< std::string_view, Count > > PatternType
Definition ListProxy.hpp:21
bool TrySet(std::string_view newValue) &&
std::optional< Type > GetAsOpt() &&
std::expected< Type, HeaderErrorEnum > Get() &&
static constexpr std::size_t Count
Definition ListProxy.hpp:15
ListProxy(const ListProxy &)=delete
static std::optional< std::vector< U > > ParseList(HeaderValue *val, std::string_view pattern)
ListProxy(ListProxy &&)=delete
ListProxy(std::string_view key, HeaderType &headers, PatternType inPattern={})
std::conditional_t< IsConst, const Headers, Headers > HeaderType
Definition ListProxy.hpp:20
void Set(R &&newValue) &&
std::conditional_t< Single, typename First< Ts... >::Type, std::variant< Ts... > > ElemType
Definition ListProxy.hpp:27
std::vector< ElemType > Type
Definition ListProxy.hpp:30
static constexpr bool Single
Definition ListProxy.hpp:16
std::expected< Type, InvalidHeaderFormat > GetWithDefault(Type defaultValue) &&
void Add(const ElemType &newItem) &&