Variant.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/FlyString.h>
  7. #include <AK/JsonValue.h>
  8. #include <AK/RefPtr.h>
  9. #include <LibGUI/Variant.h>
  10. namespace GUI {
  11. const char* to_string(Variant::Type type)
  12. {
  13. switch (type) {
  14. case Variant::Type::Invalid:
  15. return "Invalid";
  16. case Variant::Type::Bool:
  17. return "Bool";
  18. case Variant::Type::Int32:
  19. return "Int32";
  20. case Variant::Type::Int64:
  21. return "Int64";
  22. case Variant::Type::UnsignedInt32:
  23. return "UnsignedInt32";
  24. case Variant::Type::UnsignedInt64:
  25. return "UnsignedInt64";
  26. case Variant::Type::Float:
  27. return "Float";
  28. case Variant::Type::String:
  29. return "String";
  30. case Variant::Type::Bitmap:
  31. return "Bitmap";
  32. case Variant::Type::Color:
  33. return "Color";
  34. case Variant::Type::Icon:
  35. return "Icon";
  36. case Variant::Type::Point:
  37. return "Point";
  38. case Variant::Type::Size:
  39. return "Size";
  40. case Variant::Type::Rect:
  41. return "Rect";
  42. case Variant::Type::Font:
  43. return "Font";
  44. case Variant::Type::TextAlignment:
  45. return "TextAlignment";
  46. case Variant::Type::ColorRole:
  47. return "ColorRole";
  48. case Variant::Type::MetricRole:
  49. return "MetricRole";
  50. case Variant::Type::PathRole:
  51. return "PathRole";
  52. }
  53. VERIFY_NOT_REACHED();
  54. }
  55. Variant::Variant()
  56. {
  57. m_value.as_string = nullptr;
  58. }
  59. Variant::~Variant()
  60. {
  61. clear();
  62. }
  63. void Variant::clear()
  64. {
  65. switch (m_type) {
  66. case Type::String:
  67. AK::unref_if_not_null(m_value.as_string);
  68. break;
  69. case Type::Bitmap:
  70. AK::unref_if_not_null(m_value.as_bitmap);
  71. break;
  72. case Type::Icon:
  73. AK::unref_if_not_null(m_value.as_icon);
  74. break;
  75. default:
  76. break;
  77. }
  78. m_type = Type::Invalid;
  79. m_value.as_string = nullptr;
  80. }
  81. Variant::Variant(Gfx::TextAlignment value)
  82. : m_type(Type::TextAlignment)
  83. {
  84. m_value.as_text_alignment = value;
  85. }
  86. Variant::Variant(Gfx::ColorRole value)
  87. : m_type(Type::ColorRole)
  88. {
  89. m_value.as_color_role = value;
  90. }
  91. Variant::Variant(Gfx::MetricRole value)
  92. : m_type(Type::MetricRole)
  93. {
  94. m_value.as_metric_role = value;
  95. }
  96. Variant::Variant(Gfx::PathRole value)
  97. : m_type(Type::PathRole)
  98. {
  99. m_value.as_path_role = value;
  100. }
  101. Variant::Variant(i32 value)
  102. : m_type(Type::Int32)
  103. {
  104. m_value.as_i32 = value;
  105. }
  106. Variant::Variant(i64 value)
  107. : m_type(Type::Int64)
  108. {
  109. m_value.as_i64 = value;
  110. }
  111. Variant::Variant(u32 value)
  112. : m_type(Type::UnsignedInt32)
  113. {
  114. m_value.as_u32 = value;
  115. }
  116. Variant::Variant(u64 value)
  117. : m_type(Type::UnsignedInt64)
  118. {
  119. m_value.as_u64 = value;
  120. }
  121. Variant::Variant(float value)
  122. : m_type(Type::Float)
  123. {
  124. m_value.as_float = value;
  125. }
  126. Variant::Variant(bool value)
  127. : m_type(Type::Bool)
  128. {
  129. m_value.as_bool = value;
  130. }
  131. Variant::Variant(const char* cstring)
  132. : Variant(String(cstring))
  133. {
  134. }
  135. Variant::Variant(const FlyString& value)
  136. : Variant(String(value.impl()))
  137. {
  138. }
  139. Variant::Variant(const StringView& value)
  140. : Variant(value.to_string())
  141. {
  142. }
  143. Variant::Variant(const String& value)
  144. : m_type(Type::String)
  145. {
  146. m_value.as_string = const_cast<StringImpl*>(value.impl());
  147. AK::ref_if_not_null(m_value.as_string);
  148. }
  149. Variant::Variant(const JsonValue& value)
  150. {
  151. if (value.is_null()) {
  152. m_value.as_string = nullptr;
  153. return;
  154. }
  155. if (value.is_i32()) {
  156. m_type = Type::Int32;
  157. m_value.as_i32 = value.as_i32();
  158. return;
  159. }
  160. if (value.is_u32()) {
  161. m_type = Type::UnsignedInt32;
  162. m_value.as_u32 = value.as_u32();
  163. return;
  164. }
  165. if (value.is_i64()) {
  166. m_type = Type::Int64;
  167. m_value.as_i64 = value.as_i64();
  168. return;
  169. }
  170. if (value.is_u64()) {
  171. m_type = Type::UnsignedInt64;
  172. m_value.as_u64 = value.to_u64();
  173. return;
  174. }
  175. if (value.is_string()) {
  176. m_type = Type::String;
  177. m_value.as_string = value.as_string().impl();
  178. m_value.as_string->ref();
  179. return;
  180. }
  181. if (value.is_bool()) {
  182. m_type = Type::Bool;
  183. m_value.as_bool = value.as_bool();
  184. return;
  185. }
  186. VERIFY_NOT_REACHED();
  187. }
  188. Variant::Variant(const Gfx::Bitmap& value)
  189. : m_type(Type::Bitmap)
  190. {
  191. m_value.as_bitmap = const_cast<Gfx::Bitmap*>(&value);
  192. AK::ref_if_not_null(m_value.as_bitmap);
  193. }
  194. Variant::Variant(const GUI::Icon& value)
  195. : m_type(Type::Icon)
  196. {
  197. m_value.as_icon = &const_cast<GUI::IconImpl&>(value.impl());
  198. AK::ref_if_not_null(m_value.as_icon);
  199. }
  200. Variant::Variant(const Gfx::Font& value)
  201. : m_type(Type::Font)
  202. {
  203. m_value.as_font = &const_cast<Gfx::Font&>(value);
  204. AK::ref_if_not_null(m_value.as_font);
  205. }
  206. Variant::Variant(Color color)
  207. : m_type(Type::Color)
  208. {
  209. m_value.as_color = color.value();
  210. }
  211. Variant::Variant(const Gfx::IntPoint& point)
  212. : m_type(Type::Point)
  213. {
  214. m_value.as_point = { point.x(), point.y() };
  215. }
  216. Variant::Variant(const Gfx::IntSize& size)
  217. : m_type(Type::Size)
  218. {
  219. m_value.as_size = { size.width(), size.height() };
  220. }
  221. Variant::Variant(const Gfx::IntRect& rect)
  222. : m_type(Type::Rect)
  223. {
  224. m_value.as_rect = (const RawRect&)rect;
  225. }
  226. Variant& Variant::operator=(const Variant& other)
  227. {
  228. if (&other == this)
  229. return *this;
  230. clear();
  231. copy_from(other);
  232. return *this;
  233. }
  234. Variant& Variant::operator=(Variant&& other)
  235. {
  236. if (&other == this)
  237. return *this;
  238. clear();
  239. move_from(move(other));
  240. return *this;
  241. }
  242. Variant::Variant(const Variant& other)
  243. {
  244. copy_from(other);
  245. }
  246. void Variant::move_from(Variant&& other)
  247. {
  248. m_type = other.m_type;
  249. m_value = other.m_value;
  250. other.m_type = Type::Invalid;
  251. other.m_value.as_string = nullptr;
  252. }
  253. void Variant::copy_from(const Variant& other)
  254. {
  255. VERIFY(!is_valid());
  256. m_type = other.m_type;
  257. switch (m_type) {
  258. case Type::Bool:
  259. m_value.as_bool = other.m_value.as_bool;
  260. break;
  261. case Type::Int32:
  262. m_value.as_i32 = other.m_value.as_i32;
  263. break;
  264. case Type::Int64:
  265. m_value.as_i64 = other.m_value.as_i64;
  266. break;
  267. case Type::UnsignedInt32:
  268. m_value.as_u32 = other.m_value.as_u32;
  269. break;
  270. case Type::UnsignedInt64:
  271. m_value.as_u64 = other.m_value.as_u64;
  272. break;
  273. case Type::Float:
  274. m_value.as_float = other.m_value.as_float;
  275. break;
  276. case Type::String:
  277. m_value.as_string = other.m_value.as_string;
  278. AK::ref_if_not_null(m_value.as_bitmap);
  279. break;
  280. case Type::Bitmap:
  281. m_value.as_bitmap = other.m_value.as_bitmap;
  282. AK::ref_if_not_null(m_value.as_bitmap);
  283. break;
  284. case Type::Icon:
  285. m_value.as_icon = other.m_value.as_icon;
  286. AK::ref_if_not_null(m_value.as_icon);
  287. break;
  288. case Type::Font:
  289. m_value.as_font = other.m_value.as_font;
  290. AK::ref_if_not_null(m_value.as_font);
  291. break;
  292. case Type::Color:
  293. m_value.as_color = other.m_value.as_color;
  294. break;
  295. case Type::Point:
  296. m_value.as_point = other.m_value.as_point;
  297. break;
  298. case Type::Size:
  299. m_value.as_size = other.m_value.as_size;
  300. break;
  301. case Type::Rect:
  302. m_value.as_rect = other.m_value.as_rect;
  303. break;
  304. case Type::TextAlignment:
  305. m_value.as_text_alignment = other.m_value.as_text_alignment;
  306. break;
  307. case Type::ColorRole:
  308. m_value.as_color_role = other.m_value.as_color_role;
  309. break;
  310. case Type::MetricRole:
  311. m_value.as_metric_role = other.m_value.as_metric_role;
  312. break;
  313. case Type::PathRole:
  314. m_value.as_path_role = other.m_value.as_path_role;
  315. break;
  316. case Type::Invalid:
  317. break;
  318. }
  319. }
  320. bool Variant::operator==(const Variant& other) const
  321. {
  322. if (m_type != other.m_type)
  323. return to_string() == other.to_string();
  324. switch (m_type) {
  325. case Type::Bool:
  326. return as_bool() == other.as_bool();
  327. case Type::Int32:
  328. return as_i32() == other.as_i32();
  329. case Type::Int64:
  330. return as_i64() == other.as_i64();
  331. case Type::UnsignedInt32:
  332. return as_u32() == other.as_u32();
  333. case Type::UnsignedInt64:
  334. return as_u64() == other.as_u64();
  335. case Type::Float:
  336. return as_float() == other.as_float();
  337. case Type::String:
  338. return as_string() == other.as_string();
  339. case Type::Bitmap:
  340. return m_value.as_bitmap == other.m_value.as_bitmap;
  341. case Type::Icon:
  342. return m_value.as_icon == other.m_value.as_icon;
  343. case Type::Color:
  344. return m_value.as_color == other.m_value.as_color;
  345. case Type::Point:
  346. return as_point() == other.as_point();
  347. case Type::Size:
  348. return as_size() == other.as_size();
  349. case Type::Rect:
  350. return as_rect() == other.as_rect();
  351. case Type::Font:
  352. return &as_font() == &other.as_font();
  353. case Type::TextAlignment:
  354. return m_value.as_text_alignment == other.m_value.as_text_alignment;
  355. case Type::ColorRole:
  356. return m_value.as_color_role == other.m_value.as_color_role;
  357. case Type::MetricRole:
  358. return m_value.as_metric_role == other.m_value.as_metric_role;
  359. case Type::PathRole:
  360. return m_value.as_path_role == other.m_value.as_path_role;
  361. case Type::Invalid:
  362. return true;
  363. }
  364. VERIFY_NOT_REACHED();
  365. }
  366. bool Variant::operator<(const Variant& other) const
  367. {
  368. if (m_type != other.m_type)
  369. return to_string() < other.to_string();
  370. switch (m_type) {
  371. case Type::Bool:
  372. return as_bool() < other.as_bool();
  373. case Type::Int32:
  374. return as_i32() < other.as_i32();
  375. case Type::Int64:
  376. return as_i64() < other.as_i64();
  377. case Type::UnsignedInt32:
  378. return as_u32() < other.as_u32();
  379. case Type::UnsignedInt64:
  380. return as_u64() < other.as_u64();
  381. case Type::Float:
  382. return as_float() < other.as_float();
  383. case Type::String:
  384. return as_string() < other.as_string();
  385. case Type::Bitmap:
  386. // FIXME: Maybe compare bitmaps somehow differently?
  387. return m_value.as_bitmap < other.m_value.as_bitmap;
  388. case Type::Icon:
  389. // FIXME: Maybe compare icons somehow differently?
  390. return m_value.as_icon < other.m_value.as_icon;
  391. case Type::Color:
  392. return m_value.as_color < other.m_value.as_color;
  393. case Type::Point:
  394. case Type::Size:
  395. case Type::Rect:
  396. case Type::Font:
  397. case Type::TextAlignment:
  398. case Type::ColorRole:
  399. case Type::MetricRole:
  400. case Type::PathRole:
  401. // FIXME: Figure out how to compare these.
  402. VERIFY_NOT_REACHED();
  403. case Type::Invalid:
  404. break;
  405. }
  406. VERIFY_NOT_REACHED();
  407. }
  408. String Variant::to_string() const
  409. {
  410. switch (m_type) {
  411. case Type::Bool:
  412. return as_bool() ? "true" : "false";
  413. case Type::Int32:
  414. return String::number(as_i32());
  415. case Type::Int64:
  416. return String::number(as_i64());
  417. case Type::UnsignedInt32:
  418. return String::number(as_u32());
  419. case Type::UnsignedInt64:
  420. return String::number(as_u64());
  421. case Type::Float:
  422. return String::formatted("{:.2}", as_float());
  423. case Type::String:
  424. return as_string();
  425. case Type::Bitmap:
  426. return "[Gfx::Bitmap]";
  427. case Type::Icon:
  428. return "[GUI::Icon]";
  429. case Type::Color:
  430. return as_color().to_string();
  431. case Type::Point:
  432. return as_point().to_string();
  433. case Type::Size:
  434. return as_size().to_string();
  435. case Type::Rect:
  436. return as_rect().to_string();
  437. case Type::Font:
  438. return String::formatted("[Font: {}]", as_font().name());
  439. case Type::TextAlignment: {
  440. switch (m_value.as_text_alignment) {
  441. case Gfx::TextAlignment::Center:
  442. return "Gfx::TextAlignment::Center";
  443. case Gfx::TextAlignment::CenterLeft:
  444. return "Gfx::TextAlignment::CenterLeft";
  445. case Gfx::TextAlignment::CenterRight:
  446. return "Gfx::TextAlignment::CenterRight";
  447. case Gfx::TextAlignment::TopLeft:
  448. return "Gfx::TextAlignment::TopLeft";
  449. case Gfx::TextAlignment::TopRight:
  450. return "Gfx::TextAlignment::TopRight";
  451. default:
  452. VERIFY_NOT_REACHED();
  453. }
  454. return "";
  455. }
  456. case Type::ColorRole:
  457. return String::formatted("Gfx::ColorRole::{}", Gfx::to_string(m_value.as_color_role));
  458. case Type::MetricRole:
  459. return String::formatted("Gfx::MetricRole::{}", Gfx::to_string(m_value.as_metric_role));
  460. case Type::PathRole:
  461. return String::formatted("Gfx::PathRole::{}", Gfx::to_string(m_value.as_path_role));
  462. case Type::Invalid:
  463. return "[null]";
  464. }
  465. VERIFY_NOT_REACHED();
  466. }
  467. }