Variant.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/NonnullRefPtr.h>
  9. #include <LibGUI/Icon.h>
  10. #include <LibGUI/Variant.h>
  11. namespace GUI {
  12. Variant::Variant(JsonValue const& value)
  13. {
  14. *this = value;
  15. }
  16. Variant& Variant::operator=(JsonValue const& value)
  17. {
  18. if (value.is_null())
  19. return *this;
  20. if (value.is_i32()) {
  21. set(value.as_i32());
  22. return *this;
  23. }
  24. if (value.is_u32()) {
  25. set(value.as_u32());
  26. return *this;
  27. }
  28. if (value.is_i64()) {
  29. set(value.as_i64());
  30. return *this;
  31. }
  32. if (value.is_u64()) {
  33. set(value.as_u64());
  34. return *this;
  35. }
  36. if (value.is_string()) {
  37. set(value.as_string());
  38. return *this;
  39. }
  40. if (value.is_bool()) {
  41. set(Detail::Boolean { value.as_bool() });
  42. return *this;
  43. }
  44. VERIFY_NOT_REACHED();
  45. }
  46. bool Variant::operator==(Variant const& other) const
  47. {
  48. return visit([&]<typename T>(T const& own_value) {
  49. return other.visit(
  50. [&](T const& other_value) -> bool {
  51. if constexpr (requires { own_value == other_value; })
  52. return own_value == other_value;
  53. else if constexpr (IsSame<T, GUI::Icon>)
  54. return &own_value.impl() == &other_value.impl();
  55. // FIXME: Figure out if this silly behaviour is actually used anywhere, then get rid of it.
  56. else
  57. return to_string() == other.to_string();
  58. },
  59. [&](auto const&) {
  60. // FIXME: Figure out if this silly behaviour is actually used anywhere, then get rid of it.
  61. return to_string() == other.to_string();
  62. });
  63. });
  64. }
  65. bool Variant::operator<(Variant const& other) const
  66. {
  67. return visit([&]<typename T>(T const& own_value) {
  68. return other.visit(
  69. [&](T const& other_value) -> bool {
  70. // FIXME: Maybe compare icons somehow differently?
  71. if constexpr (IsSame<T, GUI::Icon>)
  72. return &own_value.impl() < &other_value.impl();
  73. // FIXME: Maybe compare bitmaps somehow differently?
  74. else if constexpr (IsSame<T, NonnullRefPtr<Gfx::Bitmap>>)
  75. return own_value.ptr() < other_value.ptr();
  76. else if constexpr (IsSame<T, NonnullRefPtr<Gfx::Font>>)
  77. return own_value->name() < other_value->name();
  78. else if constexpr (requires { own_value < other_value; })
  79. return own_value < other_value;
  80. // FIXME: Figure out if this silly behaviour is actually used anywhere, then get rid of it.
  81. else
  82. return to_string() < other.to_string();
  83. },
  84. [&](auto const&) -> bool {
  85. return to_string() < other.to_string(); // FIXME: Figure out if this silly behaviour is actually used anywhere, then get rid of it.
  86. });
  87. });
  88. }
  89. }