GVariant.cpp 11 KB

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