Dictionary.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Concepts.h>
  9. #include <AK/DeprecatedString.h>
  10. #include <AK/HashMap.h>
  11. namespace IPC {
  12. class Dictionary {
  13. public:
  14. Dictionary() = default;
  15. Dictionary(HashMap<DeprecatedString, DeprecatedString> const& initial_entries)
  16. : m_entries(initial_entries)
  17. {
  18. }
  19. bool is_empty() const { return m_entries.is_empty(); }
  20. size_t size() const { return m_entries.size(); }
  21. void add(DeprecatedString key, DeprecatedString value)
  22. {
  23. m_entries.set(move(key), move(value));
  24. }
  25. template<typename Callback>
  26. void for_each_entry(Callback callback) const
  27. {
  28. for (auto& it : m_entries) {
  29. callback(it.key, it.value);
  30. }
  31. }
  32. template<FallibleFunction<DeprecatedString const&, DeprecatedString const&> Callback>
  33. ErrorOr<void> try_for_each_entry(Callback&& callback) const
  34. {
  35. for (auto const& it : m_entries)
  36. TRY(callback(it.key, it.value));
  37. return {};
  38. }
  39. HashMap<DeprecatedString, DeprecatedString> const& entries() const { return m_entries; }
  40. private:
  41. HashMap<DeprecatedString, DeprecatedString> m_entries;
  42. };
  43. }