Variant.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. #include <AK/FlyString.h>
  27. #include <AK/JsonValue.h>
  28. #include <LibGUI/Variant.h>
  29. namespace GUI {
  30. const char* to_string(Variant::Type type)
  31. {
  32. switch (type) {
  33. case Variant::Type::Invalid:
  34. return "Invalid";
  35. case Variant::Type::Bool:
  36. return "Bool";
  37. case Variant::Type::Int32:
  38. return "Int32";
  39. case Variant::Type::Int64:
  40. return "Int64";
  41. case Variant::Type::UnsignedInt:
  42. return "UnsignedInt";
  43. case Variant::Type::Float:
  44. return "Float";
  45. case Variant::Type::String:
  46. return "String";
  47. case Variant::Type::Bitmap:
  48. return "Bitmap";
  49. case Variant::Type::Color:
  50. return "Color";
  51. case Variant::Type::Icon:
  52. return "Icon";
  53. case Variant::Type::Point:
  54. return "Point";
  55. case Variant::Type::Size:
  56. return "Size";
  57. case Variant::Type::Rect:
  58. return "Rect";
  59. case Variant::Type::Font:
  60. return "Font";
  61. }
  62. ASSERT_NOT_REACHED();
  63. }
  64. Variant::Variant()
  65. {
  66. m_value.as_string = nullptr;
  67. }
  68. Variant::~Variant()
  69. {
  70. clear();
  71. }
  72. void Variant::clear()
  73. {
  74. switch (m_type) {
  75. case Type::String:
  76. AK::unref_if_not_null(m_value.as_string);
  77. break;
  78. case Type::Bitmap:
  79. AK::unref_if_not_null(m_value.as_bitmap);
  80. break;
  81. case Type::Icon:
  82. AK::unref_if_not_null(m_value.as_icon);
  83. break;
  84. default:
  85. break;
  86. }
  87. m_type = Type::Invalid;
  88. m_value.as_string = nullptr;
  89. }
  90. Variant::Variant(i32 value)
  91. : m_type(Type::Int32)
  92. {
  93. m_value.as_i32 = value;
  94. }
  95. Variant::Variant(i64 value)
  96. : m_type(Type::Int64)
  97. {
  98. m_value.as_i64 = value;
  99. }
  100. Variant::Variant(unsigned value)
  101. : m_type(Type::UnsignedInt)
  102. {
  103. m_value.as_uint = value;
  104. }
  105. Variant::Variant(float value)
  106. : m_type(Type::Float)
  107. {
  108. m_value.as_float = value;
  109. }
  110. Variant::Variant(bool value)
  111. : m_type(Type::Bool)
  112. {
  113. m_value.as_bool = value;
  114. }
  115. Variant::Variant(const char* cstring)
  116. : Variant(String(cstring))
  117. {
  118. }
  119. Variant::Variant(const FlyString& value)
  120. : Variant(String(value.impl()))
  121. {
  122. }
  123. Variant::Variant(const String& value)
  124. : m_type(Type::String)
  125. {
  126. m_value.as_string = const_cast<StringImpl*>(value.impl());
  127. AK::ref_if_not_null(m_value.as_string);
  128. }
  129. Variant::Variant(const JsonValue& value)
  130. {
  131. if (value.is_null()) {
  132. m_value.as_string = nullptr;
  133. return;
  134. }
  135. if (value.is_i32()) {
  136. m_type = Type::Int32;
  137. m_value.as_i32 = value.as_i32();
  138. return;
  139. }
  140. if (value.is_u32()) {
  141. m_type = Type::UnsignedInt;
  142. m_value.as_uint = value.as_u32();
  143. return;
  144. }
  145. if (value.is_i64()) {
  146. m_type = Type::Int64;
  147. m_value.as_i64 = value.as_i64();
  148. return;
  149. }
  150. if (value.is_u64()) {
  151. // FIXME: Variant should have a 64-bit internal type.
  152. m_type = Type::UnsignedInt;
  153. m_value.as_uint = value.to_u32();
  154. return;
  155. }
  156. if (value.is_string()) {
  157. m_type = Type::String;
  158. m_value.as_string = value.as_string().impl();
  159. m_value.as_string->ref();
  160. return;
  161. }
  162. if (value.is_bool()) {
  163. m_type = Type::Bool;
  164. m_value.as_bool = value.as_bool();
  165. return;
  166. }
  167. ASSERT_NOT_REACHED();
  168. }
  169. Variant::Variant(const Gfx::Bitmap& value)
  170. : m_type(Type::Bitmap)
  171. {
  172. m_value.as_bitmap = const_cast<Gfx::Bitmap*>(&value);
  173. AK::ref_if_not_null(m_value.as_bitmap);
  174. }
  175. Variant::Variant(const GUI::Icon& value)
  176. : m_type(Type::Icon)
  177. {
  178. m_value.as_icon = &const_cast<GUI::IconImpl&>(value.impl());
  179. AK::ref_if_not_null(m_value.as_icon);
  180. }
  181. Variant::Variant(const Gfx::Font& value)
  182. : m_type(Type::Font)
  183. {
  184. m_value.as_font = &const_cast<Gfx::Font&>(value);
  185. AK::ref_if_not_null(m_value.as_font);
  186. }
  187. Variant::Variant(Color color)
  188. : m_type(Type::Color)
  189. {
  190. m_value.as_color = color.value();
  191. }
  192. Variant::Variant(const Gfx::Point& point)
  193. : m_type(Type::Point)
  194. {
  195. m_value.as_point = { point.x(), point.y() };
  196. }
  197. Variant::Variant(const Gfx::Size& size)
  198. : m_type(Type::Size)
  199. {
  200. m_value.as_size = { size.width(), size.height() };
  201. }
  202. Variant::Variant(const Gfx::Rect& rect)
  203. : m_type(Type::Rect)
  204. {
  205. m_value.as_rect = (const RawRect&)rect;
  206. }
  207. Variant& Variant::operator=(const Variant& other)
  208. {
  209. if (&other == this)
  210. return *this;
  211. clear();
  212. copy_from(other);
  213. return *this;
  214. }
  215. Variant& Variant::operator=(Variant&& other)
  216. {
  217. if (&other == this)
  218. return *this;
  219. // FIXME: Move, not copy!
  220. clear();
  221. copy_from(other);
  222. other.clear();
  223. return *this;
  224. }
  225. Variant::Variant(const Variant& other)
  226. {
  227. copy_from(other);
  228. }
  229. void Variant::copy_from(const Variant& other)
  230. {
  231. ASSERT(!is_valid());
  232. m_type = other.m_type;
  233. switch (m_type) {
  234. case Type::Bool:
  235. m_value.as_bool = other.m_value.as_bool;
  236. break;
  237. case Type::Int32:
  238. m_value.as_i32 = other.m_value.as_i32;
  239. break;
  240. case Type::Int64:
  241. m_value.as_i64 = other.m_value.as_i64;
  242. break;
  243. case Type::UnsignedInt:
  244. m_value.as_uint = other.m_value.as_uint;
  245. break;
  246. case Type::Float:
  247. m_value.as_float = other.m_value.as_float;
  248. break;
  249. case Type::String:
  250. m_value.as_string = other.m_value.as_string;
  251. AK::ref_if_not_null(m_value.as_bitmap);
  252. break;
  253. case Type::Bitmap:
  254. m_value.as_bitmap = other.m_value.as_bitmap;
  255. AK::ref_if_not_null(m_value.as_bitmap);
  256. break;
  257. case Type::Icon:
  258. m_value.as_icon = other.m_value.as_icon;
  259. AK::ref_if_not_null(m_value.as_icon);
  260. break;
  261. case Type::Font:
  262. m_value.as_font = other.m_value.as_font;
  263. AK::ref_if_not_null(m_value.as_font);
  264. break;
  265. case Type::Color:
  266. m_value.as_color = other.m_value.as_color;
  267. break;
  268. case Type::Point:
  269. m_value.as_point = other.m_value.as_point;
  270. break;
  271. case Type::Size:
  272. m_value.as_size = other.m_value.as_size;
  273. break;
  274. case Type::Rect:
  275. m_value.as_rect = other.m_value.as_rect;
  276. break;
  277. case Type::Invalid:
  278. break;
  279. }
  280. }
  281. bool Variant::operator==(const Variant& other) const
  282. {
  283. if (m_type != other.m_type)
  284. return to_string() == other.to_string();
  285. switch (m_type) {
  286. case Type::Bool:
  287. return as_bool() == other.as_bool();
  288. case Type::Int32:
  289. return as_i32() == other.as_i32();
  290. case Type::Int64:
  291. return as_i64() == other.as_i64();
  292. case Type::UnsignedInt:
  293. return as_uint() == other.as_uint();
  294. case Type::Float:
  295. return as_float() == other.as_float();
  296. case Type::String:
  297. return as_string() == other.as_string();
  298. case Type::Bitmap:
  299. return m_value.as_bitmap == other.m_value.as_bitmap;
  300. case Type::Icon:
  301. return m_value.as_icon == other.m_value.as_icon;
  302. case Type::Color:
  303. return m_value.as_color == other.m_value.as_color;
  304. case Type::Point:
  305. return as_point() == other.as_point();
  306. case Type::Size:
  307. return as_size() == other.as_size();
  308. case Type::Rect:
  309. return as_rect() == other.as_rect();
  310. case Type::Font:
  311. return &as_font() == &other.as_font();
  312. case Type::Invalid:
  313. return true;
  314. }
  315. ASSERT_NOT_REACHED();
  316. }
  317. bool Variant::operator<(const Variant& other) const
  318. {
  319. if (m_type != other.m_type)
  320. return to_string() < other.to_string();
  321. switch (m_type) {
  322. case Type::Bool:
  323. return as_bool() < other.as_bool();
  324. case Type::Int32:
  325. return as_i32() < other.as_i32();
  326. case Type::Int64:
  327. return as_i64() < other.as_i64();
  328. case Type::UnsignedInt:
  329. return as_uint() < other.as_uint();
  330. case Type::Float:
  331. return as_float() < other.as_float();
  332. case Type::String:
  333. return as_string() < other.as_string();
  334. case Type::Bitmap:
  335. // FIXME: Maybe compare bitmaps somehow differently?
  336. return m_value.as_bitmap < other.m_value.as_bitmap;
  337. case Type::Icon:
  338. // FIXME: Maybe compare icons somehow differently?
  339. return m_value.as_icon < other.m_value.as_icon;
  340. case Type::Color:
  341. return m_value.as_color < other.m_value.as_color;
  342. case Type::Point:
  343. case Type::Size:
  344. case Type::Rect:
  345. case Type::Font:
  346. // FIXME: Figure out how to compare these.
  347. ASSERT_NOT_REACHED();
  348. case Type::Invalid:
  349. break;
  350. }
  351. ASSERT_NOT_REACHED();
  352. }
  353. String Variant::to_string() const
  354. {
  355. switch (m_type) {
  356. case Type::Bool:
  357. return as_bool() ? "true" : "false";
  358. case Type::Int32:
  359. return String::number(as_i32());
  360. case Type::Int64:
  361. return String::number(as_i64());
  362. case Type::UnsignedInt:
  363. return String::number(as_uint());
  364. case Type::Float:
  365. return String::format("%.2f", (double)as_float());
  366. case Type::String:
  367. return as_string();
  368. case Type::Bitmap:
  369. return "[Gfx::Bitmap]";
  370. case Type::Icon:
  371. return "[GUI::Icon]";
  372. case Type::Color:
  373. return as_color().to_string();
  374. case Type::Point:
  375. return as_point().to_string();
  376. case Type::Size:
  377. return as_size().to_string();
  378. case Type::Rect:
  379. return as_rect().to_string();
  380. case Type::Font:
  381. return String::format("[Font: %s]", as_font().name().characters());
  382. case Type::Invalid:
  383. return "[null]";
  384. break;
  385. }
  386. ASSERT_NOT_REACHED();
  387. }
  388. }