diff --git a/include/ctre/return_type.hpp b/include/ctre/return_type.hpp index d3c93ab1..7829a45e 100644 --- a/include/ctre/return_type.hpp +++ b/include/ctre/return_type.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #ifdef _MSC_VER #include #endif @@ -127,6 +128,14 @@ template struct captured_content { std::from_chars(view.data(), view.data() + view.size(), result, std::forward(args)...); return result; } + + template constexpr CTRE_FORCE_INLINE auto to_optional_number(Ts && ... args) const noexcept -> std::optional { + if (!static_cast(*this)) { + return std::nullopt; + } + + return to_number(std::forward(args)...); + } #endif template constexpr CTRE_FORCE_INLINE auto to_view() const noexcept { @@ -138,6 +147,17 @@ template struct captured_content { return std::basic_string_view(data_unsafe(), static_cast(unit_size())); } + constexpr CTRE_FORCE_INLINE auto view() const noexcept { + return to_view(); + } + + template constexpr CTRE_FORCE_INLINE auto to_optional_view() const noexcept -> std::optional> { + if (!static_cast(*this)) { + return std::nullopt; + } + return to_view(); + } + constexpr CTRE_FORCE_INLINE std::basic_string to_string() const noexcept { #if __cpp_char8_t >= 201811 if constexpr (std::is_same_v) { @@ -150,11 +170,14 @@ template struct captured_content { #endif } - constexpr CTRE_FORCE_INLINE auto view() const noexcept { - return to_view(); + constexpr CTRE_FORCE_INLINE auto str() const noexcept { + return to_string(); } - constexpr CTRE_FORCE_INLINE auto str() const noexcept { + template constexpr CTRE_FORCE_INLINE auto to_optional_string() const noexcept -> std::optional> { + if (!static_cast(*this)) { + return std::nullopt; + } return to_string(); } @@ -166,6 +189,14 @@ template struct captured_content { return to_string(); } + constexpr CTRE_FORCE_INLINE operator std::optional>() const noexcept { + return to_optional_view(); + } + + constexpr CTRE_FORCE_INLINE explicit operator std::optional>() const noexcept { + return to_optional_string(); + } + constexpr CTRE_FORCE_INLINE static size_t get_id() noexcept { return Id; } @@ -378,6 +409,7 @@ template class regex_results { return bool(_captures.template select<0>()); } + // implicit conversions constexpr CTRE_FORCE_INLINE operator std::basic_string_view() const noexcept { return to_view(); } @@ -386,28 +418,55 @@ template class regex_results { return to_string(); } + constexpr CTRE_FORCE_INLINE operator std::optional>() const noexcept { + return to_optional_view(); + } + + constexpr CTRE_FORCE_INLINE explicit operator std::optional>() const noexcept { + return to_optional_string(); + } + + // conversion to numbers #if __has_include() template constexpr CTRE_FORCE_INLINE auto to_number(Ts && ... args) const noexcept -> R { return _captures.template select<0>().template to_number(std::forward(args)...); } + + template constexpr CTRE_FORCE_INLINE auto to_optional_number(Ts && ... args) const noexcept -> std::optional { + return _captures.template select<0>().template to_optional_number(std::forward(args)...); + } #endif + // conversion to basic_string_view constexpr CTRE_FORCE_INLINE auto to_view() const noexcept { return _captures.template select<0>().to_view(); } + constexpr CTRE_FORCE_INLINE auto view() const noexcept { + return _captures.template select<0>().view(); // this should be deprecated (??) + } + + constexpr CTRE_FORCE_INLINE auto to_optional_view() const noexcept { + return _captures.template select<0>().to_optional_view(); + } + + // conversion to basic_string constexpr CTRE_FORCE_INLINE auto to_string() const noexcept { return _captures.template select<0>().to_string(); } - constexpr CTRE_FORCE_INLINE auto view() const noexcept { - return _captures.template select<0>().view(); + constexpr CTRE_FORCE_INLINE auto str() const noexcept { + return _captures.template select<0>().to_string(); // this should be deprecated (??) } - constexpr CTRE_FORCE_INLINE auto str() const noexcept { - return _captures.template select<0>().to_string(); + constexpr CTRE_FORCE_INLINE auto to_optional_string() const noexcept { + return _captures.template select<0>().to_optional_string(); } + + + + constexpr CTRE_FORCE_INLINE size_t size() const noexcept { return _captures.template select<0>().size(); }