Variant.h 7.8 KB

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