1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:57:43 +00:00

LibIPC: Add encoder and decoder for AK::OrderedHashMap

Seems like a useful thing to have.
This commit is contained in:
Valtteri Koskivuori 2022-04-03 00:43:22 +03:00 committed by Linus Groh
parent a8a50a994b
commit f2b4c044db
2 changed files with 29 additions and 0 deletions

View file

@ -67,6 +67,24 @@ public:
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)
{

View file

@ -57,6 +57,17 @@ public:
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)
{