object-introspection/types/std_variant.toml
2023-06-30 12:54:02 +01:00

69 lines
2.0 KiB
TOML

[info]
type_name = "std::variant"
ctype = "STD_VARIANT_TYPE"
header = "variant"
# Old:
typeName = "std::variant"
ns = ["namespace std"]
[codegen]
decl = """
template<class... Types>
void getSizeType(const %1%<Types...> &container, size_t& returnArg);
"""
func = """
template<class... Types>
void getSizeType(const %1%<Types...> &container, size_t& returnArg)
{
SAVE_SIZE(sizeof(%1%<Types...>));
SAVE_DATA(container.index());
// 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);
}
"""
handler = """
template <typename DB, typename... Types>
struct TypeHandler<DB, %1%<Types...>> {
using type = types::st::Sum<DB, typename TypeHandler<DB, Types>::type..., types::st::Unit<DB>>;
static types::st::Unit<DB> getSizeType(
const %1%<Types...>& container,
typename TypeHandler<DB, %1%<Types...>>::type returnArg) {
return getSizeTypeRecursive(container, returnArg);
}
private:
template <size_t I = 0>
static types::st::Unit<DB> getSizeTypeRecursive(
const %1%<Types...>& container,
typename TypeHandler<DB, %1%<Types...>>::type returnArg) {
if constexpr (I < sizeof...(Types)) {
if (I == container.index()) {
return returnArg.template delegate<I>([&container](auto ret) {
return OIInternal::getSizeType<DB>(std::get<I>(container), ret);
});
} else {
return getSizeTypeRecursive<I+1>(container, returnArg);
}
} else {
return returnArg.template delegate<sizeof...(Types)>(std::identity());
}
}
};
"""