mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:57:35 +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")
|
if (parameter.type == "bool")
|
||||||
initial_value = "false";
|
initial_value = "false";
|
||||||
out() << " " << parameter.type << " " << parameter.name << " = " << initial_value << ";";
|
out() << " " << parameter.type << " " << parameter.name << " = " << initial_value << ";";
|
||||||
|
out() << " if (!decoder.decode(" << parameter.name << "))";
|
||||||
if (parameter.type.starts_with("Optional<")) {
|
out() << " return nullptr;";
|
||||||
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;";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
|
@ -343,13 +331,7 @@ int main(int argc, char** argv)
|
||||||
out() << " stream << endpoint_magic();";
|
out() << " stream << endpoint_magic();";
|
||||||
out() << " stream << (int)MessageID::" << name << ";";
|
out() << " stream << (int)MessageID::" << name << ";";
|
||||||
for (auto& parameter : parameters) {
|
for (auto& parameter : parameters) {
|
||||||
if (parameter.type.starts_with("Optional<")) {
|
out() << " stream << m_" << parameter.name << ";";
|
||||||
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() << " return buffer;";
|
out() << " return buffer;";
|
||||||
out() << " }";
|
out() << " }";
|
||||||
|
|
|
@ -83,6 +83,23 @@ public:
|
||||||
return true;
|
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:
|
private:
|
||||||
BufferStream& m_stream;
|
BufferStream& m_stream;
|
||||||
};
|
};
|
||||||
|
|
|
@ -76,6 +76,15 @@ public:
|
||||||
return *this;
|
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>
|
template<typename T>
|
||||||
void encode(const T& value)
|
void encode(const T& value)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue