summaryrefslogtreecommitdiff
path: root/LibGUI/GVariant.cpp
blob: 3592759807f59876f0f96c0103c16929b32e0913 (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
#include <LibGUI/GVariant.h>

GVariant::~GVariant()
{
    switch (m_type) {
    case Type::String:
        if (m_value.as_string)
            m_value.as_string->release();
        break;
    case Type::Bitmap:
        if (m_value.as_bitmap)
            m_value.as_bitmap->release();
        break;
    default:
        break;
    }
}

GVariant::GVariant(int value)
    : m_type(Type::Int)
{
    m_value.as_int = value;
}

GVariant::GVariant(float value)
    : m_type(Type::Float)
{
    m_value.as_float = value;
}

GVariant::GVariant(bool value)
    : m_type(Type::Bool)
{
    m_value.as_bool = value;
}

GVariant::GVariant(const String& value)
    : m_type(Type::String)
{
    m_value.as_string = const_cast<StringImpl*>(value.impl());
    AK::retain_if_not_null(m_value.as_string);
}

GVariant::GVariant(const GraphicsBitmap& value)
    : m_type(Type::Bitmap)
{
    m_value.as_bitmap = const_cast<GraphicsBitmap*>(&value);
    AK::retain_if_not_null(m_value.as_bitmap);
}

String GVariant::to_string() const
{
    switch (m_type) {
    case Type::Bool:
        return as_bool() ? "True" : "False";
    case Type::Int:
        return String::format("%d", as_int());
    case Type::Float:
        return String::format("%f", (double)as_float());
    case Type::String:
        return as_string();
    case Type::Bitmap:
        return "[GraphicsBitmap]";
    case Type::Invalid:
        break;
    }
    ASSERT_NOT_REACHED();
}