object-introspection/types/optional_type.toml
Jake Hillion d71307cb43 oil: change std::stack reference to a std::function (#345)
Summary:

Previously on large types OIL would have problems with corrupting the `std::stack<exporter::inst::Inst>` that is passed to the processors. This change hides the implementation of the stack from the processors by wrapping the call to emplace in a `std::function` written by the non-generated code, which solves the test case I've seen for this crashing. It also allows us to easily change the stack implementation in future - I plan to change it to a `std::stack<T, std::vector<T>>` in a follow up.

Reviewed By: tyroguru

Differential Revision: D49273116
2023-09-14 16:57:45 +01:00

87 lines
1.9 KiB
TOML

[info]
type_name = "std::optional"
ctype = "OPTIONAL_TYPE"
header = "optional"
# Old:
typeName = "std::optional<"
ns = ["namespace std"]
numTemplateParams = 1
[codegen]
decl = """
template<typename T>
void getSizeType(const %1%<T> &container, size_t& returnArg);
"""
func = """
template <typename T>
void getSizeType(const %1%<T>& container, size_t& returnArg) {
if (container) {
SAVE_SIZE(sizeof(%1%<T>) - sizeof(T));
SAVE_DATA(true);
getSizeType(*container, returnArg);
} else {
SAVE_SIZE(sizeof(%1%<T>));
SAVE_DATA(false);
}
}
"""
handler = """
template <typename DB, typename T0>
struct TypeHandler<DB, %1%<T0>> {
using type = types::st::Sum<DB,
types::st::Unit<DB>,
typename TypeHandler<DB, T0>::type
>;
static types::st::Unit<DB> getSizeType(
const %1%<T0>& container,
typename TypeHandler<DB, %1%<T0>>::type returnArg) {
if (container) {
return returnArg.template delegate<1>([&container](auto ret) {
return OIInternal::getSizeType<DB>(*container, ret);
});
} else {
return returnArg.template delegate<0>(std::identity());
}
}
};
"""
traversal_func = """
if (container.has_value()) {
return returnArg.template delegate<1>([&container](auto ret) {
return OIInternal::getSizeType<DB>(*container, ret);
});
} else {
return returnArg.template delegate<0>(std::identity());
}
"""
[[codegen.processor]]
type = "types::st::Sum<DB, types::st::Unit<DB>, typename TypeHandler<DB, T0>::type>"
func = """
static constexpr std::array<std::string_view, 1> names{"TODO"};
static constexpr auto elementField = inst::Field{
sizeof(T0),
"el",
names,
TypeHandler<DB, T0>::fields,
TypeHandler<DB, T0>::processors,
};
auto sum = std::get<ParsedData::Sum>(d.val);
el.container_stats = result::Element::ContainerStats {
.capacity = 1,
.length = sum.index,
};
if (sum.index == 1) {
el.exclusive_size -= sizeof(T0);
stack_ins(elementField);
}
"""