Variant.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/String.h>
  28. #include <LibGfx/Font.h>
  29. #include <LibGfx/Bitmap.h>
  30. #include <LibGUI/Icon.h>
  31. namespace GUI {
  32. class Variant {
  33. public:
  34. Variant();
  35. Variant(bool);
  36. Variant(float);
  37. Variant(i32);
  38. Variant(i64);
  39. Variant(unsigned);
  40. Variant(const char*);
  41. Variant(const String&);
  42. Variant(const FlyString&);
  43. Variant(const Gfx::Bitmap&);
  44. Variant(const GUI::Icon&);
  45. Variant(const Gfx::IntPoint&);
  46. Variant(const Gfx::IntSize&);
  47. Variant(const Gfx::IntRect&);
  48. Variant(const Gfx::Font&);
  49. Variant(const Gfx::TextAlignment);
  50. Variant(const AK::JsonValue&);
  51. Variant(Color);
  52. Variant(const Variant&);
  53. Variant& operator=(const Variant&);
  54. Variant(Variant&&) = delete;
  55. Variant& operator=(Variant&&);
  56. void clear();
  57. ~Variant();
  58. enum class Type {
  59. Invalid,
  60. Bool,
  61. Int32,
  62. Int64,
  63. UnsignedInt,
  64. Float,
  65. String,
  66. Bitmap,
  67. Color,
  68. Icon,
  69. Point,
  70. Size,
  71. Rect,
  72. Font,
  73. TextAlignment,
  74. };
  75. bool is_valid() const { return m_type != Type::Invalid; }
  76. bool is_bool() const { return m_type == Type::Bool; }
  77. bool is_i32() const { return m_type == Type::Int32; }
  78. bool is_i64() const { return m_type == Type::Int64; }
  79. bool is_uint() const { return m_type == Type::UnsignedInt; }
  80. bool is_float() const { return m_type == Type::Float; }
  81. bool is_string() const { return m_type == Type::String; }
  82. bool is_bitmap() const { return m_type == Type::Bitmap; }
  83. bool is_color() const { return m_type == Type::Color; }
  84. bool is_icon() const { return m_type == Type::Icon; }
  85. bool is_point() const { return m_type == Type::Point; }
  86. bool is_size() const { return m_type == Type::Size; }
  87. bool is_rect() const { return m_type == Type::Rect; }
  88. bool is_font() const { return m_type == Type::Font; }
  89. bool is_text_alignment() const { return m_type == Type::TextAlignment; }
  90. Type type() const { return m_type; }
  91. bool as_bool() const
  92. {
  93. ASSERT(type() == Type::Bool);
  94. return m_value.as_bool;
  95. }
  96. bool to_bool() const
  97. {
  98. if (type() == Type::Bool)
  99. return as_bool();
  100. if (type() == Type::String)
  101. return !!m_value.as_string;
  102. if (type() == Type::Int32)
  103. return m_value.as_i32 != 0;
  104. if (type() == Type::Int64)
  105. return m_value.as_i64 != 0;
  106. if (type() == Type::UnsignedInt)
  107. return m_value.as_uint != 0;
  108. if (type() == Type::Rect)
  109. return !as_rect().is_null();
  110. if (type() == Type::Size)
  111. return !as_size().is_null();
  112. if (type() == Type::Point)
  113. return !as_point().is_null();
  114. return is_valid();
  115. }
  116. int as_i32() const
  117. {
  118. ASSERT(type() == Type::Int32);
  119. return m_value.as_i32;
  120. }
  121. int as_i64() const
  122. {
  123. ASSERT(type() == Type::Int64);
  124. return m_value.as_i64;
  125. }
  126. unsigned as_uint() const
  127. {
  128. ASSERT(type() == Type::UnsignedInt);
  129. return m_value.as_uint;
  130. }
  131. template<typename T>
  132. T to_integer() const
  133. {
  134. if (is_i32())
  135. return as_i32();
  136. if (is_i64())
  137. return as_i64();
  138. if (is_bool())
  139. return as_bool() ? 1 : 0;
  140. if (is_float())
  141. return (int)as_float();
  142. if (is_uint()) {
  143. ASSERT(as_uint() <= INT32_MAX);
  144. return (int)as_uint();
  145. }
  146. if (is_string())
  147. return as_string().to_int().value_or(0);
  148. return 0;
  149. }
  150. i32 to_i32() const
  151. {
  152. return to_integer<i32>();
  153. }
  154. i64 to_i64() const
  155. {
  156. return to_integer<i64>();
  157. }
  158. float as_float() const
  159. {
  160. ASSERT(type() == Type::Float);
  161. return m_value.as_float;
  162. }
  163. Gfx::IntPoint as_point() const
  164. {
  165. return { m_value.as_point.x, m_value.as_point.y };
  166. }
  167. Gfx::IntSize as_size() const
  168. {
  169. return { m_value.as_size.width, m_value.as_size.height };
  170. }
  171. Gfx::IntRect as_rect() const
  172. {
  173. return { as_point(), as_size() };
  174. }
  175. String as_string() const
  176. {
  177. ASSERT(type() == Type::String);
  178. return m_value.as_string;
  179. }
  180. const Gfx::Bitmap& as_bitmap() const
  181. {
  182. ASSERT(type() == Type::Bitmap);
  183. return *m_value.as_bitmap;
  184. }
  185. GUI::Icon as_icon() const
  186. {
  187. ASSERT(type() == Type::Icon);
  188. return GUI::Icon(*m_value.as_icon);
  189. }
  190. Color as_color() const
  191. {
  192. ASSERT(type() == Type::Color);
  193. return Color::from_rgba(m_value.as_color);
  194. }
  195. const Gfx::Font& as_font() const
  196. {
  197. ASSERT(type() == Type::Font);
  198. return *m_value.as_font;
  199. }
  200. Gfx::TextAlignment to_text_alignment(Gfx::TextAlignment default_value) const
  201. {
  202. if (type() != Type::TextAlignment)
  203. return default_value;
  204. return m_value.as_text_alignment;
  205. }
  206. Color to_color(Color default_value = {}) const
  207. {
  208. if (type() == Type::Color)
  209. return as_color();
  210. if (type() == Type::String) {
  211. auto color = Color::from_string(as_string());
  212. if (color.has_value())
  213. return color.value();
  214. }
  215. return default_value;
  216. }
  217. String to_string() const;
  218. bool operator==(const Variant&) const;
  219. bool operator<(const Variant&) const;
  220. private:
  221. void copy_from(const Variant&);
  222. struct RawPoint {
  223. int x;
  224. int y;
  225. };
  226. struct RawSize {
  227. int width;
  228. int height;
  229. };
  230. struct RawRect {
  231. RawPoint location;
  232. RawSize size;
  233. };
  234. union {
  235. StringImpl* as_string;
  236. Gfx::Bitmap* as_bitmap;
  237. GUI::IconImpl* as_icon;
  238. Gfx::Font* as_font;
  239. bool as_bool;
  240. i32 as_i32;
  241. i64 as_i64;
  242. unsigned as_uint;
  243. float as_float;
  244. Gfx::RGBA32 as_color;
  245. Gfx::TextAlignment as_text_alignment;
  246. RawPoint as_point;
  247. RawSize as_size;
  248. RawRect as_rect;
  249. } m_value;
  250. Type m_type { Type::Invalid };
  251. };
  252. const char* to_string(Variant::Type);
  253. }