| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-29 12:06:26 +02:00
										 |  |  | #include <AK/JsonValue.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 20:33:02 +01:00
										 |  |  | #include <LibGUI/Variant.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | namespace GUI { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const char* to_string(Variant::Type type) | 
					
						
							| 
									
										
										
										
											2019-06-23 07:54:46 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (type) { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     case Variant::Type::Invalid: | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |         return "Invalid"; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     case Variant::Type::Bool: | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |         return "Bool"; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     case Variant::Type::Int32: | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |         return "Int32"; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     case Variant::Type::Int64: | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |         return "Int64"; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     case Variant::Type::UnsignedInt: | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |         return "UnsignedInt"; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     case Variant::Type::Float: | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |         return "Float"; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     case Variant::Type::String: | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |         return "String"; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     case Variant::Type::Bitmap: | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |         return "Bitmap"; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     case Variant::Type::Color: | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |         return "Color"; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     case Variant::Type::Icon: | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |         return "Icon"; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     case Variant::Type::Point: | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |         return "Point"; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     case Variant::Type::Size: | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |         return "Size"; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     case Variant::Type::Rect: | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |         return "Rect"; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     case Variant::Type::Font: | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |         return "Font"; | 
					
						
							| 
									
										
										
										
											2019-06-23 07:54:46 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     ASSERT_NOT_REACHED(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Variant::Variant() | 
					
						
							| 
									
										
										
										
											2019-03-18 04:54:07 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-11 17:28:59 +02:00
										 |  |  |     m_value.as_string = nullptr; | 
					
						
							| 
									
										
										
										
											2019-03-18 04:54:07 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Variant::~Variant() | 
					
						
							| 
									
										
										
										
											2019-04-12 14:43:44 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     clear(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Variant::clear() | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (m_type) { | 
					
						
							|  |  |  |     case Type::String: | 
					
						
							| 
									
										
										
										
											2020-01-23 15:14:21 +01:00
										 |  |  |         AK::unref_if_not_null(m_value.as_string); | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case Type::Bitmap: | 
					
						
							| 
									
										
										
										
											2020-01-23 15:14:21 +01:00
										 |  |  |         AK::unref_if_not_null(m_value.as_bitmap); | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-03-24 04:28:36 +01:00
										 |  |  |     case Type::Icon: | 
					
						
							| 
									
										
										
										
											2020-01-23 15:14:21 +01:00
										 |  |  |         AK::unref_if_not_null(m_value.as_icon); | 
					
						
							| 
									
										
										
										
											2019-03-24 04:28:36 +01:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |     default: | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-12 14:43:44 +02:00
										 |  |  |     m_type = Type::Invalid; | 
					
						
							|  |  |  |     m_value.as_string = nullptr; | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Variant::Variant(i32 value) | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |     : m_type(Type::Int32) | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |     m_value.as_i32 = value; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Variant::Variant(i64 value) | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |     : m_type(Type::Int64) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_value.as_i64 = value; | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Variant::Variant(unsigned value) | 
					
						
							| 
									
										
										
										
											2019-07-31 07:07:59 +02:00
										 |  |  |     : m_type(Type::UnsignedInt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_value.as_uint = value; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Variant::Variant(float value) | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |     : m_type(Type::Float) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_value.as_float = value; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Variant::Variant(bool value) | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |     : m_type(Type::Bool) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_value.as_bool = value; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Variant::Variant(const char* cstring) | 
					
						
							|  |  |  |     : Variant(String(cstring)) | 
					
						
							| 
									
										
										
										
											2019-06-22 10:37:44 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Variant::Variant(const String& value) | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |     : m_type(Type::String) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_value.as_string = const_cast<StringImpl*>(value.impl()); | 
					
						
							| 
									
										
										
										
											2019-06-21 15:29:31 +02:00
										 |  |  |     AK::ref_if_not_null(m_value.as_string); | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Variant::Variant(const JsonValue& value) | 
					
						
							| 
									
										
										
										
											2019-06-29 12:06:26 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (value.is_null()) { | 
					
						
							|  |  |  |         m_value.as_string = nullptr; | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-29 16:36:50 +01:00
										 |  |  |     if (value.is_i32()) { | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |         m_type = Type::Int32; | 
					
						
							|  |  |  |         m_value.as_i32 = value.as_i32(); | 
					
						
							| 
									
										
										
										
											2019-06-29 12:06:26 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-29 16:36:50 +01:00
										 |  |  |     if (value.is_u32()) { | 
					
						
							| 
									
										
										
										
											2019-07-31 07:07:59 +02:00
										 |  |  |         m_type = Type::UnsignedInt; | 
					
						
							| 
									
										
										
										
											2019-10-29 16:36:50 +01:00
										 |  |  |         m_value.as_uint = value.as_u32(); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (value.is_i64()) { | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |         m_type = Type::Int64; | 
					
						
							|  |  |  |         m_value.as_i64 = value.as_i64(); | 
					
						
							| 
									
										
										
										
											2019-10-29 16:36:50 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (value.is_u64()) { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         // FIXME: Variant should have a 64-bit internal type.
 | 
					
						
							| 
									
										
										
										
											2019-10-29 16:36:50 +01:00
										 |  |  |         m_type = Type::UnsignedInt; | 
					
						
							|  |  |  |         m_value.as_uint = value.to_u32(); | 
					
						
							| 
									
										
										
										
											2019-06-29 12:06:26 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (value.is_string()) { | 
					
						
							|  |  |  |         m_type = Type::String; | 
					
						
							|  |  |  |         m_value.as_string = value.as_string().impl(); | 
					
						
							|  |  |  |         m_value.as_string->ref(); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (value.is_bool()) { | 
					
						
							|  |  |  |         m_type = Type::Bool; | 
					
						
							|  |  |  |         m_value.as_bool = value.as_bool(); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_NOT_REACHED(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | Variant::Variant(const Gfx::Bitmap& value) | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |     : m_type(Type::Bitmap) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |     m_value.as_bitmap = const_cast<Gfx::Bitmap*>(&value); | 
					
						
							| 
									
										
										
										
											2019-06-21 15:29:31 +02:00
										 |  |  |     AK::ref_if_not_null(m_value.as_bitmap); | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 12:02:21 +13:00
										 |  |  | Variant::Variant(const GUI::Icon& value) | 
					
						
							| 
									
										
										
										
											2019-03-24 04:28:36 +01:00
										 |  |  |     : m_type(Type::Icon) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-07 12:02:21 +13:00
										 |  |  |     m_value.as_icon = &const_cast<GUI::IconImpl&>(value.impl()); | 
					
						
							| 
									
										
										
										
											2019-06-21 15:29:31 +02:00
										 |  |  |     AK::ref_if_not_null(m_value.as_icon); | 
					
						
							| 
									
										
										
										
											2019-03-24 04:28:36 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | Variant::Variant(const Gfx::Font& value) | 
					
						
							| 
									
										
										
										
											2019-10-22 21:37:11 +02:00
										 |  |  |     : m_type(Type::Font) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |     m_value.as_font = &const_cast<Gfx::Font&>(value); | 
					
						
							| 
									
										
										
										
											2019-10-22 21:37:11 +02:00
										 |  |  |     AK::ref_if_not_null(m_value.as_font); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Variant::Variant(Color color) | 
					
						
							| 
									
										
										
										
											2019-03-18 04:54:07 +01:00
										 |  |  |     : m_type(Type::Color) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_value.as_color = color.value(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | Variant::Variant(const Gfx::Point& point) | 
					
						
							| 
									
										
										
										
											2019-04-11 22:52:34 +02:00
										 |  |  |     : m_type(Type::Point) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_value.as_point = { point.x(), point.y() }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | Variant::Variant(const Gfx::Size& size) | 
					
						
							| 
									
										
										
										
											2019-04-11 22:52:34 +02:00
										 |  |  |     : m_type(Type::Size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_value.as_size = { size.width(), size.height() }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | Variant::Variant(const Gfx::Rect& rect) | 
					
						
							| 
									
										
										
										
											2019-04-11 22:52:34 +02:00
										 |  |  |     : m_type(Type::Rect) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_value.as_rect = (const RawRect&)rect; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Variant& Variant::operator=(const Variant& other) | 
					
						
							| 
									
										
										
										
											2019-04-12 14:43:44 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (&other == this) | 
					
						
							|  |  |  |         return *this; | 
					
						
							|  |  |  |     clear(); | 
					
						
							|  |  |  |     copy_from(other); | 
					
						
							|  |  |  |     return *this; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Variant& Variant::operator=(Variant&& other) | 
					
						
							| 
									
										
										
										
											2019-04-19 01:05:59 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (&other == this) | 
					
						
							|  |  |  |         return *this; | 
					
						
							|  |  |  |     // FIXME: Move, not copy!
 | 
					
						
							|  |  |  |     clear(); | 
					
						
							|  |  |  |     copy_from(other); | 
					
						
							|  |  |  |     other.clear(); | 
					
						
							|  |  |  |     return *this; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Variant::Variant(const Variant& other) | 
					
						
							| 
									
										
										
										
											2019-04-11 21:36:04 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-12 14:43:44 +02:00
										 |  |  |     copy_from(other); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Variant::copy_from(const Variant& other) | 
					
						
							| 
									
										
										
										
											2019-04-12 14:43:44 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     ASSERT(!is_valid()); | 
					
						
							|  |  |  |     m_type = other.m_type; | 
					
						
							| 
									
										
										
										
											2019-04-11 21:36:04 +02:00
										 |  |  |     switch (m_type) { | 
					
						
							|  |  |  |     case Type::Bool: | 
					
						
							|  |  |  |         m_value.as_bool = other.m_value.as_bool; | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |     case Type::Int32: | 
					
						
							|  |  |  |         m_value.as_i32 = other.m_value.as_i32; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case Type::Int64: | 
					
						
							|  |  |  |         m_value.as_i64 = other.m_value.as_i64; | 
					
						
							| 
									
										
										
										
											2019-04-11 21:36:04 +02:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-07-31 07:07:59 +02:00
										 |  |  |     case Type::UnsignedInt: | 
					
						
							|  |  |  |         m_value.as_uint = other.m_value.as_uint; | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-04-11 21:36:04 +02:00
										 |  |  |     case Type::Float: | 
					
						
							|  |  |  |         m_value.as_float = other.m_value.as_float; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case Type::String: | 
					
						
							|  |  |  |         m_value.as_string = other.m_value.as_string; | 
					
						
							| 
									
										
										
										
											2019-06-21 15:29:31 +02:00
										 |  |  |         AK::ref_if_not_null(m_value.as_bitmap); | 
					
						
							| 
									
										
										
										
											2019-04-11 21:36:04 +02:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case Type::Bitmap: | 
					
						
							|  |  |  |         m_value.as_bitmap = other.m_value.as_bitmap; | 
					
						
							| 
									
										
										
										
											2019-06-21 15:29:31 +02:00
										 |  |  |         AK::ref_if_not_null(m_value.as_bitmap); | 
					
						
							| 
									
										
										
										
											2019-04-11 21:36:04 +02:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case Type::Icon: | 
					
						
							|  |  |  |         m_value.as_icon = other.m_value.as_icon; | 
					
						
							| 
									
										
										
										
											2019-06-21 15:29:31 +02:00
										 |  |  |         AK::ref_if_not_null(m_value.as_icon); | 
					
						
							| 
									
										
										
										
											2019-04-11 21:36:04 +02:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-10-22 21:37:11 +02:00
										 |  |  |     case Type::Font: | 
					
						
							|  |  |  |         m_value.as_font = other.m_value.as_font; | 
					
						
							|  |  |  |         AK::ref_if_not_null(m_value.as_font); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-04-11 21:36:04 +02:00
										 |  |  |     case Type::Color: | 
					
						
							|  |  |  |         m_value.as_color = other.m_value.as_color; | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-04-11 22:52:34 +02:00
										 |  |  |     case Type::Point: | 
					
						
							|  |  |  |         m_value.as_point = other.m_value.as_point; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case Type::Size: | 
					
						
							|  |  |  |         m_value.as_size = other.m_value.as_size; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case Type::Rect: | 
					
						
							|  |  |  |         m_value.as_rect = other.m_value.as_rect; | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-04-11 21:36:04 +02:00
										 |  |  |     case Type::Invalid: | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | bool Variant::operator==(const Variant& other) const | 
					
						
							| 
									
										
										
										
											2019-03-09 13:33:52 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_type != other.m_type) | 
					
						
							|  |  |  |         return to_string() == other.to_string(); | 
					
						
							|  |  |  |     switch (m_type) { | 
					
						
							|  |  |  |     case Type::Bool: | 
					
						
							|  |  |  |         return as_bool() == other.as_bool(); | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |     case Type::Int32: | 
					
						
							|  |  |  |         return as_i32() == other.as_i32(); | 
					
						
							|  |  |  |     case Type::Int64: | 
					
						
							|  |  |  |         return as_i64() == other.as_i64(); | 
					
						
							| 
									
										
										
										
											2019-07-31 07:07:59 +02:00
										 |  |  |     case Type::UnsignedInt: | 
					
						
							|  |  |  |         return as_uint() == other.as_uint(); | 
					
						
							| 
									
										
										
										
											2019-03-09 13:33:52 +01:00
										 |  |  |     case Type::Float: | 
					
						
							|  |  |  |         return as_float() == other.as_float(); | 
					
						
							|  |  |  |     case Type::String: | 
					
						
							|  |  |  |         return as_string() == other.as_string(); | 
					
						
							|  |  |  |     case Type::Bitmap: | 
					
						
							|  |  |  |         return m_value.as_bitmap == other.m_value.as_bitmap; | 
					
						
							| 
									
										
										
										
											2019-03-24 04:28:36 +01:00
										 |  |  |     case Type::Icon: | 
					
						
							|  |  |  |         return m_value.as_icon == other.m_value.as_icon; | 
					
						
							| 
									
										
										
										
											2019-03-18 04:54:07 +01:00
										 |  |  |     case Type::Color: | 
					
						
							|  |  |  |         return m_value.as_color == other.m_value.as_color; | 
					
						
							| 
									
										
										
										
											2019-04-11 22:52:34 +02:00
										 |  |  |     case Type::Point: | 
					
						
							|  |  |  |         return as_point() == other.as_point(); | 
					
						
							|  |  |  |     case Type::Size: | 
					
						
							|  |  |  |         return as_size() == other.as_size(); | 
					
						
							|  |  |  |     case Type::Rect: | 
					
						
							|  |  |  |         return as_rect() == other.as_rect(); | 
					
						
							| 
									
										
										
										
											2019-10-22 21:37:11 +02:00
										 |  |  |     case Type::Font: | 
					
						
							|  |  |  |         return &as_font() == &other.as_font(); | 
					
						
							| 
									
										
										
										
											2019-03-09 13:33:52 +01:00
										 |  |  |     case Type::Invalid: | 
					
						
							| 
									
										
										
										
											2019-06-29 12:06:26 +02:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2019-03-09 13:33:52 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     ASSERT_NOT_REACHED(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | bool Variant::operator<(const Variant& other) const | 
					
						
							| 
									
										
										
										
											2019-03-09 13:33:52 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_type != other.m_type) | 
					
						
							|  |  |  |         return to_string() < other.to_string(); | 
					
						
							|  |  |  |     switch (m_type) { | 
					
						
							|  |  |  |     case Type::Bool: | 
					
						
							|  |  |  |         return as_bool() < other.as_bool(); | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |     case Type::Int32: | 
					
						
							|  |  |  |         return as_i32() < other.as_i32(); | 
					
						
							|  |  |  |     case Type::Int64: | 
					
						
							|  |  |  |         return as_i64() < other.as_i64(); | 
					
						
							| 
									
										
										
										
											2019-07-31 07:07:59 +02:00
										 |  |  |     case Type::UnsignedInt: | 
					
						
							|  |  |  |         return as_uint() < other.as_uint(); | 
					
						
							| 
									
										
										
										
											2019-03-09 13:33:52 +01:00
										 |  |  |     case Type::Float: | 
					
						
							|  |  |  |         return as_float() < other.as_float(); | 
					
						
							|  |  |  |     case Type::String: | 
					
						
							|  |  |  |         return as_string() < other.as_string(); | 
					
						
							|  |  |  |     case Type::Bitmap: | 
					
						
							|  |  |  |         // FIXME: Maybe compare bitmaps somehow differently?
 | 
					
						
							|  |  |  |         return m_value.as_bitmap < other.m_value.as_bitmap; | 
					
						
							| 
									
										
										
										
											2019-03-24 04:28:36 +01:00
										 |  |  |     case Type::Icon: | 
					
						
							|  |  |  |         // FIXME: Maybe compare icons somehow differently?
 | 
					
						
							|  |  |  |         return m_value.as_icon < other.m_value.as_icon; | 
					
						
							| 
									
										
										
										
											2019-03-18 04:54:07 +01:00
										 |  |  |     case Type::Color: | 
					
						
							|  |  |  |         return m_value.as_color < other.m_value.as_color; | 
					
						
							| 
									
										
										
										
											2019-04-11 22:52:34 +02:00
										 |  |  |     case Type::Point: | 
					
						
							|  |  |  |     case Type::Size: | 
					
						
							|  |  |  |     case Type::Rect: | 
					
						
							| 
									
										
										
										
											2019-10-22 21:37:11 +02:00
										 |  |  |     case Type::Font: | 
					
						
							| 
									
										
										
										
											2019-04-11 22:52:34 +02:00
										 |  |  |         // FIXME: Figure out how to compare these.
 | 
					
						
							|  |  |  |         ASSERT_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2019-03-09 13:33:52 +01:00
										 |  |  |     case Type::Invalid: | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     ASSERT_NOT_REACHED(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | String Variant::to_string() const | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (m_type) { | 
					
						
							|  |  |  |     case Type::Bool: | 
					
						
							| 
									
										
										
										
											2019-04-12 14:49:45 +02:00
										 |  |  |         return as_bool() ? "true" : "false"; | 
					
						
							| 
									
										
										
										
											2020-01-27 10:55:10 +01:00
										 |  |  |     case Type::Int32: | 
					
						
							|  |  |  |         return String::number(as_i32()); | 
					
						
							|  |  |  |     case Type::Int64: | 
					
						
							|  |  |  |         return String::number(as_i64()); | 
					
						
							| 
									
										
										
										
											2019-07-31 07:07:59 +02:00
										 |  |  |     case Type::UnsignedInt: | 
					
						
							|  |  |  |         return String::number(as_uint()); | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |     case Type::Float: | 
					
						
							| 
									
										
										
										
											2019-03-06 19:49:59 +01:00
										 |  |  |         return String::format("%f", (double)as_float()); | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |     case Type::String: | 
					
						
							|  |  |  |         return as_string(); | 
					
						
							|  |  |  |     case Type::Bitmap: | 
					
						
							| 
									
										
										
										
											2020-02-06 12:07:05 +01:00
										 |  |  |         return "[Gfx::Bitmap]"; | 
					
						
							| 
									
										
										
										
											2019-03-24 04:28:36 +01:00
										 |  |  |     case Type::Icon: | 
					
						
							| 
									
										
										
										
											2020-03-07 12:02:21 +13:00
										 |  |  |         return "[GUI::Icon]"; | 
					
						
							| 
									
										
										
										
											2019-03-18 04:54:07 +01:00
										 |  |  |     case Type::Color: | 
					
						
							|  |  |  |         return as_color().to_string(); | 
					
						
							| 
									
										
										
										
											2019-04-11 22:52:34 +02:00
										 |  |  |     case Type::Point: | 
					
						
							|  |  |  |         return as_point().to_string(); | 
					
						
							|  |  |  |     case Type::Size: | 
					
						
							|  |  |  |         return as_size().to_string(); | 
					
						
							|  |  |  |     case Type::Rect: | 
					
						
							|  |  |  |         return as_rect().to_string(); | 
					
						
							| 
									
										
										
										
											2019-10-22 21:37:11 +02:00
										 |  |  |     case Type::Font: | 
					
						
							|  |  |  |         return String::format("[Font: %s]", as_font().name().characters()); | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |     case Type::Invalid: | 
					
						
							| 
									
										
										
										
											2019-04-12 14:49:45 +02:00
										 |  |  |         return "[null]"; | 
					
						
							| 
									
										
										
										
											2019-02-28 16:20:29 +01:00
										 |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     ASSERT_NOT_REACHED(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |