From 78943f031eabf208954358aef76615dbe17c41d4 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 3 May 2020 22:15:27 +0200 Subject: [PATCH] LibIPC: Add a simple IPC::Dictionary type (String key -> String value) --- DevTools/IPCCompiler/main.cpp | 1 + Libraries/LibIPC/Decoder.cpp | 24 +++++++++++++ Libraries/LibIPC/Decoder.h | 2 ++ Libraries/LibIPC/Dictionary.cpp | 0 Libraries/LibIPC/Dictionary.h | 60 +++++++++++++++++++++++++++++++++ Libraries/LibIPC/Encoder.cpp | 10 ++++++ Libraries/LibIPC/Encoder.h | 2 ++ Libraries/LibIPC/Forward.h | 1 + Libraries/LibIPC/Makefile | 1 + 9 files changed, 101 insertions(+) create mode 100644 Libraries/LibIPC/Dictionary.cpp create mode 100644 Libraries/LibIPC/Dictionary.h diff --git a/DevTools/IPCCompiler/main.cpp b/DevTools/IPCCompiler/main.cpp index a2164f52ea..1eacc6b177 100644 --- a/DevTools/IPCCompiler/main.cpp +++ b/DevTools/IPCCompiler/main.cpp @@ -230,6 +230,7 @@ int main(int argc, char** argv) out() << "#include "; out() << "#include "; out() << "#include "; + out() << "#include "; out() << "#include "; out() << "#include "; out() << "#include "; diff --git a/Libraries/LibIPC/Decoder.cpp b/Libraries/LibIPC/Decoder.cpp index 529b8d438e..0d9ed08318 100644 --- a/Libraries/LibIPC/Decoder.cpp +++ b/Libraries/LibIPC/Decoder.cpp @@ -26,6 +26,7 @@ #include #include +#include namespace IPC { @@ -112,4 +113,27 @@ bool Decoder::decode(String& value) return !m_stream.handle_read_failure(); } +bool Decoder::decode(Dictionary& dictionary) +{ + u64 size = 0; + m_stream >> size; + if (m_stream.handle_read_failure()) + return false; + if (size >= NumericLimits::max()) { + ASSERT_NOT_REACHED(); + } + + for (size_t i = 0; i < size; ++i) { + String key; + if (!decode(key)) + return false; + String value; + if (!decode(value)) + return false; + dictionary.add(move(key), move(value)); + } + + return true; +} + } diff --git a/Libraries/LibIPC/Decoder.h b/Libraries/LibIPC/Decoder.h index 4cba8ebb5a..dfc68f6f56 100644 --- a/Libraries/LibIPC/Decoder.h +++ b/Libraries/LibIPC/Decoder.h @@ -27,6 +27,7 @@ #pragma once #include +#include #include namespace IPC { @@ -55,6 +56,7 @@ public: bool decode(i64&); bool decode(float&); bool decode(String&); + bool decode(Dictionary&); template bool decode(T& value) diff --git a/Libraries/LibIPC/Dictionary.cpp b/Libraries/LibIPC/Dictionary.cpp new file mode 100644 index 0000000000..e69de29bb2 diff --git a/Libraries/LibIPC/Dictionary.h b/Libraries/LibIPC/Dictionary.h new file mode 100644 index 0000000000..f272ec9165 --- /dev/null +++ b/Libraries/LibIPC/Dictionary.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2020, Andreas Kling + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include +#include + +namespace IPC { + +class Dictionary { +public: + Dictionary() {} + + bool is_empty() const { return m_entries.is_empty(); } + size_t size() const { return m_entries.size(); } + + void add(String key, String value) + { + m_entries.set(move(key), move(value)); + } + + template + void for_each_entry(Callback callback) const + { + for (auto& it : m_entries) { + callback(it.key, it.value); + } + } + + const HashMap& entries() const { return m_entries; } + +private: + HashMap m_entries; +}; + +} diff --git a/Libraries/LibIPC/Encoder.cpp b/Libraries/LibIPC/Encoder.cpp index 61cc915d94..36f302dbeb 100644 --- a/Libraries/LibIPC/Encoder.cpp +++ b/Libraries/LibIPC/Encoder.cpp @@ -25,6 +25,7 @@ */ #include +#include #include namespace IPC { @@ -139,4 +140,13 @@ Encoder& Encoder::operator<<(const String& value) return *this << value.view(); } +Encoder& Encoder::operator<<(const Dictionary& dictionary) +{ + *this << (u64)dictionary.size(); + dictionary.for_each_entry([this](auto& key, auto& value) { + *this << key << value; + }); + return *this; +} + } diff --git a/Libraries/LibIPC/Encoder.h b/Libraries/LibIPC/Encoder.h index c77bccd951..742dc6bf69 100644 --- a/Libraries/LibIPC/Encoder.h +++ b/Libraries/LibIPC/Encoder.h @@ -26,6 +26,7 @@ #pragma once +#include #include namespace IPC { @@ -50,6 +51,7 @@ public: Encoder& operator<<(const char*); Encoder& operator<<(const StringView&); Encoder& operator<<(const String&); + Encoder& operator<<(const Dictionary&); private: MessageBuffer& m_buffer; diff --git a/Libraries/LibIPC/Forward.h b/Libraries/LibIPC/Forward.h index a0dcad1021..774aba1fdf 100644 --- a/Libraries/LibIPC/Forward.h +++ b/Libraries/LibIPC/Forward.h @@ -29,6 +29,7 @@ namespace IPC { class Decoder; +class Dictionary; class Encoder; class Message; diff --git a/Libraries/LibIPC/Makefile b/Libraries/LibIPC/Makefile index 38595cced0..a482e99f6f 100644 --- a/Libraries/LibIPC/Makefile +++ b/Libraries/LibIPC/Makefile @@ -1,5 +1,6 @@ OBJS = \ Decoder.o \ + Dictionary.o \ Encoder.o \ Endpoint.o \ Message.o