Use Parse instead TryParse in Serialization for last type in std::variant
This commit is contained in:
parent
ba03f3fe6f
commit
3e18243f79
1 changed files with 25 additions and 12 deletions
|
@ -138,19 +138,32 @@ struct Serialization<std::variant<Ts...>> : SerializationBase<> {
|
|||
static constexpr auto StartCheck(xmlpp::Element* element) {
|
||||
return true;
|
||||
}
|
||||
[[nodiscard]] static constexpr auto TryParse(xmlpp::Element* element) -> std::optional<std::variant<Ts...>> {
|
||||
return utempl::FirstOf(utempl::Tuple{[&] -> std::optional<Ts> {
|
||||
if(Serialization<Ts>::StartCheck(element)) {
|
||||
return Serialization<Ts>::TryParse(element);
|
||||
} else {
|
||||
SPDLOG_DEBUG("StartCheck failed for type {}", nameof::nameof_type<Ts>());
|
||||
return std::nullopt;
|
||||
}
|
||||
}...},
|
||||
std::optional<std::variant<Ts...>>{});
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
static constexpr auto FunctionForType(xmlpp::Element* element) {
|
||||
return [=] -> std::optional<T> {
|
||||
if(Serialization<T>::StartCheck(element)) {
|
||||
return Serialization<T>::TryParse(element);
|
||||
} else {
|
||||
SPDLOG_DEBUG("StartCheck failed for type {}", nameof::nameof_type<T>());
|
||||
return std::nullopt;
|
||||
}
|
||||
};
|
||||
}
|
||||
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) -> std::variant<Ts...> {
|
||||
return Serialization::TryParse(element).value();
|
||||
|
||||
public:
|
||||
[[nodiscard]] static constexpr auto TryParse(xmlpp::Element* element) -> std::optional<std::variant<Ts...>> {
|
||||
return utempl::FirstOf(utempl::Tuple{FunctionForType<Ts>(element)...}, std::optional<std::variant<Ts...>>{});
|
||||
}
|
||||
[[nodiscard]] static constexpr auto Parse(xmlpp::Element* element) {
|
||||
return [&]<typename... TTs>(utempl::TypeList<TTs...>) {
|
||||
// operator* is safe because in or_else Parse returns Ts...[sizeof...(Ts) - 1] (not optional)
|
||||
return *utempl::FirstOf(utempl::Tuple{FunctionForType<TTs>(element)...}, std::optional<std::variant<Ts...>>{})
|
||||
.or_else([&] -> std::optional<std::variant<Ts...>> {
|
||||
return Serialization<decltype(utempl::Get<sizeof...(Ts) - 1>(utempl::kTypeList<Ts...>))>::Parse(element);
|
||||
});
|
||||
}(utempl::TakeFrom<sizeof...(Ts) - 1>(utempl::kTypeList<Ts...>));
|
||||
}
|
||||
static constexpr auto Serialize(xmlpp::Element* element, const std::variant<Ts...>& object) -> void {
|
||||
std::visit(
|
||||
|
|
Loading…
Reference in a new issue