Property.h 843 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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/Function.h>
  9. #include <AK/JsonValue.h>
  10. namespace Core {
  11. class Property {
  12. AK_MAKE_NONCOPYABLE(Property);
  13. public:
  14. Property(String name, Function<JsonValue()> getter, Function<bool(JsonValue const&)> setter = nullptr);
  15. ~Property() = default;
  16. bool set(JsonValue const& value)
  17. {
  18. if (!m_setter)
  19. return false;
  20. return m_setter(value);
  21. }
  22. JsonValue get() const { return m_getter(); }
  23. String const& name() const { return m_name; }
  24. bool is_readonly() const { return !m_setter; }
  25. private:
  26. String m_name;
  27. Function<JsonValue()> m_getter;
  28. Function<bool(JsonValue const&)> m_setter;
  29. };
  30. }