mirror of
https://github.com/JakeHillion/object-introspection.git
synced 2024-11-12 21:56:54 +00:00
std::variant: change to use std::visit
This commit is contained in:
parent
512163f98e
commit
2e74fa357a
@ -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);
|
||||
}
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user