object-introspection/types/uniq_ptr_type.toml
Jake Hillion b117150f83 tbv2: add dynamic context passed through all functions (#410)
Summary:
tbv2: add dynamic context passed through all functions

Previously for we had some shared state between all requests, noticeably the
pointers set. This change adds a by reference value to all requests which can
hold additional mutable state. The pointers set is moved into this mutable
state for OIL, which means each concurrent request will have its own pointer
set. Doing things this way allows more features to be added in the future
without such a big code modification.

Closes https://github.com/facebookexperimental/object-introspection/issues/404

Pull Request resolved: https://github.com/facebookexperimental/object-introspection/pull/410

Test Plan: - CI

Differential Revision: D51394035

Pulled By: JakeHillion

fbshipit-source-id: 55d2ba9b5e056148a29dc821020cfc3d94e5175a
2023-11-16 08:03:32 -08:00

81 lines
1.9 KiB
TOML

[info]
type_name = "std::unique_ptr"
ctype = "UNIQ_PTR_TYPE"
header = "memory"
stub_template_params = [1]
# Old:
typeName = "std::unique_ptr"
ns = ["namespace std"]
numTemplateParams = 1
replaceTemplateParamIndex = []
[codegen]
decl = """
template<typename T, class Deleter>
void getSizeType(const %1%<T,Deleter> &container, size_t& returnArg);
"""
func = """
template<typename T, class Deleter>
void getSizeType(const %1%<T,Deleter> &u_ptr, size_t& returnArg)
{
SAVE_SIZE(sizeof(%1%<T,Deleter>));
if constexpr (!std::is_void<T>::value) {
SAVE_DATA((uintptr_t)(u_ptr.get()));
if (u_ptr && ctx.pointers.add((uintptr_t)(u_ptr.get()))) {
SAVE_DATA(1);
getSizeType(*(u_ptr.get()), returnArg);
} else {
SAVE_DATA(0);
}
}
}
"""
traversal_func = """
auto tail = returnArg.write((uintptr_t)container.get());
if constexpr (std::is_void<T0>::value) {
return tail.template delegate<0>(std::identity());
} else {
bool do_visit = container && ctx.pointers.add((uintptr_t)container.get());
if (!do_visit)
return tail.template delegate<0>(std::identity());
return tail.template delegate<1>([&ctx, &container](auto ret) {
return OIInternal::getSizeType<Ctx>(ctx, *container, ret);
});
}
"""
[[codegen.processor]]
type = "types::st::VarInt<DB>"
func = """
el.pointer = std::get<ParsedData::VarInt>(d.val).value;
"""
[[codegen.processor]]
type = """
types::st::Sum<DB,
types::st::Unit<DB>,
typename TypeHandler<Ctx, T0>::type>
"""
func = """
auto sum = std::get<ParsedData::Sum>(d.val);
el.container_stats.emplace(result::Element::ContainerStats {
.capacity = 1,
.length = sum.index, // 0 for empty containers/void, 1 otherwise
});
// Must be in a `if constexpr` or the compiler will complain about make_field<Ctx, void>
if constexpr (!std::is_void<T0>::value) {
if (sum.index == 1) {
static constexpr auto element = make_field<Ctx, T0>("ptr_val");
stack_ins(element);
}
}
"""