Property.h 783 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Function.h>
  8. #include <AK/JsonValue.h>
  9. namespace Core {
  10. class Property {
  11. AK_MAKE_NONCOPYABLE(Property);
  12. public:
  13. Property(String name, Function<JsonValue()> getter, Function<bool(const JsonValue&)> setter = nullptr);
  14. ~Property();
  15. bool set(const JsonValue& value)
  16. {
  17. if (!m_setter)
  18. return false;
  19. return m_setter(value);
  20. }
  21. JsonValue get() const { return m_getter(); }
  22. const String& name() const { return m_name; }
  23. bool is_readonly() const { return !m_setter; }
  24. private:
  25. String m_name;
  26. Function<JsonValue()> m_getter;
  27. Function<bool(const JsonValue&)> m_setter;
  28. };
  29. }