summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibIPC/Dictionary.h
blob: 9a83c03d2c0e168ee7b753027cbd6192ac83a3da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Concepts.h>
#include <AK/DeprecatedString.h>
#include <AK/HashMap.h>

namespace IPC {

class Dictionary {
public:
    Dictionary() = default;

    Dictionary(HashMap<DeprecatedString, DeprecatedString> const& initial_entries)
        : m_entries(initial_entries)
    {
    }

    bool is_empty() const { return m_entries.is_empty(); }
    size_t size() const { return m_entries.size(); }

    void add(DeprecatedString key, DeprecatedString 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);
        }
    }

    template<FallibleFunction<DeprecatedString const&, DeprecatedString const&> Callback>
    ErrorOr<void> try_for_each_entry(Callback&& callback) const
    {
        for (auto const& it : m_entries)
            TRY(callback(it.key, it.value));
        return {};
    }

    HashMap<DeprecatedString, DeprecatedString> const& entries() const { return m_entries; }

private:
    HashMap<DeprecatedString, DeprecatedString> m_entries;
};

}