Variant.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 AK {
  32. class JsonValue;
  33. }
  34. namespace GUI {
  35. class Variant {
  36. public:
  37. Variant();
  38. Variant(bool);
  39. Variant(float);
  40. Variant(i32);
  41. Variant(i64);
  42. Variant(unsigned);
  43. Variant(const char*);
  44. Variant(const String&);
  45. Variant(const Gfx::Bitmap&);
  46. Variant(const GIcon&);
  47. Variant(const Gfx::Point&);
  48. Variant(const Gfx::Size&);
  49. Variant(const Gfx::Rect&);
  50. Variant(const Gfx::Font&);
  51. Variant(const AK::JsonValue&);
  52. Variant(Color);
  53. Variant(const Variant&);
  54. Variant& operator=(const Variant&);
  55. Variant(Variant&&) = delete;
  56. Variant& operator=(Variant&&);
  57. void clear();
  58. ~Variant();
  59. enum class Type {
  60. Invalid,
  61. Bool,
  62. Int32,
  63. Int64,
  64. UnsignedInt,
  65. Float,
  66. String,
  67. Bitmap,
  68. Color,
  69. Icon,
  70. Point,
  71. Size,
  72. Rect,
  73. Font,
  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. Type type() const { return m_type; }
  90. bool as_bool() const
  91. {
  92. ASSERT(type() == Type::Bool);
  93. return m_value.as_bool;
  94. }
  95. bool to_bool() const
  96. {
  97. if (type() == Type::Bool)
  98. return as_bool();
  99. if (type() == Type::String)
  100. return !!m_value.as_string;
  101. if (type() == Type::Int32)
  102. return m_value.as_i32 != 0;
  103. if (type() == Type::Int64)
  104. return m_value.as_i64 != 0;
  105. if (type() == Type::UnsignedInt)
  106. return m_value.as_uint != 0;
  107. if (type() == Type::Rect)
  108. return !as_rect().is_null();
  109. if (type() == Type::Size)
  110. return !as_size().is_null();
  111. if (type() == Type::Point)
  112. return !as_point().is_null();
  113. return is_valid();
  114. }
  115. int as_i32() const
  116. {
  117. ASSERT(type() == Type::Int32);
  118. return m_value.as_i32;
  119. }
  120. int as_i64() const
  121. {
  122. ASSERT(type() == Type::Int64);
  123. return m_value.as_i64;
  124. }
  125. unsigned as_uint() const
  126. {
  127. ASSERT(type() == Type::UnsignedInt);
  128. return m_value.as_uint;
  129. }
  130. template<typename T>
  131. T to_integer() const
  132. {
  133. if (is_i32())
  134. return as_i32();
  135. if (is_i64())
  136. return as_i64();
  137. if (is_bool())
  138. return as_bool() ? 1 : 0;
  139. if (is_float())
  140. return (int)as_float();
  141. if (is_uint()) {
  142. ASSERT(as_uint() <= INT32_MAX);
  143. return (int)as_uint();
  144. }
  145. if (is_string()) {
  146. bool ok;
  147. int value = as_string().to_int(ok);
  148. if (!ok)
  149. return 0;
  150. return value;
  151. }
  152. return 0;
  153. }
  154. i32 to_i32() const
  155. {
  156. return to_integer<i32>();
  157. }
  158. i64 to_i64() const
  159. {
  160. return to_integer<i64>();
  161. }
  162. float as_float() const
  163. {
  164. ASSERT(type() == Type::Float);
  165. return m_value.as_float;
  166. }
  167. Gfx::Point as_point() const
  168. {
  169. return { m_value.as_point.x, m_value.as_point.y };
  170. }
  171. Gfx::Size as_size() const
  172. {
  173. return { m_value.as_size.width, m_value.as_size.height };
  174. }
  175. Gfx::Rect as_rect() const
  176. {
  177. return { as_point(), as_size() };
  178. }
  179. String as_string() const
  180. {
  181. ASSERT(type() == Type::String);
  182. return m_value.as_string;
  183. }
  184. const Gfx::Bitmap& as_bitmap() const
  185. {
  186. ASSERT(type() == Type::Bitmap);
  187. return *m_value.as_bitmap;
  188. }
  189. GIcon as_icon() const
  190. {
  191. ASSERT(type() == Type::Icon);
  192. return GIcon(*m_value.as_icon);
  193. }
  194. Color as_color() const
  195. {
  196. ASSERT(type() == Type::Color);
  197. return Color::from_rgba(m_value.as_color);
  198. }
  199. const Gfx::Font& as_font() const
  200. {
  201. ASSERT(type() == Type::Font);
  202. return *m_value.as_font;
  203. }
  204. Color to_color(Color default_value = {}) const
  205. {
  206. if (type() == Type::Color)
  207. return as_color();
  208. if (type() == Type::String) {
  209. auto color = Color::from_string(as_string());
  210. if (color.has_value())
  211. return color.value();
  212. }
  213. return default_value;
  214. }
  215. String to_string() const;
  216. bool operator==(const Variant&) const;
  217. bool operator<(const Variant&) const;
  218. private:
  219. void copy_from(const Variant&);
  220. struct RawPoint {
  221. int x;
  222. int y;
  223. };
  224. struct RawSize {
  225. int width;
  226. int height;
  227. };
  228. struct RawRect {
  229. RawPoint location;
  230. RawSize size;
  231. };
  232. union {
  233. StringImpl* as_string;
  234. Gfx::Bitmap* as_bitmap;
  235. GIconImpl* as_icon;
  236. Gfx::Font* as_font;
  237. bool as_bool;
  238. i32 as_i32;
  239. i64 as_i64;
  240. unsigned as_uint;
  241. float as_float;
  242. Gfx::RGBA32 as_color;
  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. }