1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 20:07:36 +00:00

LibGUI: Add u64 type to LibGUI::Variant

`uint` has also been more appropriately renamed to
u32.
This commit is contained in:
Jesse Buhagiar 2021-05-26 23:23:31 +10:00 committed by Linus Groh
parent 9adcfd5726
commit 786275feb8
3 changed files with 63 additions and 31 deletions

View file

@ -177,7 +177,7 @@ void AutocompleteBox::apply_suggestion()
VERIFY(suggestion.length() >= partial_length); VERIFY(suggestion.length() >= partial_length);
auto completion_view = suggestion.substring_view(partial_length, suggestion.length() - partial_length); auto completion_view = suggestion.substring_view(partial_length, suggestion.length() - partial_length);
auto completion_kind = (GUI::AutocompleteProvider::CompletionKind)suggestion_index.data((GUI::ModelRole)AutocompleteSuggestionModel::InternalRole::Kind).as_uint(); auto completion_kind = (GUI::AutocompleteProvider::CompletionKind)suggestion_index.data((GUI::ModelRole)AutocompleteSuggestionModel::InternalRole::Kind).as_u32();
String completion; String completion;
if (completion_view.ends_with(".h") && completion_kind == GUI::AutocompleteProvider::CompletionKind::SystemInclude) if (completion_view.ends_with(".h") && completion_kind == GUI::AutocompleteProvider::CompletionKind::SystemInclude)

View file

@ -22,8 +22,10 @@ const char* to_string(Variant::Type type)
return "Int32"; return "Int32";
case Variant::Type::Int64: case Variant::Type::Int64:
return "Int64"; return "Int64";
case Variant::Type::UnsignedInt: case Variant::Type::UnsignedInt32:
return "UnsignedInt"; return "UnsignedInt32";
case Variant::Type::UnsignedInt64:
return "UnsignedInt64";
case Variant::Type::Float: case Variant::Type::Float:
return "Float"; return "Float";
case Variant::Type::String: case Variant::Type::String:
@ -95,10 +97,16 @@ Variant::Variant(i64 value)
m_value.as_i64 = value; m_value.as_i64 = value;
} }
Variant::Variant(unsigned value) Variant::Variant(u32 value)
: m_type(Type::UnsignedInt) : m_type(Type::UnsignedInt32)
{ {
m_value.as_uint = value; m_value.as_u32 = value;
}
Variant::Variant(u64 value)
: m_type(Type::UnsignedInt64)
{
m_value.as_u64 = value;
} }
Variant::Variant(float value) Variant::Variant(float value)
@ -149,8 +157,8 @@ Variant::Variant(const JsonValue& value)
} }
if (value.is_u32()) { if (value.is_u32()) {
m_type = Type::UnsignedInt; m_type = Type::UnsignedInt32;
m_value.as_uint = value.as_u32(); m_value.as_u32 = value.as_u32();
return; return;
} }
@ -161,9 +169,8 @@ Variant::Variant(const JsonValue& value)
} }
if (value.is_u64()) { if (value.is_u64()) {
// FIXME: Variant should have a 64-bit internal type. m_type = Type::UnsignedInt64;
m_type = Type::UnsignedInt; m_value.as_u64 = value.to_u64();
m_value.as_uint = value.to_u32();
return; return;
} }
@ -273,8 +280,11 @@ void Variant::copy_from(const Variant& other)
case Type::Int64: case Type::Int64:
m_value.as_i64 = other.m_value.as_i64; m_value.as_i64 = other.m_value.as_i64;
break; break;
case Type::UnsignedInt: case Type::UnsignedInt32:
m_value.as_uint = other.m_value.as_uint; m_value.as_u32 = other.m_value.as_u32;
break;
case Type::UnsignedInt64:
m_value.as_u64 = other.m_value.as_u64;
break; break;
case Type::Float: case Type::Float:
m_value.as_float = other.m_value.as_float; m_value.as_float = other.m_value.as_float;
@ -326,8 +336,10 @@ bool Variant::operator==(const Variant& other) const
return as_i32() == other.as_i32(); return as_i32() == other.as_i32();
case Type::Int64: case Type::Int64:
return as_i64() == other.as_i64(); return as_i64() == other.as_i64();
case Type::UnsignedInt: case Type::UnsignedInt32:
return as_uint() == other.as_uint(); return as_u32() == other.as_u32();
case Type::UnsignedInt64:
return as_u64() == other.as_u64();
case Type::Float: case Type::Float:
return as_float() == other.as_float(); return as_float() == other.as_float();
case Type::String: case Type::String:
@ -365,8 +377,10 @@ bool Variant::operator<(const Variant& other) const
return as_i32() < other.as_i32(); return as_i32() < other.as_i32();
case Type::Int64: case Type::Int64:
return as_i64() < other.as_i64(); return as_i64() < other.as_i64();
case Type::UnsignedInt: case Type::UnsignedInt32:
return as_uint() < other.as_uint(); return as_u32() < other.as_u32();
case Type::UnsignedInt64:
return as_u64() < other.as_u64();
case Type::Float: case Type::Float:
return as_float() < other.as_float(); return as_float() < other.as_float();
case Type::String: case Type::String:
@ -401,8 +415,10 @@ String Variant::to_string() const
return String::number(as_i32()); return String::number(as_i32());
case Type::Int64: case Type::Int64:
return String::number(as_i64()); return String::number(as_i64());
case Type::UnsignedInt: case Type::UnsignedInt32:
return String::number(as_uint()); return String::number(as_u32());
case Type::UnsignedInt64:
return String::number(as_u64());
case Type::Float: case Type::Float:
return String::formatted("{:.2}", as_float()); return String::formatted("{:.2}", as_float());
case Type::String: case Type::String:

View file

@ -20,7 +20,8 @@ public:
Variant(float); Variant(float);
Variant(i32); Variant(i32);
Variant(i64); Variant(i64);
Variant(unsigned); Variant(u32);
Variant(u64);
Variant(const char*); Variant(const char*);
Variant(const StringView&); Variant(const StringView&);
Variant(const String&); Variant(const String&);
@ -49,7 +50,8 @@ public:
Bool, Bool,
Int32, Int32,
Int64, Int64,
UnsignedInt, UnsignedInt32,
UnsignedInt64,
Float, Float,
String, String,
Bitmap, Bitmap,
@ -66,7 +68,8 @@ public:
bool is_bool() const { return m_type == Type::Bool; } bool is_bool() const { return m_type == Type::Bool; }
bool is_i32() const { return m_type == Type::Int32; } bool is_i32() const { return m_type == Type::Int32; }
bool is_i64() const { return m_type == Type::Int64; } bool is_i64() const { return m_type == Type::Int64; }
bool is_uint() const { return m_type == Type::UnsignedInt; } bool is_u32() const { return m_type == Type::UnsignedInt32; }
bool is_u64() const { return m_type == Type::UnsignedInt64; }
bool is_float() const { return m_type == Type::Float; } bool is_float() const { return m_type == Type::Float; }
bool is_string() const { return m_type == Type::String; } bool is_string() const { return m_type == Type::String; }
bool is_bitmap() const { return m_type == Type::Bitmap; } bool is_bitmap() const { return m_type == Type::Bitmap; }
@ -95,8 +98,10 @@ public:
return m_value.as_i32 != 0; return m_value.as_i32 != 0;
if (type() == Type::Int64) if (type() == Type::Int64)
return m_value.as_i64 != 0; return m_value.as_i64 != 0;
if (type() == Type::UnsignedInt) if (type() == Type::UnsignedInt32)
return m_value.as_uint != 0; return m_value.as_u32 != 0;
if (type() == Type::UnsignedInt64)
return m_value.as_u64 != 0;
if (type() == Type::Rect) if (type() == Type::Rect)
return !as_rect().is_null(); return !as_rect().is_null();
if (type() == Type::Size) if (type() == Type::Size)
@ -118,10 +123,16 @@ public:
return m_value.as_i64; return m_value.as_i64;
} }
unsigned as_uint() const u32 as_u32() const
{ {
VERIFY(type() == Type::UnsignedInt); VERIFY(type() == Type::UnsignedInt32);
return m_value.as_uint; return m_value.as_u32;
}
u64 as_u64() const
{
VERIFY(type() == Type::UnsignedInt64);
return m_value.as_u64;
} }
template<typename T> template<typename T>
@ -135,9 +146,13 @@ public:
return as_bool() ? 1 : 0; return as_bool() ? 1 : 0;
if (is_float()) if (is_float())
return (int)as_float(); return (int)as_float();
if (is_uint()) { if (is_u32()) {
VERIFY(as_uint() <= INT32_MAX); VERIFY(as_u32() <= INT32_MAX);
return (int)as_uint(); return static_cast<i32>(as_u32());
}
if (is_u64()) {
VERIFY(as_u64() <= INT64_MAX);
return static_cast<i64>(as_u64());
} }
if (is_string()) if (is_string())
return as_string().to_int().value_or(0); return as_string().to_int().value_or(0);
@ -256,7 +271,8 @@ private:
bool as_bool; bool as_bool;
i32 as_i32; i32 as_i32;
i64 as_i64; i64 as_i64;
unsigned as_uint; u32 as_u32;
u64 as_u64;
float as_float; float as_float;
Gfx::RGBA32 as_color; Gfx::RGBA32 as_color;
Gfx::TextAlignment as_text_alignment; Gfx::TextAlignment as_text_alignment;