Variant.h 6.7 KB

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