From 0c25efd3531380739574cedec47e95fdbfeb479d Mon Sep 17 00:00:00 2001 From: ado Date: Fri, 11 Dec 2020 21:17:07 +0100 Subject: [PATCH] add support for std::optional when extracting --- include/ss/extract.hpp | 26 ++++++++++++++++++++------ include/ss/type_traits.hpp | 14 ++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/include/ss/extract.hpp b/include/ss/extract.hpp index 3230777..4c84128 100644 --- a/include/ss/extract.hpp +++ b/include/ss/extract.hpp @@ -1,5 +1,6 @@ #pragma once +#include "type_traits.hpp" #include #include #include @@ -313,18 +314,15 @@ struct unsupported_type { } /* namespace */ template -std::enable_if_t && !std::is_floating_point_v, bool> +std::enable_if_t && !std::is_floating_point_v && + !is_instance_of::value, + bool> extract(const char*, const char*, T&) { - static_assert(error::unsupported_type::value, "Conversion for given type is not defined, an " "\'extract\' function needs to be defined!"); } -//////////////// -// extract specialization -//////////////// - template std::enable_if_t || std::is_floating_point_v, bool> extract(const char* begin, const char* end, T& value) { @@ -336,6 +334,22 @@ extract(const char* begin, const char* end, T& value) { return true; } +template +std::enable_if_t::value, bool> extract( + const char* begin, const char* end, T& value) { + typename T::value_type raw_value; + if (extract(begin, end, raw_value)) { + value = raw_value; + } else { + value = std::nullopt; + } + return true; +} + +//////////////// +// extract specialization +//////////////// + template <> inline bool extract(const char* begin, const char* end, bool& value) { if (end == begin + 1) { diff --git a/include/ss/type_traits.hpp b/include/ss/type_traits.hpp index dbe979e..c637dfb 100644 --- a/include/ss/type_traits.hpp +++ b/include/ss/type_traits.hpp @@ -308,6 +308,20 @@ struct none_of> { static constexpr bool value = count::size == 0; }; +//////////////// +// is instance of +//////////////// + +template class> +struct is_instance_of { + constexpr static bool value = false; +}; + +template class U> +struct is_instance_of, U> { + constexpr static bool value = true; +}; + //////////////// // tuple to struct ////////////////