2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-01-01 18:26:19 +01:00
|
|
|
* Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
|
2022-04-17 09:24:55 +04:30
|
|
|
* Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2022-04-17 09:24:55 +04:30
|
|
|
#include <AK/NonnullRefPtr.h>
|
|
|
|
#include <LibGUI/Icon.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Variant.h>
|
2019-02-28 16:20:29 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
2022-04-17 09:24:55 +04:30
|
|
|
Variant::Variant(JsonValue const& value)
|
2019-02-28 16:20:29 +01:00
|
|
|
{
|
2022-04-17 09:24:55 +04:30
|
|
|
*this = value;
|
2019-02-28 16:20:29 +01:00
|
|
|
}
|
|
|
|
|
2022-04-17 09:24:55 +04:30
|
|
|
Variant& Variant::operator=(JsonValue const& value)
|
2019-06-29 12:06:26 +02:00
|
|
|
{
|
2022-04-17 09:24:55 +04:30
|
|
|
if (value.is_null())
|
|
|
|
return *this;
|
2019-06-29 12:06:26 +02:00
|
|
|
|
2019-10-29 16:36:50 +01:00
|
|
|
if (value.is_i32()) {
|
2022-04-17 09:24:55 +04:30
|
|
|
set(value.as_i32());
|
|
|
|
return *this;
|
2019-06-29 12:06:26 +02:00
|
|
|
}
|
|
|
|
|
2019-10-29 16:36:50 +01:00
|
|
|
if (value.is_u32()) {
|
2022-04-17 09:24:55 +04:30
|
|
|
set(value.as_u32());
|
|
|
|
return *this;
|
2019-10-29 16:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value.is_i64()) {
|
2022-04-17 09:24:55 +04:30
|
|
|
set(value.as_i64());
|
|
|
|
return *this;
|
2019-10-29 16:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value.is_u64()) {
|
2022-04-17 09:24:55 +04:30
|
|
|
set(value.as_u64());
|
|
|
|
return *this;
|
2019-06-29 12:06:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value.is_string()) {
|
2022-04-17 09:24:55 +04:30
|
|
|
set(value.as_string());
|
|
|
|
return *this;
|
2019-06-29 12:06:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value.is_bool()) {
|
2022-04-17 09:24:55 +04:30
|
|
|
set(Detail::Boolean { value.as_bool() });
|
|
|
|
return *this;
|
2019-06-29 12:06:26 +02:00
|
|
|
}
|
|
|
|
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-06-29 12:06:26 +02:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool Variant::operator==(Variant const& other) const
|
2019-03-09 13:33:52 +01:00
|
|
|
{
|
2022-04-17 09:24:55 +04:30
|
|
|
return visit([&]<typename T>(T const& own_value) {
|
|
|
|
return other.visit(
|
|
|
|
[&](T const& other_value) -> bool {
|
|
|
|
if constexpr (requires { own_value == other_value; })
|
|
|
|
return own_value == other_value;
|
|
|
|
else if constexpr (IsSame<T, GUI::Icon>)
|
|
|
|
return &own_value.impl() == &other_value.impl();
|
|
|
|
// FIXME: Figure out if this silly behaviour is actually used anywhere, then get rid of it.
|
|
|
|
else
|
|
|
|
return to_string() == other.to_string();
|
|
|
|
},
|
|
|
|
[&](auto const&) {
|
|
|
|
// FIXME: Figure out if this silly behaviour is actually used anywhere, then get rid of it.
|
|
|
|
return to_string() == other.to_string();
|
|
|
|
});
|
|
|
|
});
|
2019-03-09 13:33:52 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool Variant::operator<(Variant const& other) const
|
2019-03-09 13:33:52 +01:00
|
|
|
{
|
2022-04-17 09:24:55 +04:30
|
|
|
return visit([&]<typename T>(T const& own_value) {
|
|
|
|
return other.visit(
|
|
|
|
[&](T const& other_value) -> bool {
|
|
|
|
// FIXME: Maybe compare icons somehow differently?
|
|
|
|
if constexpr (IsSame<T, GUI::Icon>)
|
|
|
|
return &own_value.impl() < &other_value.impl();
|
|
|
|
// FIXME: Maybe compare bitmaps somehow differently?
|
|
|
|
else if constexpr (IsSame<T, NonnullRefPtr<Gfx::Bitmap>>)
|
|
|
|
return own_value.ptr() < other_value.ptr();
|
|
|
|
else if constexpr (IsSame<T, NonnullRefPtr<Gfx::Font>>)
|
|
|
|
return own_value->name() < other_value->name();
|
|
|
|
else if constexpr (requires { own_value < other_value; })
|
|
|
|
return own_value < other_value;
|
|
|
|
// FIXME: Figure out if this silly behaviour is actually used anywhere, then get rid of it.
|
|
|
|
else
|
|
|
|
return to_string() < other.to_string();
|
|
|
|
},
|
|
|
|
[&](auto const&) -> bool {
|
|
|
|
return to_string() < other.to_string(); // FIXME: Figure out if this silly behaviour is actually used anywhere, then get rid of it.
|
|
|
|
});
|
|
|
|
});
|
2019-03-09 13:33:52 +01:00
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
}
|