Variant.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_double()) {
  38. set(value.as_double());
  39. return *this;
  40. }
  41. if (value.is_string()) {
  42. set(value.as_string());
  43. return *this;
  44. }
  45. if (value.is_bool()) {
  46. set(Detail::Boolean { value.as_bool() });
  47. return *this;
  48. }
  49. VERIFY_NOT_REACHED();
  50. }
  51. bool Variant::operator==(Variant const& other) const
  52. {
  53. return visit([&]<typename T>(T const& own_value) {
  54. return other.visit(
  55. [&](T const& other_value) -> bool {
  56. if constexpr (requires { own_value == other_value; })
  57. return own_value == other_value;
  58. else if constexpr (IsSame<T, GUI::Icon>)
  59. return &own_value.impl() == &other_value.impl();
  60. // FIXME: Figure out if this silly behavior is actually used anywhere, then get rid of it.
  61. else
  62. return to_deprecated_string() == other.to_deprecated_string();
  63. },
  64. [&](auto const&) {
  65. // FIXME: Figure out if this silly behavior is actually used anywhere, then get rid of it.
  66. return to_deprecated_string() == other.to_deprecated_string();
  67. });
  68. });
  69. }
  70. bool Variant::operator<(Variant const& other) const
  71. {
  72. return visit([&]<typename T>(T const& own_value) {
  73. return other.visit(
  74. [&](T const& other_value) -> bool {
  75. // FIXME: Maybe compare icons somehow differently?
  76. if constexpr (IsSame<T, GUI::Icon>)
  77. return &own_value.impl() < &other_value.impl();
  78. // FIXME: Maybe compare bitmaps somehow differently?
  79. else if constexpr (IsSame<T, NonnullRefPtr<Gfx::Bitmap>>)
  80. return own_value.ptr() < other_value.ptr();
  81. else if constexpr (IsSame<T, NonnullRefPtr<Gfx::Font>>)
  82. return own_value->name() < other_value->name();
  83. else if constexpr (requires { own_value < other_value; })
  84. return own_value < other_value;
  85. // FIXME: Figure out if this silly behavior is actually used anywhere, then get rid of it.
  86. else
  87. return to_deprecated_string() < other.to_deprecated_string();
  88. },
  89. [&](auto const&) -> bool {
  90. return to_deprecated_string() < other.to_deprecated_string(); // FIXME: Figure out if this silly behavior is actually used anywhere, then get rid of it.
  91. });
  92. });
  93. }
  94. }