7 #include "arg_router/exception.hpp"
8 #include "arg_router/traits.hpp"
9 #include "arg_router/utility/string_view_ops.hpp"
11 #include <boost/lexical_cast/try_lexical_convert.hpp>
25 template <
typename T,
typename Enable =
void>
27 [[noreturn]] constexpr
static T parse([[maybe_unused]] std::string_view token) noexcept
29 static_assert(traits::always_false_v<T>,
30 "No parse function for this type, use a custom_parser policy "
31 "or define a parser<T>::parse(std::string_view) specialisation");
36 struct parser<T, typename std::enable_if_t<std::is_arithmetic_v<T>>> {
37 [[nodiscard]]
static T parse(std::string_view token)
39 using namespace utility::string_view_ops;
40 using namespace std::string_view_literals;
43 if (!boost::conversion::try_lexical_convert(token, result)) {
53 struct parser<T, typename std::enable_if_t<std::is_constructible_v<T, std::string_view>>> {
54 [[nodiscard]]
static T parse(std::string_view token) {
return T{token}; }
59 [[nodiscard]]
static inline bool parse(std::string_view token)
61 using namespace utility::string_view_ops;
62 using namespace std::string_view_literals;
64 constexpr
auto true_tokens = std::array{
73 constexpr
auto false_tokens = std::array{
82 const auto match = [&](
const auto& list) {
83 return std::find(list.begin(), list.end(), token) != list.end();
86 if (
match(true_tokens)) {
89 if (
match(false_tokens)) {
99 struct parser<std::optional<T>> {
100 [[nodiscard]]
static std::optional<T> parse(std::string_view token)
102 return parser<T>::parse(token);
109 template <
typename T>
111 typename std::enable_if_t<traits::has_push_back_method_v<T> &&
112 !traits::is_specialisation_of_v<T, std::basic_string> &&
113 !std::is_same_v<T, string>>> {
114 [[nodiscard]]
static typename T::value_type parse(std::string_view token)
116 return parser<typename T::value_type>::parse(token);
constexpr bool match(token_type token) noexcept
@ failed_to_parse
A value token could not be converted into its target value.