Skip to content

Commit

Permalink
LibIPC: Add encoder and decoder for AK::OrderedHashMap
Browse files Browse the repository at this point in the history
Seems like a useful thing to have.
  • Loading branch information
vkoskiv authored and linusg committed Apr 3, 2022
1 parent a8a50a9 commit f2b4c04
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Userland/Libraries/LibIPC/Decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ class Decoder {
return {};
}

template<typename K, typename V>
ErrorOr<void> decode(OrderedHashMap<K, V>& hashmap)
{
u32 size;
TRY(decode(size));
if (size > NumericLimits<i32>::max())
return Error::from_string_literal("IPC: Invalid HashMap size"sv);

for (size_t i = 0; i < size; ++i) {
K key;
TRY(decode(key));
V value;
TRY(decode(value));
TRY(hashmap.try_set(move(key), move(value)));
}
return {};
}

template<Enum T>
ErrorOr<void> decode(T& enum_value)
{
Expand Down
11 changes: 11 additions & 0 deletions Userland/Libraries/LibIPC/Encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ class Encoder {
return *this;
}

template<typename K, typename V>
Encoder& operator<<(OrderedHashMap<K, V> const& hashmap)
{
*this << (u32)hashmap.size();
for (auto it : hashmap) {
*this << it.key;
*this << it.value;
}
return *this;
}

template<typename T>
Encoder& operator<<(Vector<T> const& vector)
{
Expand Down

0 comments on commit f2b4c04

Please sign in to comment.