summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/Variant.cpp
blob: 95fdbcb97649241fa9ccb0854cb5650b9a881566 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
 * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/JsonValue.h>
#include <AK/NonnullRefPtr.h>
#include <LibGUI/Icon.h>
#include <LibGUI/Variant.h>

namespace GUI {

Variant::Variant(JsonValue const& value)
{
    *this = value;
}

Variant& Variant::operator=(JsonValue const& value)
{
    if (value.is_null())
        return *this;

    if (value.is_i32()) {
        set(value.as_i32());
        return *this;
    }

    if (value.is_u32()) {
        set(value.as_u32());
        return *this;
    }

    if (value.is_i64()) {
        set(value.as_i64());
        return *this;
    }

    if (value.is_u64()) {
        set(value.as_u64());
        return *this;
    }

    if (value.is_string()) {
        set(value.as_string());
        return *this;
    }

    if (value.is_bool()) {
        set(Detail::Boolean { value.as_bool() });
        return *this;
    }

    VERIFY_NOT_REACHED();
}

bool Variant::operator==(Variant const& other) const
{
    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_deprecated_string() == other.to_deprecated_string();
            },
            [&](auto const&) {
                // FIXME: Figure out if this silly behaviour is actually used anywhere, then get rid of it.
                return to_deprecated_string() == other.to_deprecated_string();
            });
    });
}

bool Variant::operator<(Variant const& other) const
{
    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_deprecated_string() < other.to_deprecated_string();
            },
            [&](auto const&) -> bool {
                return to_deprecated_string() < other.to_deprecated_string(); // FIXME: Figure out if this silly behaviour is actually used anywhere, then get rid of it.
            });
    });
}
}