std::variant: change to use std::visit

This commit is contained in:
Jake Hillion 2023-03-22 15:16:45 -07:00 committed by Jake Hillion
parent 512163f98e
commit 2e74fa357a

View File

@ -11,32 +11,24 @@ void getSizeType(const %1%<Types...> &container, size_t& returnArg);
"""
func = """
template <size_t I = 0, class... Types>
void getSizeVariantContents(const %1%<Types...> &container, size_t& returnArg)
{
if constexpr (I < sizeof...(Types))
{
if (I == container.index())
{
// Contents are stored inline - don't double count
SAVE_SIZE(-sizeof(std::get<I>(container)));
getSizeType(std::get<I>(container), returnArg);
}
else
{
getSizeVariantContents<I+1>(container, returnArg);
}
}
// else variant is valueless - save no data
}
template<class... Types>
void getSizeType(const %1%<Types...> &container, size_t& returnArg)
{
SAVE_SIZE(sizeof(%1%<Types...>));
SAVE_DATA(container.index());
getSizeVariantContents(container, returnArg);
// This check should be `container.valueless_by_exception()` but it doesn't
// work with the variable sized integers used in `std::variant`. For fewer
// than 256 options it uses a `uint8_t` index but checks against -1 of
// `uintptr_t`. Manually check for any out of bounds indices as a workaround.
if (container.index() >= sizeof...(Types)) {
return;
}
std::visit([&returnArg](auto &&arg) {
// Account for inline contents
SAVE_SIZE(-sizeof(arg));
getSizeType(arg, returnArg);
}, container);
}
"""