mirror of
https://github.com/red0124/ssp.git
synced 2025-01-23 04:55:20 +01:00
add support for std::optional when extracting
This commit is contained in:
parent
0d84fcf7e8
commit
0c25efd353
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "type_traits.hpp"
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <limits>
|
||||
@ -313,18 +314,15 @@ struct unsupported_type {
|
||||
} /* namespace */
|
||||
|
||||
template <typename T>
|
||||
std::enable_if_t<!std::is_integral_v<T> && !std::is_floating_point_v<T>, bool>
|
||||
std::enable_if_t<!std::is_integral_v<T> && !std::is_floating_point_v<T> &&
|
||||
!is_instance_of<T, std::optional>::value,
|
||||
bool>
|
||||
extract(const char*, const char*, T&) {
|
||||
|
||||
static_assert(error::unsupported_type<T>::value,
|
||||
"Conversion for given type is not defined, an "
|
||||
"\'extract\' function needs to be defined!");
|
||||
}
|
||||
|
||||
////////////////
|
||||
// extract specialization
|
||||
////////////////
|
||||
|
||||
template <typename T>
|
||||
std::enable_if_t<std::is_integral_v<T> || std::is_floating_point_v<T>, 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 <typename T>
|
||||
std::enable_if_t<is_instance_of<T, std::optional>::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) {
|
||||
|
@ -308,6 +308,20 @@ struct none_of<Trait, std::tuple<Ts...>> {
|
||||
static constexpr bool value = count<Trait, Ts...>::size == 0;
|
||||
};
|
||||
|
||||
////////////////
|
||||
// is instance of
|
||||
////////////////
|
||||
|
||||
template <typename, template <class> class>
|
||||
struct is_instance_of {
|
||||
constexpr static bool value = false;
|
||||
};
|
||||
|
||||
template <typename T, template <class> class U>
|
||||
struct is_instance_of<U<T>, U> {
|
||||
constexpr static bool value = true;
|
||||
};
|
||||
|
||||
////////////////
|
||||
// tuple to struct
|
||||
////////////////
|
||||
|
Loading…
Reference in New Issue
Block a user