add variant support

This commit is contained in:
ado 2020-12-20 03:16:17 +01:00
parent 879c726964
commit bf92abdb2c

View File

@ -7,6 +7,7 @@
#include <optional>
#include <stdexcept>
#include <string>
#include <variant>
namespace ss {
@ -315,7 +316,8 @@ struct unsupported_type {
template <typename T>
std::enable_if_t<!std::is_integral_v<T> && !std::is_floating_point_v<T> &&
!is_instance_of<T, std::optional>::value,
!is_instance_of<T, std::optional>::value &&
!is_instance_of<T, std::variant>::value,
bool>
extract(const char*, const char*, T&) {
static_assert(error::unsupported_type<T>::value,
@ -346,6 +348,25 @@ std::enable_if_t<is_instance_of<T, std::optional>::value, bool> extract(
return true;
}
template <typename T, size_t I>
bool extract_variant(const char* begin, const char* end, T& value) {
using IthType = std::variant_alternative_t<I, std::decay_t<T>>;
IthType ithValue;
if (extract<IthType>(begin, end, ithValue)) {
value = ithValue;
return true;
} else if constexpr (I + 1 < std::variant_size_v<T>) {
return extract_variant<T, I + 1>(begin, end, value);
}
return false;
}
template <typename T>
std::enable_if_t<is_instance_of<T, std::variant>::value, bool> extract(
const char* begin, const char* end, T& value) {
return extract_variant<T, 0>(begin, end, value);
}
////////////////
// extract specialization
////////////////