object-introspection/types/seq_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

107 lines
2.8 KiB
TOML

[info]
type_name = "std::vector"
stub_template_params = [1]
header = "vector"
# Old:
ctype = "SEQ_TYPE"
typeName = "std::vector<"
ns = ["namespace std"]
numTemplateParams = 1
allocatorIndex = 1
[codegen]
decl = """
template<typename T, typename Allocator>
void getSizeType(const %1%<T, Allocator> &container, size_t& returnArg);
"""
func = """
template<typename T, typename Allocator>
void getSizeType(const %1%<T, Allocator> &container, size_t& returnArg)
{
SAVE_SIZE(sizeof(%1%<T>));
SAVE_DATA((uintptr_t)&container);
SAVE_DATA((uintptr_t)container.capacity());
SAVE_DATA((uintptr_t)container.size());
SAVE_SIZE((container.capacity() - container.size()) * sizeof(T));
// The double ampersand is needed otherwise this loop doesn't work with vector<bool>
for (auto&& it: container) {
getSizeType(it, returnArg);
}
}
"""
handler = """
template <typename DB, typename T0, typename T1>
struct TypeHandler<DB, %1%<T0, T1>> {
using type = types::st::Pair<
DB, types::st::VarInt<DB>,
types::st::Pair<
DB, types::st::VarInt<DB>,
types::st::List<DB, typename TypeHandler<DB, T0>::type>>>;
static types::st::Unit<DB> getSizeType(
const %1%<T0, T1> & container,
typename TypeHandler<DB, %1%<T0, T1>>::type returnArg) {
auto tail = returnArg.write((uintptr_t)&container)
.write(container.capacity())
.write(container.size());
// The double ampersand is needed otherwise this loop doesn't work with
// vector<bool>
for (auto&& it : container) {
tail = tail.delegate([&it](auto ret) {
return OIInternal::getSizeType<DB>(it, ret);
});
}
return tail.finish();
}
};
"""
traversal_func = """
auto tail = returnArg.write((uintptr_t)&container)
.write(container.capacity())
.write(container.size());
// The double ampersand is needed otherwise this loop doesn't work with
// vector<bool>
for (auto&& it : container) {
tail = tail.delegate([&it](auto ret) {
return OIInternal::getSizeType<DB>(it, ret);
});
}
return tail.finish();
"""
[[codegen.processor]]
type = "types::st::VarInt<DB>"
func = """
el.pointer = std::get<ParsedData::VarInt>(d.val).value;
"""
[[codegen.processor]]
type = "types::st::VarInt<DB>"
func = """
el.container_stats.emplace(result::Element::ContainerStats{ .capacity = std::get<ParsedData::VarInt>(d.val).value });
"""
[[codegen.processor]]
type = "types::st::List<DB, typename TypeHandler<DB, T0>::type>"
func = """
static constexpr auto childField = make_field<DB, T0>("[]");
auto list = std::get<ParsedData::List>(d.val);
el.container_stats->length = list.length;
el.exclusive_size += (el.container_stats->capacity - el.container_stats->length) * sizeof(T0);
for (size_t i = 0; i < list.length; i++)
stack_ins(childField);
"""