| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/AKString.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-24 04:28:36 +01:00
										 |  |  | #include <LibGUI/GIcon.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  | #include <SharedGraphics/GraphicsBitmap.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class GVariant { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     GVariant(); | 
					
						
							|  |  |  |     GVariant(bool); | 
					
						
							|  |  |  |     GVariant(float); | 
					
						
							|  |  |  |     GVariant(int); | 
					
						
							| 
									
										
										
										
											2019-06-22 10:37:44 +02:00
										 |  |  |     GVariant(const char*); | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |     GVariant(const String&); | 
					
						
							|  |  |  |     GVariant(const GraphicsBitmap&); | 
					
						
							| 
									
										
										
										
											2019-03-24 04:28:36 +01:00
										 |  |  |     GVariant(const GIcon&); | 
					
						
							| 
									
										
										
										
											2019-04-11 22:52:34 +02:00
										 |  |  |     GVariant(const Point&); | 
					
						
							|  |  |  |     GVariant(const Size&); | 
					
						
							|  |  |  |     GVariant(const Rect&); | 
					
						
							| 
									
										
										
										
											2019-03-18 04:54:07 +01:00
										 |  |  |     GVariant(Color); | 
					
						
							| 
									
										
										
										
											2019-04-11 21:36:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     GVariant(const GVariant&); | 
					
						
							| 
									
										
										
										
											2019-04-12 14:43:44 +02:00
										 |  |  |     GVariant& operator=(const GVariant&); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     GVariant(GVariant&&) = delete; | 
					
						
							| 
									
										
										
										
											2019-04-19 01:05:59 +02:00
										 |  |  |     GVariant& operator=(GVariant&&); | 
					
						
							| 
									
										
										
										
											2019-04-12 14:43:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     void clear(); | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |     ~GVariant(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-07 17:13:23 +02:00
										 |  |  |     enum class Type { | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |         Invalid, | 
					
						
							|  |  |  |         Bool, | 
					
						
							|  |  |  |         Int, | 
					
						
							|  |  |  |         Float, | 
					
						
							|  |  |  |         String, | 
					
						
							|  |  |  |         Bitmap, | 
					
						
							| 
									
										
										
										
											2019-03-18 04:54:07 +01:00
										 |  |  |         Color, | 
					
						
							| 
									
										
										
										
											2019-03-24 04:28:36 +01:00
										 |  |  |         Icon, | 
					
						
							| 
									
										
										
										
											2019-04-11 22:52:34 +02:00
										 |  |  |         Point, | 
					
						
							|  |  |  |         Size, | 
					
						
							|  |  |  |         Rect, | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool is_valid() const { return m_type != Type::Invalid; } | 
					
						
							| 
									
										
										
										
											2019-02-28 20:16:10 +01:00
										 |  |  |     bool is_bool() const { return m_type == Type::Bool; } | 
					
						
							|  |  |  |     bool is_int() const { return m_type == Type::Int; } | 
					
						
							|  |  |  |     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; } | 
					
						
							| 
									
										
										
										
											2019-03-18 04:54:07 +01:00
										 |  |  |     bool is_color() const { return m_type == Type::Color; } | 
					
						
							| 
									
										
										
										
											2019-03-24 04:28:36 +01:00
										 |  |  |     bool is_icon() const { return m_type == Type::Icon; } | 
					
						
							| 
									
										
										
										
											2019-04-11 22:52:34 +02:00
										 |  |  |     bool is_point() const { return m_type == Type::Point; } | 
					
						
							|  |  |  |     bool is_size() const { return m_type == Type::Size; } | 
					
						
							|  |  |  |     bool is_rect() const { return m_type == Type::Rect; } | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |     Type type() const { return m_type; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool as_bool() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ASSERT(type() == Type::Bool); | 
					
						
							|  |  |  |         return m_value.as_bool; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-13 12:35:19 +02:00
										 |  |  |     bool to_bool() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (type() == Type::Bool) | 
					
						
							|  |  |  |             return as_bool(); | 
					
						
							|  |  |  |         if (type() == Type::String) | 
					
						
							|  |  |  |             return !!m_value.as_string; | 
					
						
							|  |  |  |         if (type() == Type::Int) | 
					
						
							|  |  |  |             return m_value.as_int != 0; | 
					
						
							|  |  |  |         if (type() == Type::Rect) | 
					
						
							|  |  |  |             return !as_rect().is_null(); | 
					
						
							|  |  |  |         if (type() == Type::Size) | 
					
						
							|  |  |  |             return !as_size().is_null(); | 
					
						
							|  |  |  |         if (type() == Type::Point) | 
					
						
							|  |  |  |             return !as_point().is_null(); | 
					
						
							|  |  |  |         return is_valid(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |     int as_int() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ASSERT(type() == Type::Int); | 
					
						
							|  |  |  |         return m_value.as_int; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-13 12:35:19 +02:00
										 |  |  |     int to_int() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (is_int()) | 
					
						
							|  |  |  |             return as_int(); | 
					
						
							|  |  |  |         if (is_bool()) | 
					
						
							|  |  |  |             return as_bool() ? 1 : 0; | 
					
						
							|  |  |  |         if (is_float()) | 
					
						
							|  |  |  |             return (int)as_float(); | 
					
						
							| 
									
										
										
										
											2019-04-19 01:05:59 +02:00
										 |  |  |         if (is_string()) { | 
					
						
							|  |  |  |             bool ok; | 
					
						
							|  |  |  |             int value = as_string().to_int(ok); | 
					
						
							|  |  |  |             if (!ok) | 
					
						
							|  |  |  |                 return 0; | 
					
						
							|  |  |  |             return value; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-04-13 12:35:19 +02:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |     float as_float() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ASSERT(type() == Type::Float); | 
					
						
							|  |  |  |         return m_value.as_float; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 22:52:34 +02:00
										 |  |  |     Point as_point() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return { m_value.as_point.x, m_value.as_point.y }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Size as_size() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return { m_value.as_size.width, m_value.as_size.height }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Rect as_rect() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return { as_point(), as_size() }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |     String as_string() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ASSERT(type() == Type::String); | 
					
						
							| 
									
										
										
										
											2019-04-12 14:43:44 +02:00
										 |  |  |         return m_value.as_string; | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const GraphicsBitmap& as_bitmap() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ASSERT(type() == Type::Bitmap); | 
					
						
							|  |  |  |         return *m_value.as_bitmap; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 04:28:36 +01:00
										 |  |  |     GIcon as_icon() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ASSERT(type() == Type::Icon); | 
					
						
							|  |  |  |         return GIcon(*m_value.as_icon); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-18 04:54:07 +01:00
										 |  |  |     Color as_color() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ASSERT(type() == Type::Color); | 
					
						
							|  |  |  |         return Color::from_rgba(m_value.as_color); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-13 12:35:19 +02:00
										 |  |  |     Color to_color() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (is_color()) | 
					
						
							|  |  |  |             return as_color(); | 
					
						
							|  |  |  |         return Color(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-18 04:54:07 +01:00
										 |  |  |     Color to_color(Color default_value) const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (type() == Type::Color) | 
					
						
							|  |  |  |             return as_color(); | 
					
						
							|  |  |  |         return default_value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |     String to_string() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-09 13:33:52 +01:00
										 |  |  |     bool operator==(const GVariant&) const; | 
					
						
							|  |  |  |     bool operator<(const GVariant&) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2019-04-12 14:43:44 +02:00
										 |  |  |     void copy_from(const GVariant&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 22:52:34 +02:00
										 |  |  |     struct RawPoint { | 
					
						
							|  |  |  |         int x; | 
					
						
							|  |  |  |         int y; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct RawSize { | 
					
						
							|  |  |  |         int width; | 
					
						
							|  |  |  |         int height; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct RawRect { | 
					
						
							|  |  |  |         RawPoint location; | 
					
						
							|  |  |  |         RawSize size; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |     union { | 
					
						
							|  |  |  |         StringImpl* as_string; | 
					
						
							|  |  |  |         GraphicsBitmap* as_bitmap; | 
					
						
							| 
									
										
										
										
											2019-03-24 04:28:36 +01:00
										 |  |  |         GIconImpl* as_icon; | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |         bool as_bool; | 
					
						
							|  |  |  |         int as_int; | 
					
						
							|  |  |  |         float as_float; | 
					
						
							| 
									
										
										
										
											2019-03-18 04:54:07 +01:00
										 |  |  |         RGBA32 as_color; | 
					
						
							| 
									
										
										
										
											2019-04-11 22:52:34 +02:00
										 |  |  |         RawPoint as_point; | 
					
						
							|  |  |  |         RawSize as_size; | 
					
						
							|  |  |  |         RawRect as_rect; | 
					
						
							| 
									
										
										
										
											2019-05-26 03:08:36 +01:00
										 |  |  |     } m_value; | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Type m_type { Type::Invalid }; | 
					
						
							|  |  |  | }; |