mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
LibIPC: Add a simple IPC::Dictionary type (String key -> String value)
This commit is contained in:
parent
dce3faff08
commit
78943f031e
Notes:
sideshowbarker
2024-07-19 07:00:34 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/78943f031ea
9 changed files with 101 additions and 0 deletions
|
@ -230,6 +230,7 @@ int main(int argc, char** argv)
|
|||
out() << "#include <LibGfx/Rect.h>";
|
||||
out() << "#include <LibGfx/ShareableBitmap.h>";
|
||||
out() << "#include <LibIPC/Decoder.h>";
|
||||
out() << "#include <LibIPC/Dictionary.h>";
|
||||
out() << "#include <LibIPC/Encoder.h>";
|
||||
out() << "#include <LibIPC/Endpoint.h>";
|
||||
out() << "#include <LibIPC/Message.h>";
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
|
||||
#include <AK/BufferStream.h>
|
||||
#include <LibIPC/Decoder.h>
|
||||
#include <LibIPC/Dictionary.h>
|
||||
|
||||
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<i32>::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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Forward.h>
|
||||
#include <LibIPC/Forward.h>
|
||||
#include <LibIPC/Message.h>
|
||||
|
||||
namespace IPC {
|
||||
|
@ -55,6 +56,7 @@ public:
|
|||
bool decode(i64&);
|
||||
bool decode(float&);
|
||||
bool decode(String&);
|
||||
bool decode(Dictionary&);
|
||||
|
||||
template<typename T>
|
||||
bool decode(T& value)
|
||||
|
|
0
Libraries/LibIPC/Dictionary.cpp
Normal file
0
Libraries/LibIPC/Dictionary.cpp
Normal file
60
Libraries/LibIPC/Dictionary.h
Normal file
60
Libraries/LibIPC/Dictionary.h
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* 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 <AK/HashMap.h>
|
||||
#include <AK/String.h>
|
||||
|
||||
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<typename Callback>
|
||||
void for_each_entry(Callback callback) const
|
||||
{
|
||||
for (auto& it : m_entries) {
|
||||
callback(it.key, it.value);
|
||||
}
|
||||
}
|
||||
|
||||
const HashMap<String, String>& entries() const { return m_entries; }
|
||||
|
||||
private:
|
||||
HashMap<String, String> m_entries;
|
||||
};
|
||||
|
||||
}
|
|
@ -25,6 +25,7 @@
|
|||
*/
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <LibIPC/Dictionary.h>
|
||||
#include <LibIPC/Encoder.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <LibIPC/Forward.h>
|
||||
#include <LibIPC/Message.h>
|
||||
|
||||
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;
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
namespace IPC {
|
||||
|
||||
class Decoder;
|
||||
class Dictionary;
|
||||
class Encoder;
|
||||
class Message;
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
OBJS = \
|
||||
Decoder.o \
|
||||
Dictionary.o \
|
||||
Encoder.o \
|
||||
Endpoint.o \
|
||||
Message.o
|
||||
|
|
Loading…
Reference in a new issue