Variant.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/String.h>
  9. #include <LibGUI/Icon.h>
  10. #include <LibGfx/Bitmap.h>
  11. #include <LibGfx/Font.h>
  12. #include <LibGfx/SystemTheme.h>
  13. namespace GUI {
  14. class Variant {
  15. public:
  16. Variant();
  17. Variant(bool);
  18. Variant(float);
  19. Variant(i32);
  20. Variant(i64);
  21. Variant(u32);
  22. Variant(u64);
  23. Variant(const char*);
  24. Variant(StringView);
  25. Variant(const String&);
  26. Variant(const FlyString&);
  27. Variant(const Gfx::Bitmap&);
  28. Variant(const GUI::Icon&);
  29. Variant(const Gfx::IntPoint&);
  30. Variant(const Gfx::IntSize&);
  31. Variant(const Gfx::IntRect&);
  32. Variant(const Gfx::Font&);
  33. Variant(const Gfx::TextAlignment);
  34. Variant(const Gfx::ColorRole);
  35. Variant(const Gfx::AlignmentRole);
  36. Variant(const Gfx::FlagRole);
  37. Variant(const Gfx::MetricRole);
  38. Variant(const Gfx::PathRole);
  39. Variant(const JsonValue&);
  40. Variant(Color);
  41. Variant(const Variant&);
  42. Variant& operator=(const Variant&);
  43. Variant(Variant&&) = delete;
  44. Variant& operator=(Variant&&);
  45. void clear();
  46. ~Variant();
  47. enum class Type {
  48. Invalid,
  49. Bool,
  50. Int32,
  51. Int64,
  52. UnsignedInt32,
  53. UnsignedInt64,
  54. Float,
  55. String,
  56. Bitmap,
  57. Color,
  58. Icon,
  59. Point,
  60. Size,
  61. Rect,
  62. Font,
  63. TextAlignment,
  64. ColorRole,
  65. AlignmentRole,
  66. FlagRole,
  67. MetricRole,
  68. PathRole,
  69. };
  70. bool is_valid() const { return m_type != Type::Invalid; }
  71. bool is_bool() const { return m_type == Type::Bool; }
  72. bool is_i32() const { return m_type == Type::Int32; }
  73. bool is_i64() const { return m_type == Type::Int64; }
  74. bool is_u32() const { return m_type == Type::UnsignedInt32; }
  75. bool is_u64() const { return m_type == Type::UnsignedInt64; }
  76. bool is_float() const { return m_type == Type::Float; }
  77. bool is_string() const { return m_type == Type::String; }
  78. bool is_bitmap() const { return m_type == Type::Bitmap; }
  79. bool is_color() const { return m_type == Type::Color; }
  80. bool is_icon() const { return m_type == Type::Icon; }
  81. bool is_point() const { return m_type == Type::Point; }
  82. bool is_size() const { return m_type == Type::Size; }
  83. bool is_rect() const { return m_type == Type::Rect; }
  84. bool is_font() const { return m_type == Type::Font; }
  85. bool is_text_alignment() const { return m_type == Type::TextAlignment; }
  86. bool is_color_role() const { return m_type == Type::ColorRole; }
  87. bool is_alignment_role() const { return m_type == Type::AlignmentRole; }
  88. bool is_flag_role() const { return m_type == Type::FlagRole; }
  89. bool is_metric_role() const { return m_type == Type::MetricRole; }
  90. bool is_path_role() const { return m_type == Type::PathRole; }
  91. Type type() const { return m_type; }
  92. bool as_bool() const
  93. {
  94. VERIFY(type() == Type::Bool);
  95. return m_value.as_bool;
  96. }
  97. bool to_bool() const
  98. {
  99. if (type() == Type::Bool)
  100. return as_bool();
  101. if (type() == Type::String)
  102. return !!m_value.as_string;
  103. if (type() == Type::Int32)
  104. return m_value.as_i32 != 0;
  105. if (type() == Type::Int64)
  106. return m_value.as_i64 != 0;
  107. if (type() == Type::UnsignedInt32)
  108. return m_value.as_u32 != 0;
  109. if (type() == Type::UnsignedInt64)
  110. return m_value.as_u64 != 0;
  111. if (type() == Type::Rect)
  112. return !as_rect().is_null();
  113. if (type() == Type::Size)
  114. return !as_size().is_null();
  115. if (type() == Type::Point)
  116. return !as_point().is_null();
  117. return is_valid();
  118. }
  119. int as_i32() const
  120. {
  121. VERIFY(type() == Type::Int32);
  122. return m_value.as_i32;
  123. }
  124. int as_i64() const
  125. {
  126. VERIFY(type() == Type::Int64);
  127. return m_value.as_i64;
  128. }
  129. u32 as_u32() const
  130. {
  131. VERIFY(type() == Type::UnsignedInt32);
  132. return m_value.as_u32;
  133. }
  134. u64 as_u64() const
  135. {
  136. VERIFY(type() == Type::UnsignedInt64);
  137. return m_value.as_u64;
  138. }
  139. template<typename T>
  140. T to_integer() const
  141. {
  142. if (is_i32())
  143. return as_i32();
  144. if (is_i64())
  145. return as_i64();
  146. if (is_bool())
  147. return as_bool() ? 1 : 0;
  148. if (is_float())
  149. return (int)as_float();
  150. if (is_u32()) {
  151. VERIFY(as_u32() <= INT32_MAX);
  152. return static_cast<i32>(as_u32());
  153. }
  154. if (is_u64()) {
  155. VERIFY(as_u64() <= INT64_MAX);
  156. return static_cast<i64>(as_u64());
  157. }
  158. if (is_string())
  159. return as_string().to_int().value_or(0);
  160. return 0;
  161. }
  162. i32 to_i32() const
  163. {
  164. return to_integer<i32>();
  165. }
  166. i64 to_i64() const
  167. {
  168. return to_integer<i64>();
  169. }
  170. float as_float() const
  171. {
  172. VERIFY(type() == Type::Float);
  173. return m_value.as_float;
  174. }
  175. float as_float_or(float fallback) const
  176. {
  177. if (is_float())
  178. return as_float();
  179. return fallback;
  180. }
  181. Gfx::IntPoint as_point() const
  182. {
  183. return { m_value.as_point.x, m_value.as_point.y };
  184. }
  185. Gfx::IntSize as_size() const
  186. {
  187. return { m_value.as_size.width, m_value.as_size.height };
  188. }
  189. Gfx::IntRect as_rect() const
  190. {
  191. return { as_point(), as_size() };
  192. }
  193. String as_string() const
  194. {
  195. VERIFY(type() == Type::String);
  196. return m_value.as_string;
  197. }
  198. const Gfx::Bitmap& as_bitmap() const
  199. {
  200. VERIFY(type() == Type::Bitmap);
  201. return *m_value.as_bitmap;
  202. }
  203. GUI::Icon as_icon() const
  204. {
  205. VERIFY(type() == Type::Icon);
  206. return GUI::Icon(*m_value.as_icon);
  207. }
  208. Color as_color() const
  209. {
  210. VERIFY(type() == Type::Color);
  211. return Color::from_argb(m_value.as_color);
  212. }
  213. const Gfx::Font& as_font() const
  214. {
  215. VERIFY(type() == Type::Font);
  216. return *m_value.as_font;
  217. }
  218. Gfx::TextAlignment to_text_alignment(Gfx::TextAlignment default_value) const
  219. {
  220. if (type() != Type::TextAlignment)
  221. return default_value;
  222. return m_value.as_text_alignment;
  223. }
  224. Gfx::ColorRole to_color_role() const
  225. {
  226. if (type() != Type::ColorRole)
  227. return Gfx::ColorRole::NoRole;
  228. return m_value.as_color_role;
  229. }
  230. Gfx::AlignmentRole to_alignment_role() const
  231. {
  232. if (type() != Type::AlignmentRole)
  233. return Gfx::AlignmentRole::NoRole;
  234. return m_value.as_alignment_role;
  235. }
  236. Gfx::FlagRole to_flag_role() const
  237. {
  238. if (type() != Type::FlagRole)
  239. return Gfx::FlagRole::NoRole;
  240. return m_value.as_flag_role;
  241. }
  242. Gfx::MetricRole to_metric_role() const
  243. {
  244. if (type() != Type::MetricRole)
  245. return Gfx::MetricRole::NoRole;
  246. return m_value.as_metric_role;
  247. }
  248. Gfx::PathRole to_path_role() const
  249. {
  250. if (type() != Type::PathRole)
  251. return Gfx::PathRole::NoRole;
  252. return m_value.as_path_role;
  253. }
  254. Color to_color(Color default_value = {}) const
  255. {
  256. if (type() == Type::Color)
  257. return as_color();
  258. if (type() == Type::String) {
  259. auto color = Color::from_string(as_string());
  260. if (color.has_value())
  261. return color.value();
  262. }
  263. return default_value;
  264. }
  265. String to_string() const;
  266. bool operator==(const Variant&) const;
  267. bool operator<(const Variant&) const;
  268. private:
  269. void copy_from(const Variant&);
  270. void move_from(Variant&&);
  271. struct RawPoint {
  272. int x;
  273. int y;
  274. };
  275. struct RawSize {
  276. int width;
  277. int height;
  278. };
  279. struct RawRect {
  280. RawPoint location;
  281. RawSize size;
  282. };
  283. union {
  284. StringImpl* as_string;
  285. Gfx::Bitmap* as_bitmap;
  286. GUI::IconImpl* as_icon;
  287. Gfx::Font* as_font;
  288. bool as_bool;
  289. i32 as_i32;
  290. i64 as_i64;
  291. u32 as_u32;
  292. u64 as_u64;
  293. float as_float;
  294. Gfx::ARGB32 as_color;
  295. Gfx::TextAlignment as_text_alignment;
  296. Gfx::ColorRole as_color_role;
  297. Gfx::AlignmentRole as_alignment_role;
  298. Gfx::FlagRole as_flag_role;
  299. Gfx::MetricRole as_metric_role;
  300. Gfx::PathRole as_path_role;
  301. RawPoint as_point;
  302. RawSize as_size;
  303. RawRect as_rect;
  304. } m_value;
  305. Type m_type { Type::Invalid };
  306. };
  307. const char* to_string(Variant::Type);
  308. }