Property.h 935 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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(DeprecatedString 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
  23. {
  24. if (!m_getter)
  25. return {};
  26. return m_getter();
  27. }
  28. DeprecatedString const& name() const { return m_name; }
  29. bool is_readonly() const { return !m_setter; }
  30. private:
  31. DeprecatedString m_name;
  32. Function<JsonValue()> m_getter;
  33. Function<bool(JsonValue const&)> m_setter;
  34. };
  35. }