Variant.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
  4. * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/JsonValue.h>
  9. #include <AK/NonnullRefPtr.h>
  10. #include <LibGUI/Icon.h>
  11. #include <LibGUI/Variant.h>
  12. namespace GUI {
  13. Variant::Variant(JsonValue const& value)
  14. {
  15. *this = value;
  16. }
  17. Variant& Variant::operator=(JsonValue const& value)
  18. {
  19. if (value.is_null())
  20. return *this;
  21. if (value.is_i32()) {
  22. set(value.as_i32());
  23. return *this;
  24. }
  25. if (value.is_u32()) {
  26. set(value.as_u32());
  27. return *this;
  28. }
  29. if (value.is_i64()) {
  30. set(value.as_i64());
  31. return *this;
  32. }
  33. if (value.is_u64()) {
  34. set(value.as_u64());
  35. return *this;
  36. }
  37. if (value.is_string()) {
  38. set(value.as_string());
  39. return *this;
  40. }
  41. if (value.is_bool()) {
  42. set(Detail::Boolean { value.as_bool() });
  43. return *this;
  44. }
  45. VERIFY_NOT_REACHED();
  46. }
  47. bool Variant::operator==(Variant const& other) const
  48. {
  49. return visit([&]<typename T>(T const& own_value) {
  50. return other.visit(
  51. [&](T const& other_value) -> bool {
  52. if constexpr (requires { own_value == other_value; })
  53. return own_value == other_value;
  54. else if constexpr (IsSame<T, GUI::Icon>)
  55. return &own_value.impl() == &other_value.impl();
  56. // FIXME: Figure out if this silly behavior is actually used anywhere, then get rid of it.
  57. else
  58. return to_deprecated_string() == other.to_deprecated_string();
  59. },
  60. [&](auto const&) {
  61. // FIXME: Figure out if this silly behavior is actually used anywhere, then get rid of it.
  62. return to_deprecated_string() == other.to_deprecated_string();
  63. });
  64. });
  65. }
  66. bool Variant::operator<(Variant const& other) const
  67. {
  68. return visit([&]<typename T>(T const& own_value) {
  69. return other.visit(
  70. [&](T const& other_value) -> bool {
  71. // FIXME: Maybe compare icons somehow differently?
  72. if constexpr (IsSame<T, GUI::Icon>)
  73. return &own_value.impl() < &other_value.impl();
  74. // FIXME: Maybe compare bitmaps somehow differently?
  75. else if constexpr (IsSame<T, NonnullRefPtr<Gfx::Bitmap>>)
  76. return own_value.ptr() < other_value.ptr();
  77. else if constexpr (IsSame<T, NonnullRefPtr<Gfx::Font>>)
  78. return own_value->name() < other_value->name();
  79. else if constexpr (requires { own_value < other_value; })
  80. return own_value < other_value;
  81. // FIXME: Figure out if this silly behavior is actually used anywhere, then get rid of it.
  82. else
  83. return to_deprecated_string() < other.to_deprecated_string();
  84. },
  85. [&](auto const&) -> bool {
  86. return to_deprecated_string() < other.to_deprecated_string(); // FIXME: Figure out if this silly behavior is actually used anywhere, then get rid of it.
  87. });
  88. });
  89. }
  90. }