summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--DevTools/IPCCompiler/main.cpp24
-rw-r--r--Libraries/LibIPC/Decoder.h17
-rw-r--r--Libraries/LibIPC/Encoder.h9
3 files changed, 29 insertions, 21 deletions
diff --git a/DevTools/IPCCompiler/main.cpp b/DevTools/IPCCompiler/main.cpp
index c73ad776f9..8351e2da80 100644
--- a/DevTools/IPCCompiler/main.cpp
+++ b/DevTools/IPCCompiler/main.cpp
@@ -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() << " }";
diff --git a/Libraries/LibIPC/Decoder.h b/Libraries/LibIPC/Decoder.h
index d79abb9c06..340a86701f 100644
--- a/Libraries/LibIPC/Decoder.h
+++ b/Libraries/LibIPC/Decoder.h
@@ -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;
};
diff --git a/Libraries/LibIPC/Encoder.h b/Libraries/LibIPC/Encoder.h
index 96141cc0ec..90b1372ce0 100644
--- a/Libraries/LibIPC/Encoder.h
+++ b/Libraries/LibIPC/Encoder.h
@@ -77,6 +77,15 @@ public:
}
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)
{
IPC::encode(*this, value);