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> template <typename F>
Unit<DataBuffer> delegate(F const& cb) { Unit<DataBuffer> consume(F const& cb) {
return cb(*this); return cb(*this);
} }
@ -131,6 +131,11 @@ class VarInt {
return Unit<DataBuffer>(_buf); return Unit<DataBuffer>(_buf);
} }
template <typename F>
Unit<DataBuffer> consume(F const& cb) {
return cb(*this);
}
#ifdef DEFINE_DESCRIBE #ifdef DEFINE_DESCRIBE
static constexpr types::dy::VarInt describe{}; static constexpr types::dy::VarInt describe{};
#endif #endif
@ -165,6 +170,11 @@ class Pair {
return second.template cast<T2>(); return second.template cast<T2>();
} }
template <typename F>
Unit<DataBuffer> consume(F const& cb) {
return cb(*this);
}
#ifdef DEFINE_DESCRIBE #ifdef DEFINE_DESCRIBE
static constexpr types::dy::Pair describe{T1::describe, T2::describe}; static constexpr types::dy::Pair describe{T1::describe, T2::describe};
#endif #endif
@ -217,6 +227,11 @@ class Sum {
return cb(tail); return cb(tail);
} }
template <typename F>
Unit<DataBuffer> consume(F const& cb) {
return cb(*this);
}
#ifdef DEFINE_DESCRIBE #ifdef DEFINE_DESCRIBE
private: private:
static constexpr std::array<types::dy::Dynamic, sizeof...(Types)> members{ static constexpr std::array<types::dy::Dynamic, sizeof...(Types)> members{
@ -274,6 +289,11 @@ class List
: Pair<DataBuffer, VarInt<DataBuffer>, ListContents<DataBuffer, T>>(db) { : Pair<DataBuffer, VarInt<DataBuffer>, ListContents<DataBuffer, T>>(db) {
} }
template <typename F>
Unit<DataBuffer> consume(F const& cb) {
return cb(*this);
}
#ifdef DEFINE_DESCRIBE #ifdef DEFINE_DESCRIBE
public: public:
static constexpr types::dy::List describe{T::describe}; 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 // Generate the function body that walks the type. Uses the monadic
// `delegate()` form to handle each field except for the last. The last field // `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 // instead uses `consume()` as we must not accidentally handle the first half
// the entire type instead of delegating the next part. // of a pair as the last field.
void CodeGen::genClassTraversalFunction(const Class& c, std::string& code) { void CodeGen::genClassTraversalFunction(const Class& c, std::string& code) {
std::string funcName = "getSizeType"; std::string funcName = "getSizeType";
@ -667,30 +667,28 @@ void CodeGen::genClassTraversalFunction(const Class& c, std::string& code) {
} }
if (code.size() == emptySize) { if (code.size() == emptySize) {
code += " auto ret = returnArg"; code += " return returnArg";
} }
if (thriftIssetMember != nullptr && thriftIssetMember != &member) { if (thriftIssetMember != nullptr && thriftIssetMember != &member) {
code += "\n .write(getThriftIsset(t, " + std::to_string(i) + "))"; code += "\n .write(getThriftIsset(t, " + std::to_string(i) + "))";
} }
if (i != lastNonPaddingElement) { code += "\n .";
code += if (i == lastNonPaddingElement) {
"\n .delegate([&t](auto ret) { return " code += "consume";
"OIInternal::getSizeType<DB>(t."; } else {
code += "delegate";
}
code += "([&t](auto ret) { return OIInternal::getSizeType<DB>(t.";
code += member.name; code += member.name;
code += ", ret); })"; code += ", ret); })";
} else {
code += ";";
code += "\n return OIInternal::getSizeType<DB>(t." + member.name +
", ret);\n";
}
} }
if (code.size() == emptySize) { if (code.size() == emptySize) {
code += " return returnArg;"; code += " return returnArg;";
} }
code += " }\n"; code += ";\n }\n";
} }
// Generate the static type for the class's representation in the data buffer. // Generate the static type for the class's representation in the data buffer.