Variant.h 7.2 KB

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