mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:47:46 +00:00
LibIPC+IPCCompiler: Templatize encoding/decoding of Optional<T>
This was the last one! IPCCompiler no longer has any type-specific encoding/decoding logic! :^)
This commit is contained in:
parent
6800f6eff5
commit
413ab652c8
3 changed files with 29 additions and 21 deletions
|
@ -310,20 +310,8 @@ int main(int argc, char** argv)
|
|||
if (parameter.type == "bool")
|
||||
initial_value = "false";
|
||||
out() << " " << parameter.type << " " << parameter.name << " = " << initial_value << ";";
|
||||
|
||||
if (parameter.type.starts_with("Optional<")) {
|
||||
out() << " bool has_value = false;";
|
||||
out() << " stream >> has_value;";
|
||||
out() << " if (has_value) {";
|
||||
out() << " " << parameter.type.substring_view(9, parameter.type.length() - 10) << " value;";
|
||||
out() << " if (!decoder.decode(value))";
|
||||
out() << " return nullptr;";
|
||||
out() << " " << parameter.name << " = value;";
|
||||
out() << " }";
|
||||
} else {
|
||||
out() << " if (!decoder.decode(" << parameter.name << "))";
|
||||
out() << " return nullptr;";
|
||||
}
|
||||
out() << " if (!decoder.decode(" << parameter.name << "))";
|
||||
out() << " return nullptr;";
|
||||
}
|
||||
|
||||
StringBuilder builder;
|
||||
|
@ -343,13 +331,7 @@ int main(int argc, char** argv)
|
|||
out() << " stream << endpoint_magic();";
|
||||
out() << " stream << (int)MessageID::" << name << ";";
|
||||
for (auto& parameter : parameters) {
|
||||
if (parameter.type.starts_with("Optional<")) {
|
||||
out() << " stream << m_" << parameter.name << ".has_value();";
|
||||
out() << " if (m_" << parameter.name << ".has_value())";
|
||||
out() << " stream << m_" << parameter.name << ".value();";
|
||||
} else {
|
||||
out() << " stream << m_" << parameter.name << ";";
|
||||
}
|
||||
out() << " stream << m_" << parameter.name << ";";
|
||||
}
|
||||
out() << " return buffer;";
|
||||
out() << " }";
|
||||
|
|
|
@ -83,6 +83,23 @@ public:
|
|||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool decode(Optional<T>& optional)
|
||||
{
|
||||
bool has_value;
|
||||
if (!decode(has_value))
|
||||
return false;
|
||||
if (!has_value) {
|
||||
optional = {};
|
||||
return true;
|
||||
}
|
||||
T value;
|
||||
if (!decode(value))
|
||||
return false;
|
||||
optional = move(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
BufferStream& m_stream;
|
||||
};
|
||||
|
|
|
@ -76,6 +76,15 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Encoder& operator<<(const Optional<T>& optional)
|
||||
{
|
||||
*this << optional.has_value();
|
||||
if (optional.has_value())
|
||||
*this << optional.value();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void encode(const T& value)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue