Use Parse instead TryParse in Serialization for last type in std::variant

This commit is contained in:
sha512sum 2024-10-09 15:56:45 +00:00
parent ba03f3fe6f
commit 3e18243f79

View file

@ -138,19 +138,32 @@ struct Serialization<std::variant<Ts...>> : SerializationBase<> {
static constexpr auto StartCheck(xmlpp::Element* element) { static constexpr auto StartCheck(xmlpp::Element* element) {
return true; return true;
} }
[[nodiscard]] static constexpr auto TryParse(xmlpp::Element* element) -> std::optional<std::variant<Ts...>> {
return utempl::FirstOf(utempl::Tuple{[&] -> std::optional<Ts> { private:
if(Serialization<Ts>::StartCheck(element)) { template <typename T>
return Serialization<Ts>::TryParse(element); static constexpr auto FunctionForType(xmlpp::Element* element) {
return [=] -> std::optional<T> {
if(Serialization<T>::StartCheck(element)) {
return Serialization<T>::TryParse(element);
} else { } else {
SPDLOG_DEBUG("StartCheck failed for type {}", nameof::nameof_type<Ts>()); SPDLOG_DEBUG("StartCheck failed for type {}", nameof::nameof_type<T>());
return std::nullopt; return std::nullopt;
} }
}...}, };
std::optional<std::variant<Ts...>>{});
} }
[[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 { static constexpr auto Serialize(xmlpp::Element* element, const std::variant<Ts...>& object) -> void {
std::visit( std::visit(