GVariant: Add Type::UnsignedInt.

This commit is contained in:
Andreas Kling 2019-07-31 07:07:59 +02:00
parent 34db7067fa
commit 10b4c92e21
Notes: sideshowbarker 2024-07-19 12:59:13 +09:00
2 changed files with 34 additions and 3 deletions

View file

@ -7,6 +7,7 @@ const char* to_string(GVariant::Type type)
case GVariant::Type::Invalid: return "Invalid";
case GVariant::Type::Bool: return "Bool";
case GVariant::Type::Int: return "Int";
case GVariant::Type::UnsignedInt: return "UnsignedInt";
case GVariant::Type::Float: return "Float";
case GVariant::Type::String: return "String";
case GVariant::Type::Bitmap: return "Bitmap";
@ -54,6 +55,12 @@ GVariant::GVariant(int value)
m_value.as_int = value;
}
GVariant::GVariant(unsigned value)
: m_type(Type::UnsignedInt)
{
m_value.as_uint = value;
}
GVariant::GVariant(float value)
: m_type(Type::Float)
{
@ -92,9 +99,8 @@ GVariant::GVariant(const JsonValue& value)
}
if (value.is_uint()) {
ASSERT(value.as_uint() < INT32_MAX);
m_type = Type::Int;
m_value.as_int = value.as_uint();
m_type = Type::UnsignedInt;
m_value.as_uint = value.as_uint();
return;
}
@ -188,6 +194,9 @@ void GVariant::copy_from(const GVariant& other)
case Type::Int:
m_value.as_int = other.m_value.as_int;
break;
case Type::UnsignedInt:
m_value.as_uint = other.m_value.as_uint;
break;
case Type::Float:
m_value.as_float = other.m_value.as_float;
break;
@ -229,6 +238,8 @@ bool GVariant::operator==(const GVariant& other) const
return as_bool() == other.as_bool();
case Type::Int:
return as_int() == other.as_int();
case Type::UnsignedInt:
return as_uint() == other.as_uint();
case Type::Float:
return as_float() == other.as_float();
case Type::String:
@ -260,6 +271,8 @@ bool GVariant::operator<(const GVariant& other) const
return as_bool() < other.as_bool();
case Type::Int:
return as_int() < other.as_int();
case Type::UnsignedInt:
return as_uint() < other.as_uint();
case Type::Float:
return as_float() < other.as_float();
case Type::String:
@ -290,6 +303,8 @@ String GVariant::to_string() const
return as_bool() ? "true" : "false";
case Type::Int:
return String::number(as_int());
case Type::UnsignedInt:
return String::number(as_uint());
case Type::Float:
return String::format("%f", (double)as_float());
case Type::String:

View file

@ -14,6 +14,7 @@ public:
GVariant(bool);
GVariant(float);
GVariant(int);
GVariant(unsigned);
GVariant(const char*);
GVariant(const String&);
GVariant(const GraphicsBitmap&);
@ -37,6 +38,7 @@ public:
Invalid,
Bool,
Int,
UnsignedInt,
Float,
String,
Bitmap,
@ -50,6 +52,7 @@ public:
bool is_valid() const { return m_type != Type::Invalid; }
bool is_bool() const { return m_type == Type::Bool; }
bool is_int() const { return m_type == Type::Int; }
bool is_uint() const { return m_type == Type::UnsignedInt; }
bool is_float() const { return m_type == Type::Float; }
bool is_string() const { return m_type == Type::String; }
bool is_bitmap() const { return m_type == Type::Bitmap; }
@ -74,6 +77,8 @@ public:
return !!m_value.as_string;
if (type() == Type::Int)
return m_value.as_int != 0;
if (type() == Type::UnsignedInt)
return m_value.as_uint != 0;
if (type() == Type::Rect)
return !as_rect().is_null();
if (type() == Type::Size)
@ -89,6 +94,12 @@ public:
return m_value.as_int;
}
unsigned as_uint() const
{
ASSERT(type() == Type::UnsignedInt);
return m_value.as_uint;
}
int to_int() const
{
if (is_int())
@ -97,6 +108,10 @@ public:
return as_bool() ? 1 : 0;
if (is_float())
return (int)as_float();
if (is_uint()) {
ASSERT(as_uint() <= INT32_MAX);
return (int)as_uint();
}
if (is_string()) {
bool ok;
int value = as_string().to_int(ok);
@ -195,6 +210,7 @@ private:
GIconImpl* as_icon;
bool as_bool;
int as_int;
unsigned as_uint;
float as_float;
RGBA32 as_color;
RawPoint as_point;