Variant.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
  4. * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/Concepts.h>
  10. #include <AK/String.h>
  11. #include <LibGUI/Icon.h>
  12. #include <LibGfx/Bitmap.h>
  13. #include <LibGfx/Font/Font.h>
  14. #include <LibGfx/SystemTheme.h>
  15. namespace GUI {
  16. namespace Detail {
  17. struct Boolean {
  18. bool value;
  19. };
  20. using VariantUnderlyingType = AK::Variant<Empty, Boolean, float, i32, i64, u32, u64, String, Color, Gfx::IntPoint, Gfx::IntSize, Gfx::IntRect, Gfx::TextAlignment, Gfx::ColorRole, Gfx::AlignmentRole, Gfx::FlagRole, Gfx::MetricRole, Gfx::PathRole, NonnullRefPtr<Gfx::Bitmap>, NonnullRefPtr<Gfx::Font>, GUI::Icon>;
  21. }
  22. class Variant : public Detail::VariantUnderlyingType {
  23. public:
  24. using Detail::VariantUnderlyingType::Variant;
  25. using Detail::VariantUnderlyingType::operator=;
  26. Variant(JsonValue const&);
  27. Variant& operator=(JsonValue const&);
  28. Variant(bool v)
  29. : Variant(Detail::Boolean { v })
  30. {
  31. }
  32. Variant& operator=(bool v)
  33. {
  34. set(Detail::Boolean { v });
  35. return *this;
  36. }
  37. template<typename T>
  38. Variant(T&& value) requires(IsConstructible<String, T>)
  39. : Variant(String(forward<T>(value)))
  40. {
  41. }
  42. template<typename T>
  43. Variant& operator=(T&& v) requires(IsConstructible<String, T>)
  44. {
  45. set(String(v));
  46. return *this;
  47. }
  48. template<OneOfIgnoringCV<Gfx::Bitmap, Gfx::Font> T>
  49. Variant(T const& value)
  50. : Variant(NonnullRefPtr<RemoveCV<T>>(value))
  51. {
  52. }
  53. template<OneOfIgnoringCV<Gfx::Bitmap, Gfx::Font> T>
  54. Variant& operator=(T&& value)
  55. {
  56. set(NonnullRefPtr<RemoveCV<T>>(forward<T>(value)));
  57. return *this;
  58. }
  59. ~Variant() = default;
  60. bool is_valid() const { return !has<Empty>(); }
  61. bool is_bool() const { return has<Detail::Boolean>(); }
  62. bool is_i32() const { return has<i32>(); }
  63. bool is_i64() const { return has<i64>(); }
  64. bool is_u32() const { return has<u32>(); }
  65. bool is_u64() const { return has<u64>(); }
  66. bool is_float() const { return has<float>(); }
  67. bool is_string() const { return has<String>(); }
  68. bool is_bitmap() const { return has<NonnullRefPtr<Gfx::Bitmap>>(); }
  69. bool is_color() const { return has<Color>(); }
  70. bool is_icon() const { return has<GUI::Icon>(); }
  71. bool is_point() const { return has<Gfx::IntPoint>(); }
  72. bool is_size() const { return has<Gfx::IntSize>(); }
  73. bool is_rect() const { return has<Gfx::IntRect>(); }
  74. bool is_font() const { return has<NonnullRefPtr<Gfx::Font>>(); }
  75. bool is_text_alignment() const { return has<Gfx::TextAlignment>(); }
  76. bool is_color_role() const { return has<Gfx::ColorRole>(); }
  77. bool is_alignment_role() const { return has<Gfx::AlignmentRole>(); }
  78. bool is_flag_role() const { return has<Gfx::FlagRole>(); }
  79. bool is_metric_role() const { return has<Gfx::MetricRole>(); }
  80. bool is_path_role() const { return has<Gfx::PathRole>(); }
  81. bool as_bool() const { return get<Detail::Boolean>().value; }
  82. bool to_bool() const
  83. {
  84. return visit(
  85. [](Empty) { return false; },
  86. [](Detail::Boolean v) { return v.value; },
  87. [](String const& v) { return !v.is_null(); },
  88. [](Integral auto v) { return v != 0; },
  89. [](OneOf<Gfx::IntRect, Gfx::IntPoint, Gfx::IntSize> auto const& v) { return !v.is_null(); },
  90. [](Enum auto const&) { return true; },
  91. [](OneOf<float, String, Color, NonnullRefPtr<Gfx::Font>, NonnullRefPtr<Gfx::Bitmap>, GUI::Icon> auto const&) { return true; });
  92. }
  93. i32 as_i32() const { return get<i32>(); }
  94. i64 as_i64() const { return get<i64>(); }
  95. u32 as_u32() const { return get<u32>(); }
  96. u64 as_u64() const { return get<u64>(); }
  97. template<Integral T>
  98. T to_integer() const
  99. {
  100. return visit(
  101. [](Empty) -> T { return 0; },
  102. [](Integral auto v) { return static_cast<T>(v); },
  103. [](FloatingPoint auto v) { return (T)v; },
  104. [](Detail::Boolean v) -> T { return v.value ? 1 : 0; },
  105. [](String const& v) {
  106. if constexpr (IsUnsigned<T>)
  107. return v.to_uint<T>().value_or(0u);
  108. else
  109. return v.to_int<T>().value_or(0);
  110. },
  111. [](Enum auto const&) -> T { return 0; },
  112. [](OneOf<Gfx::IntPoint, Gfx::IntRect, Gfx::IntSize, Color, NonnullRefPtr<Gfx::Font>, NonnullRefPtr<Gfx::Bitmap>, GUI::Icon> auto const&) -> T { return 0; });
  113. }
  114. i32 to_i32() const { return to_integer<i32>(); }
  115. i64 to_i64() const { return to_integer<i64>(); }
  116. float as_float() const { return get<float>(); }
  117. float as_float_or(float fallback) const
  118. {
  119. if (auto const* p = get_pointer<float>())
  120. return *p;
  121. return fallback;
  122. }
  123. Gfx::IntPoint as_point() const { return get<Gfx::IntPoint>(); }
  124. Gfx::IntSize as_size() const { return get<Gfx::IntSize>(); }
  125. Gfx::IntRect as_rect() const { return get<Gfx::IntRect>(); }
  126. String as_string() const { return get<String>(); }
  127. Gfx::Bitmap const& as_bitmap() const { return *get<NonnullRefPtr<Gfx::Bitmap>>(); }
  128. GUI::Icon as_icon() const { return get<GUI::Icon>(); }
  129. Color as_color() const { return get<Color>(); }
  130. Gfx::Font const& as_font() const { return *get<NonnullRefPtr<Gfx::Font>>(); }
  131. Gfx::TextAlignment to_text_alignment(Gfx::TextAlignment default_value) const
  132. {
  133. if (auto const* p = get_pointer<Gfx::TextAlignment>())
  134. return *p;
  135. return default_value;
  136. }
  137. Gfx::ColorRole to_color_role() const
  138. {
  139. if (auto const* p = get_pointer<Gfx::ColorRole>())
  140. return *p;
  141. return Gfx::ColorRole::NoRole;
  142. }
  143. Gfx::AlignmentRole to_alignment_role() const
  144. {
  145. if (auto const* p = get_pointer<Gfx::AlignmentRole>())
  146. return *p;
  147. return Gfx::AlignmentRole::NoRole;
  148. }
  149. Gfx::FlagRole to_flag_role() const
  150. {
  151. if (auto const* p = get_pointer<Gfx::FlagRole>())
  152. return *p;
  153. return Gfx::FlagRole::NoRole;
  154. }
  155. Gfx::MetricRole to_metric_role() const
  156. {
  157. if (auto const* p = get_pointer<Gfx::MetricRole>())
  158. return *p;
  159. return Gfx::MetricRole::NoRole;
  160. }
  161. Gfx::PathRole to_path_role() const
  162. {
  163. if (auto const* p = get_pointer<Gfx::PathRole>())
  164. return *p;
  165. return Gfx::PathRole::NoRole;
  166. }
  167. Color to_color(Color default_value = {}) const
  168. {
  169. if (auto const* p = get_pointer<Color>())
  170. return *p;
  171. if (auto const* p = get_pointer<String>())
  172. return Color::from_string(*p).value_or(default_value);
  173. return default_value;
  174. }
  175. String to_string() const
  176. {
  177. return visit(
  178. [](Empty) -> String { return "[null]"; },
  179. [](Gfx::TextAlignment v) { return String::formatted("Gfx::TextAlignment::{}", Gfx::to_string(v)); },
  180. [](Gfx::ColorRole v) { return String::formatted("Gfx::ColorRole::{}", Gfx::to_string(v)); },
  181. [](Gfx::AlignmentRole v) { return String::formatted("Gfx::AlignmentRole::{}", Gfx::to_string(v)); },
  182. [](Gfx::FlagRole v) { return String::formatted("Gfx::FlagRole::{}", Gfx::to_string(v)); },
  183. [](Gfx::MetricRole v) { return String::formatted("Gfx::MetricRole::{}", Gfx::to_string(v)); },
  184. [](Gfx::PathRole v) { return String::formatted("Gfx::PathRole::{}", Gfx::to_string(v)); },
  185. [](NonnullRefPtr<Gfx::Font> const& font) { return String::formatted("[Font: {}]", font->name()); },
  186. [](NonnullRefPtr<Gfx::Bitmap> const&) -> String { return "[Gfx::Bitmap]"; },
  187. [](GUI::Icon const&) -> String { return "[GUI::Icon]"; },
  188. [](Detail::Boolean v) { return String::formatted("{}", v.value); },
  189. [](auto const& v) { return String::formatted("{}", v); });
  190. }
  191. bool operator==(Variant const&) const;
  192. bool operator<(Variant const&) const;
  193. };
  194. }