AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
paramConverter.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2026 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "anch-rest-common-export.hpp"
23
24#include <concepts>
25#include <optional>
26#include <ostream>
27#include <string>
28#include <string_view>
29#include <type_traits>
30
31#include "uuid.hpp"
32
33#include "rest/form.hpp"
34#include "rest/request.hpp"
35
36namespace anch::rest {
37
50 template<typename T>
52
58 template<typename T>
59 requires (std::integral<T> && !std::same_as<T, bool>)
60 struct ANCH_REST_COMMON_EXPORT ParamConverter<T> {
68 static std::optional<T> convert(std::string_view value) noexcept;
69 };
70
76 template<std::floating_point T>
77 struct ParamConverter<T> {
85 static std::optional<T> convert(std::string_view value) noexcept;
86 };
87
91 template<>
92 struct ParamConverter<bool> {
100 static std::optional<bool> convert(std::string_view value) noexcept;
101 };
102
106 template<>
107 struct ParamConverter<std::string> {
115 static std::optional<std::string> convert(std::string_view value);
116 };
117
121 template<>
130 static std::optional<anch::UUID> convert(std::string_view value);
131 };
132
138 template<typename T>
139 concept PathParameter = requires(std::string_view value) {
140 { anch::rest::ParamConverter<T>::convert(value) } -> std::same_as<std::optional<T>>;
141 };
142
157 template<anch::rest::PathParameter T>
158 std::optional<T> queryParameter(const anch::rest::Request& request, std::string_view name);
159
174 template<anch::rest::PathParameter T>
175 T requiredQueryParameter(const anch::rest::Request& request, std::string_view name);
176
192 template<anch::rest::PathParameter T>
193 std::optional<T> formField(const anch::rest::Form& form, std::string_view name);
194
210 template<anch::rest::PathParameter T>
211 T requiredFormField(const anch::rest::Form& form, std::string_view name);
212
213
222 template<typename T>
223 concept ParameterValue = std::convertible_to<const T&, std::string_view>
224 || std::is_arithmetic_v<T>
225 || requires(std::ostream& out, const T& value) { out << value; };
226
236 template<anch::rest::ParameterValue T>
237 std::string toParameterString(const T& value);
238
239}
240
241#include "rest/impl/paramConverter.hpp"
UUID generator and parser.
Definition uuid.hpp:51
Definition form.hpp:287
Definition request.hpp:50
Definition paramConverter.hpp:223
Definition paramConverter.hpp:139
AnCH REST library namespace.
Definition auth.hpp:29
std::string toParameterString(const T &value)
std::optional< T > formField(const anch::rest::Form &form, std::string_view name)
T requiredQueryParameter(const anch::rest::Request &request, std::string_view name)
T requiredFormField(const anch::rest::Form &form, std::string_view name)
std::optional< T > queryParameter(const anch::rest::Request &request, std::string_view name)
AnCH framework base namespace.
Definition app.hpp:30
static std::optional< T > convert(std::string_view value) noexcept
static std::optional< anch::UUID > convert(std::string_view value)
static std::optional< bool > convert(std::string_view value) noexcept
static std::optional< std::string > convert(std::string_view value)
Definition paramConverter.hpp:51