static types: add consume function similar to delegate

This commit is contained in:
Jake Hillion 2023-09-15 06:15:01 -07:00 committed by Jake Hillion
parent d71307cb43
commit 8dd1182323
2 changed files with 32 additions and 14 deletions

View File

@ -80,7 +80,7 @@ class Unit {
}
template <typename F>
Unit<DataBuffer> delegate(F const& cb) {
Unit<DataBuffer> consume(F const& cb) {
return cb(*this);
}
@ -131,6 +131,11 @@ class VarInt {
return Unit<DataBuffer>(_buf);
}
template <typename F>
Unit<DataBuffer> consume(F const& cb) {
return cb(*this);
}
#ifdef DEFINE_DESCRIBE
static constexpr types::dy::VarInt describe{};
#endif
@ -165,6 +170,11 @@ class Pair {
return second.template cast<T2>();
}
template <typename F>
Unit<DataBuffer> consume(F const& cb) {
return cb(*this);
}
#ifdef DEFINE_DESCRIBE
static constexpr types::dy::Pair describe{T1::describe, T2::describe};
#endif
@ -217,6 +227,11 @@ class Sum {
return cb(tail);
}
template <typename F>
Unit<DataBuffer> consume(F const& cb) {
return cb(*this);
}
#ifdef DEFINE_DESCRIBE
private:
static constexpr std::array<types::dy::Dynamic, sizeof...(Types)> members{
@ -274,6 +289,11 @@ class List
: Pair<DataBuffer, VarInt<DataBuffer>, ListContents<DataBuffer, T>>(db) {
}
template <typename F>
Unit<DataBuffer> consume(F const& cb) {
return cb(*this);
}
#ifdef DEFINE_DESCRIBE
public:
static constexpr types::dy::List describe{T::describe};

View File

@ -639,8 +639,8 @@ size_t getLastNonPaddingMemberIndex(const std::vector<Member>& members) {
// Generate the function body that walks the type. Uses the monadic
// `delegate()` form to handle each field except for the last. The last field
// is handled explicitly by passing it to `getSizeType`, as we must consume
// the entire type instead of delegating the next part.
// instead uses `consume()` as we must not accidentally handle the first half
// of a pair as the last field.
void CodeGen::genClassTraversalFunction(const Class& c, std::string& code) {
std::string funcName = "getSizeType";
@ -667,30 +667,28 @@ void CodeGen::genClassTraversalFunction(const Class& c, std::string& code) {
}
if (code.size() == emptySize) {
code += " auto ret = returnArg";
code += " return returnArg";
}
if (thriftIssetMember != nullptr && thriftIssetMember != &member) {
code += "\n .write(getThriftIsset(t, " + std::to_string(i) + "))";
}
if (i != lastNonPaddingElement) {
code +=
"\n .delegate([&t](auto ret) { return "
"OIInternal::getSizeType<DB>(t.";
code += member.name;
code += ", ret); })";
code += "\n .";
if (i == lastNonPaddingElement) {
code += "consume";
} else {
code += ";";
code += "\n return OIInternal::getSizeType<DB>(t." + member.name +
", ret);\n";
code += "delegate";
}
code += "([&t](auto ret) { return OIInternal::getSizeType<DB>(t.";
code += member.name;
code += ", ret); })";
}
if (code.size() == emptySize) {
code += " return returnArg;";
}
code += " }\n";
code += ";\n }\n";
}
// Generate the static type for the class's representation in the data buffer.