Add facade types to avoid using raw Broker types

By avoiding to use `broker::data` directly, we gain a degree of freedom
that allows us to swap out `broker::data` for something else (e.g.,
`broker::variant`) in the future. Furthermore, it also helps us to keep
Broker types "local" to the Broker manager and gives us a nicer
interface.

Also replaces uses of `broker::expected` with `std::optional`. While an
`expected `can carry additional information as to why a value is not
present, nothing in Zeek ever cared about that. Hence, using
`std::optional` removes an unnecessary dependency on a Broker detail
while also being more efficient (no extra heap allocation when no value
is present).
This commit is contained in:
Dominik Charousset 2023-11-11 09:21:00 +01:00
parent bc0f85caa8
commit 647fdf7737
30 changed files with 1091 additions and 552 deletions

View file

@ -2,7 +2,7 @@
#include "zeek/script_opt/CPP/Func.h"
#include <broker/error.hh>
#include <string_view>
#include "zeek/Desc.h"
#include "zeek/broker/Data.h"
@ -46,27 +46,29 @@ CPPLambdaFunc::CPPLambdaFunc(string _name, FuncTypePtr ft, CPPStmtPtr _l_body)
l_body = std::move(_l_body);
}
broker::expected<broker::data> CPPLambdaFunc::SerializeCaptures() const {
std::optional<BrokerData> CPPLambdaFunc::SerializeCaptures() const {
using namespace std::literals;
auto name = "CopyFrame"sv;
auto vals = l_body->SerializeLambdaCaptures();
broker::vector rval;
rval.emplace_back(string("CopyFrame"));
broker::vector body;
BrokerListBuilder body;
body.Reserve(vals.size());
for ( const auto& val : vals ) {
auto expected = Broker::detail::val_to_data(val.get());
if ( ! expected )
return broker::ec::invalid_data;
BrokerData tmp;
if ( ! tmp.Convert(val) )
return std::nullopt;
TypeTag tag = val->GetType()->Tag();
broker::vector val_tuple{std::move(*expected), static_cast<broker::integer>(tag)};
body.emplace_back(std::move(val_tuple));
body.AddList(std::move(tmp), static_cast<int64_t>(tag));
}
rval.emplace_back(std::move(body));
return {std::move(rval)};
BrokerListBuilder builder;
builder.Reserve(2);
builder.AddString(name.data(), name.size());
builder.Add(std::move(body));
return std::move(builder).Build();
}
void CPPLambdaFunc::SetCaptures(Frame* f) { l_body->SetLambdaCaptures(f); }