GVariant.h 7.1 KB

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