Variant.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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::Point&);
  46. Variant(const Gfx::Size&);
  47. Variant(const Gfx::Rect&);
  48. Variant(const Gfx::Font&);
  49. Variant(const AK::JsonValue&);
  50. Variant(Color);
  51. Variant(const Variant&);
  52. Variant& operator=(const Variant&);
  53. Variant(Variant&&) = delete;
  54. Variant& operator=(Variant&&);
  55. void clear();
  56. ~Variant();
  57. enum class Type {
  58. Invalid,
  59. Bool,
  60. Int32,
  61. Int64,
  62. UnsignedInt,
  63. Float,
  64. String,
  65. Bitmap,
  66. Color,
  67. Icon,
  68. Point,
  69. Size,
  70. Rect,
  71. Font,
  72. };
  73. bool is_valid() const { return m_type != Type::Invalid; }
  74. bool is_bool() const { return m_type == Type::Bool; }
  75. bool is_i32() const { return m_type == Type::Int32; }
  76. bool is_i64() const { return m_type == Type::Int64; }
  77. bool is_uint() const { return m_type == Type::UnsignedInt; }
  78. bool is_float() const { return m_type == Type::Float; }
  79. bool is_string() const { return m_type == Type::String; }
  80. bool is_bitmap() const { return m_type == Type::Bitmap; }
  81. bool is_color() const { return m_type == Type::Color; }
  82. bool is_icon() const { return m_type == Type::Icon; }
  83. bool is_point() const { return m_type == Type::Point; }
  84. bool is_size() const { return m_type == Type::Size; }
  85. bool is_rect() const { return m_type == Type::Rect; }
  86. bool is_font() const { return m_type == Type::Font; }
  87. Type type() const { return m_type; }
  88. bool as_bool() const
  89. {
  90. ASSERT(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::UnsignedInt)
  104. return m_value.as_uint != 0;
  105. if (type() == Type::Rect)
  106. return !as_rect().is_null();
  107. if (type() == Type::Size)
  108. return !as_size().is_null();
  109. if (type() == Type::Point)
  110. return !as_point().is_null();
  111. return is_valid();
  112. }
  113. int as_i32() const
  114. {
  115. ASSERT(type() == Type::Int32);
  116. return m_value.as_i32;
  117. }
  118. int as_i64() const
  119. {
  120. ASSERT(type() == Type::Int64);
  121. return m_value.as_i64;
  122. }
  123. unsigned as_uint() const
  124. {
  125. ASSERT(type() == Type::UnsignedInt);
  126. return m_value.as_uint;
  127. }
  128. template<typename T>
  129. T to_integer() const
  130. {
  131. if (is_i32())
  132. return as_i32();
  133. if (is_i64())
  134. return as_i64();
  135. if (is_bool())
  136. return as_bool() ? 1 : 0;
  137. if (is_float())
  138. return (int)as_float();
  139. if (is_uint()) {
  140. ASSERT(as_uint() <= INT32_MAX);
  141. return (int)as_uint();
  142. }
  143. if (is_string()) {
  144. bool ok;
  145. int value = as_string().to_int(ok);
  146. if (!ok)
  147. return 0;
  148. return value;
  149. }
  150. return 0;
  151. }
  152. i32 to_i32() const
  153. {
  154. return to_integer<i32>();
  155. }
  156. i64 to_i64() const
  157. {
  158. return to_integer<i64>();
  159. }
  160. float as_float() const
  161. {
  162. ASSERT(type() == Type::Float);
  163. return m_value.as_float;
  164. }
  165. Gfx::Point as_point() const
  166. {
  167. return { m_value.as_point.x, m_value.as_point.y };
  168. }
  169. Gfx::Size as_size() const
  170. {
  171. return { m_value.as_size.width, m_value.as_size.height };
  172. }
  173. Gfx::Rect as_rect() const
  174. {
  175. return { as_point(), as_size() };
  176. }
  177. String as_string() const
  178. {
  179. ASSERT(type() == Type::String);
  180. return m_value.as_string;
  181. }
  182. const Gfx::Bitmap& as_bitmap() const
  183. {
  184. ASSERT(type() == Type::Bitmap);
  185. return *m_value.as_bitmap;
  186. }
  187. GUI::Icon as_icon() const
  188. {
  189. ASSERT(type() == Type::Icon);
  190. return GUI::Icon(*m_value.as_icon);
  191. }
  192. Color as_color() const
  193. {
  194. ASSERT(type() == Type::Color);
  195. return Color::from_rgba(m_value.as_color);
  196. }
  197. const Gfx::Font& as_font() const
  198. {
  199. ASSERT(type() == Type::Font);
  200. return *m_value.as_font;
  201. }
  202. Color to_color(Color default_value = {}) const
  203. {
  204. if (type() == Type::Color)
  205. return as_color();
  206. if (type() == Type::String) {
  207. auto color = Color::from_string(as_string());
  208. if (color.has_value())
  209. return color.value();
  210. }
  211. return default_value;
  212. }
  213. String to_string() const;
  214. bool operator==(const Variant&) const;
  215. bool operator<(const Variant&) const;
  216. private:
  217. void copy_from(const 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. unsigned as_uint;
  239. float as_float;
  240. Gfx::RGBA32 as_color;
  241. RawPoint as_point;
  242. RawSize as_size;
  243. RawRect as_rect;
  244. } m_value;
  245. Type m_type { Type::Invalid };
  246. };
  247. const char* to_string(Variant::Type);
  248. }