object-introspection/types/optional_type.toml
Jake Hillion 592e182e0f tbv2: replace DB template param with Ctx (#409)
Summary:
tbv2: replace DB template param with Ctx

TreeBuilder v2 adds a DB template parameter to every function. This is used as
part of the static type to decide what type of DataBuffer is being used:
currently `BackInserterDataBuffer<std::vector<uint8_t>>` for OIL and it would
be `DataSegmentDataBuffer` for OID.

This change replaces the `DB` template parameter with a more general `Ctx`. Due
to issues with dependent naming it also adds a `using DB` to each `TypeHandler`
which has the same function as before. This allows us to add more "static
context" (typedefs and constants) to functions without changing this signature
again, because changing the signature of everything is a massive pain.

Currently this change achieves nothing because Ctx contains only DB in a static
wrapper. In the next change I'm going to pass a reference of type Ctx around to
add a "dynamic context" to invocations which will contain the pointer array. In
future we'll then be able to add either static or dynamic context without any
signature adjustments.


Test Plan:
- CI

---
Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebookexperimental/object-introspection/pull/409).
* https://github.com/facebookexperimental/object-introspection/issues/410
* __->__ https://github.com/facebookexperimental/object-introspection/issues/409

Reviewed By: ajor

Differential Revision: D51352092

Pulled By: JakeHillion
2023-11-15 11:52:17 -08:00

65 lines
1.4 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);
}
}
"""
traversal_func = """
if (container.has_value()) {
return returnArg.template delegate<1>([&container](auto ret) {
return OIInternal::getSizeType<Ctx>(*container, ret);
});
} else {
return returnArg.template delegate<0>(std::identity());
}
"""
[[codegen.processor]]
type = "types::st::Sum<DB, types::st::Unit<DB>, typename TypeHandler<Ctx, T0>::type>"
func = """
static constexpr std::array<std::string_view, 1> names{"TODO"};
static constexpr auto elementField = inst::Field{
sizeof(T0),
"el",
names,
TypeHandler<Ctx, T0>::fields,
TypeHandler<Ctx, 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);
}
"""