GVariant.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #pragma once
  2. #include <AK/String.h>
  3. #include <LibDraw/Font.h>
  4. #include <LibDraw/GraphicsBitmap.h>
  5. #include <LibGUI/GIcon.h>
  6. namespace AK {
  7. class JsonValue;
  8. }
  9. class GVariant {
  10. public:
  11. GVariant();
  12. GVariant(bool);
  13. GVariant(float);
  14. GVariant(int);
  15. GVariant(unsigned);
  16. GVariant(const char*);
  17. GVariant(const String&);
  18. GVariant(const GraphicsBitmap&);
  19. GVariant(const GIcon&);
  20. GVariant(const Point&);
  21. GVariant(const Size&);
  22. GVariant(const Rect&);
  23. GVariant(const Font&);
  24. GVariant(const AK::JsonValue&);
  25. GVariant(Color);
  26. GVariant(const GVariant&);
  27. GVariant& operator=(const GVariant&);
  28. GVariant(GVariant&&) = delete;
  29. GVariant& operator=(GVariant&&);
  30. void clear();
  31. ~GVariant();
  32. enum class Type {
  33. Invalid,
  34. Bool,
  35. Int,
  36. UnsignedInt,
  37. Float,
  38. String,
  39. Bitmap,
  40. Color,
  41. Icon,
  42. Point,
  43. Size,
  44. Rect,
  45. Font,
  46. };
  47. bool is_valid() const { return m_type != Type::Invalid; }
  48. bool is_bool() const { return m_type == Type::Bool; }
  49. bool is_int() const { return m_type == Type::Int; }
  50. bool is_uint() const { return m_type == Type::UnsignedInt; }
  51. bool is_float() const { return m_type == Type::Float; }
  52. bool is_string() const { return m_type == Type::String; }
  53. bool is_bitmap() const { return m_type == Type::Bitmap; }
  54. bool is_color() const { return m_type == Type::Color; }
  55. bool is_icon() const { return m_type == Type::Icon; }
  56. bool is_point() const { return m_type == Type::Point; }
  57. bool is_size() const { return m_type == Type::Size; }
  58. bool is_rect() const { return m_type == Type::Rect; }
  59. bool is_font() const { return m_type == Type::Font; }
  60. Type type() const { return m_type; }
  61. bool as_bool() const
  62. {
  63. ASSERT(type() == Type::Bool);
  64. return m_value.as_bool;
  65. }
  66. bool to_bool() const
  67. {
  68. if (type() == Type::Bool)
  69. return as_bool();
  70. if (type() == Type::String)
  71. return !!m_value.as_string;
  72. if (type() == Type::Int)
  73. return m_value.as_int != 0;
  74. if (type() == Type::UnsignedInt)
  75. return m_value.as_uint != 0;
  76. if (type() == Type::Rect)
  77. return !as_rect().is_null();
  78. if (type() == Type::Size)
  79. return !as_size().is_null();
  80. if (type() == Type::Point)
  81. return !as_point().is_null();
  82. return is_valid();
  83. }
  84. int as_int() const
  85. {
  86. ASSERT(type() == Type::Int);
  87. return m_value.as_int;
  88. }
  89. unsigned as_uint() const
  90. {
  91. ASSERT(type() == Type::UnsignedInt);
  92. return m_value.as_uint;
  93. }
  94. int to_int() const
  95. {
  96. if (is_int())
  97. return as_int();
  98. if (is_bool())
  99. return as_bool() ? 1 : 0;
  100. if (is_float())
  101. return (int)as_float();
  102. if (is_uint()) {
  103. ASSERT(as_uint() <= INT32_MAX);
  104. return (int)as_uint();
  105. }
  106. if (is_string()) {
  107. bool ok;
  108. int value = as_string().to_int(ok);
  109. if (!ok)
  110. return 0;
  111. return value;
  112. }
  113. return 0;
  114. }
  115. float as_float() const
  116. {
  117. ASSERT(type() == Type::Float);
  118. return m_value.as_float;
  119. }
  120. Point as_point() const
  121. {
  122. return { m_value.as_point.x, m_value.as_point.y };
  123. }
  124. Size as_size() const
  125. {
  126. return { m_value.as_size.width, m_value.as_size.height };
  127. }
  128. Rect as_rect() const
  129. {
  130. return { as_point(), as_size() };
  131. }
  132. String as_string() const
  133. {
  134. ASSERT(type() == Type::String);
  135. return m_value.as_string;
  136. }
  137. const GraphicsBitmap& as_bitmap() const
  138. {
  139. ASSERT(type() == Type::Bitmap);
  140. return *m_value.as_bitmap;
  141. }
  142. GIcon as_icon() const
  143. {
  144. ASSERT(type() == Type::Icon);
  145. return GIcon(*m_value.as_icon);
  146. }
  147. Color as_color() const
  148. {
  149. ASSERT(type() == Type::Color);
  150. return Color::from_rgba(m_value.as_color);
  151. }
  152. const Font& as_font() const
  153. {
  154. ASSERT(type() == Type::Font);
  155. return *m_value.as_font;
  156. }
  157. Color to_color(Color default_value = {}) const
  158. {
  159. if (type() == Type::Color)
  160. return as_color();
  161. if (type() == Type::String) {
  162. auto color = Color::from_string(as_string());
  163. if (color.has_value())
  164. return color.value();
  165. }
  166. return default_value;
  167. }
  168. String to_string() const;
  169. bool operator==(const GVariant&) const;
  170. bool operator<(const GVariant&) const;
  171. private:
  172. void copy_from(const GVariant&);
  173. struct RawPoint {
  174. int x;
  175. int y;
  176. };
  177. struct RawSize {
  178. int width;
  179. int height;
  180. };
  181. struct RawRect {
  182. RawPoint location;
  183. RawSize size;
  184. };
  185. union {
  186. StringImpl* as_string;
  187. GraphicsBitmap* as_bitmap;
  188. GIconImpl* as_icon;
  189. Font* as_font;
  190. bool as_bool;
  191. int as_int;
  192. unsigned as_uint;
  193. float as_float;
  194. RGBA32 as_color;
  195. RawPoint as_point;
  196. RawSize as_size;
  197. RawRect as_rect;
  198. } m_value;
  199. Type m_type { Type::Invalid };
  200. };
  201. const char* to_string(GVariant::Type);