Variant.h 8.2 KB

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