Variant.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/Core/Variant.h"
  29. #include "../../Include/RmlUi/Core/DecorationTypes.h"
  30. #include <string.h>
  31. namespace Rml {
  32. Variant::Variant()
  33. {
  34. // Make sure our object size assumptions fit inside the static buffer
  35. static_assert(sizeof(Colourb) <= LOCAL_DATA_SIZE, "Local data too small for Colourb");
  36. static_assert(sizeof(Colourf) <= LOCAL_DATA_SIZE, "Local data too small for Colourf");
  37. static_assert(sizeof(Vector4f) <= LOCAL_DATA_SIZE, "Local data too small for Vector4f");
  38. static_assert(sizeof(String) <= LOCAL_DATA_SIZE, "Local data too small for String");
  39. static_assert(sizeof(TransformPtr) <= LOCAL_DATA_SIZE, "Local data too small for TransformPtr");
  40. static_assert(sizeof(TransitionList) <= LOCAL_DATA_SIZE, "Local data too small for TransitionList");
  41. static_assert(sizeof(AnimationList) <= LOCAL_DATA_SIZE, "Local data too small for AnimationList");
  42. static_assert(sizeof(DecoratorsPtr) <= LOCAL_DATA_SIZE, "Local data too small for DecoratorsPtr");
  43. static_assert(sizeof(FiltersPtr) <= LOCAL_DATA_SIZE, "Local data too small for FiltersPtr");
  44. static_assert(sizeof(FontEffectsPtr) <= LOCAL_DATA_SIZE, "Local data too small for FontEffectsPtr");
  45. static_assert(sizeof(ColorStopList) <= LOCAL_DATA_SIZE, "Local data too small for ColorStopList");
  46. static_assert(sizeof(BoxShadowList) <= LOCAL_DATA_SIZE, "Local data too small for BoxShadowList");
  47. }
  48. Variant::Variant(const Variant& copy)
  49. {
  50. Set(copy);
  51. }
  52. Variant::Variant(Variant&& other) noexcept
  53. {
  54. Set(std::move(other));
  55. }
  56. Variant::~Variant()
  57. {
  58. Clear();
  59. }
  60. void Variant::Clear()
  61. {
  62. // Free any allocated types.
  63. switch (type)
  64. {
  65. case STRING:
  66. {
  67. // Clean up the string.
  68. String* string = (String*)data;
  69. string->~String();
  70. }
  71. break;
  72. case TRANSFORMPTR:
  73. {
  74. // Clean up the transform.
  75. TransformPtr* transform = (TransformPtr*)data;
  76. transform->~TransformPtr();
  77. }
  78. break;
  79. case TRANSITIONLIST:
  80. {
  81. // Clean up the transition list.
  82. TransitionList* transition_list = (TransitionList*)data;
  83. transition_list->~TransitionList();
  84. }
  85. break;
  86. case ANIMATIONLIST:
  87. {
  88. // Clean up the transition list.
  89. AnimationList* animation_list = (AnimationList*)data;
  90. animation_list->~AnimationList();
  91. }
  92. break;
  93. case DECORATORSPTR:
  94. {
  95. DecoratorsPtr* decorators = (DecoratorsPtr*)data;
  96. decorators->~DecoratorsPtr();
  97. }
  98. break;
  99. case FILTERSPTR:
  100. {
  101. FiltersPtr* decorators = (FiltersPtr*)data;
  102. decorators->~FiltersPtr();
  103. }
  104. break;
  105. case FONTEFFECTSPTR:
  106. {
  107. FontEffectsPtr* font_effects = (FontEffectsPtr*)data;
  108. font_effects->~shared_ptr();
  109. }
  110. break;
  111. case COLORSTOPLIST:
  112. {
  113. ColorStopList* value = (ColorStopList*)data;
  114. value->~ColorStopList();
  115. }
  116. break;
  117. case BOXSHADOWLIST:
  118. {
  119. BoxShadowList* value = (BoxShadowList*)data;
  120. value->~BoxShadowList();
  121. }
  122. break;
  123. default: break;
  124. }
  125. type = NONE;
  126. }
  127. #define SET_VARIANT(type) *((type*)data) = value;
  128. void Variant::Set(const Variant& copy)
  129. {
  130. switch (copy.type)
  131. {
  132. case STRING: Set(*reinterpret_cast<const String*>(copy.data)); break;
  133. case TRANSFORMPTR: Set(*reinterpret_cast<const TransformPtr*>(copy.data)); break;
  134. case TRANSITIONLIST: Set(*reinterpret_cast<const TransitionList*>(copy.data)); break;
  135. case ANIMATIONLIST: Set(*reinterpret_cast<const AnimationList*>(copy.data)); break;
  136. case DECORATORSPTR: Set(*reinterpret_cast<const DecoratorsPtr*>(copy.data)); break;
  137. case FILTERSPTR: Set(*reinterpret_cast<const FiltersPtr*>(copy.data)); break;
  138. case FONTEFFECTSPTR: Set(*reinterpret_cast<const FontEffectsPtr*>(copy.data)); break;
  139. case COLORSTOPLIST: Set(*reinterpret_cast<const ColorStopList*>(copy.data)); break;
  140. case BOXSHADOWLIST: Set(*reinterpret_cast<const BoxShadowList*>(copy.data)); break;
  141. default:
  142. memcpy(data, copy.data, LOCAL_DATA_SIZE);
  143. type = copy.type;
  144. break;
  145. }
  146. RMLUI_ASSERT(type == copy.type);
  147. }
  148. void Variant::Set(Variant&& other)
  149. {
  150. switch (other.type)
  151. {
  152. case STRING: Set(std::move(*reinterpret_cast<String*>(other.data))); break;
  153. case TRANSFORMPTR: Set(std::move(*reinterpret_cast<TransformPtr*>(other.data))); break;
  154. case TRANSITIONLIST: Set(std::move(*reinterpret_cast<TransitionList*>(other.data))); break;
  155. case ANIMATIONLIST: Set(std::move(*reinterpret_cast<AnimationList*>(other.data))); break;
  156. case DECORATORSPTR: Set(std::move(*reinterpret_cast<DecoratorsPtr*>(other.data))); break;
  157. case FILTERSPTR: Set(std::move(*reinterpret_cast<FiltersPtr*>(other.data))); break;
  158. case FONTEFFECTSPTR: Set(std::move(*reinterpret_cast<FontEffectsPtr*>(other.data))); break;
  159. case COLORSTOPLIST: Set(std::move(*reinterpret_cast<ColorStopList*>(other.data))); break;
  160. case BOXSHADOWLIST: Set(std::move(*reinterpret_cast<BoxShadowList*>(other.data))); break;
  161. default:
  162. memcpy(data, other.data, LOCAL_DATA_SIZE);
  163. type = other.type;
  164. break;
  165. }
  166. RMLUI_ASSERT(type == other.type);
  167. }
  168. void Variant::Set(const bool value)
  169. {
  170. type = BOOL;
  171. SET_VARIANT(bool);
  172. }
  173. void Variant::Set(const byte value)
  174. {
  175. type = BYTE;
  176. SET_VARIANT(byte);
  177. }
  178. void Variant::Set(const char value)
  179. {
  180. type = CHAR;
  181. SET_VARIANT(char);
  182. }
  183. void Variant::Set(const float value)
  184. {
  185. type = FLOAT;
  186. SET_VARIANT(float);
  187. }
  188. void Variant::Set(const double value)
  189. {
  190. type = DOUBLE;
  191. SET_VARIANT(double);
  192. }
  193. void Variant::Set(const int value)
  194. {
  195. type = INT;
  196. SET_VARIANT(int);
  197. }
  198. void Variant::Set(const int64_t value)
  199. {
  200. type = INT64;
  201. SET_VARIANT(int64_t);
  202. }
  203. void Variant::Set(const unsigned int value)
  204. {
  205. type = UINT;
  206. SET_VARIANT(unsigned int);
  207. }
  208. void Variant::Set(const uint64_t value)
  209. {
  210. type = UINT64;
  211. SET_VARIANT(uint64_t);
  212. }
  213. void Variant::Set(const char* value)
  214. {
  215. Set(String(value));
  216. }
  217. void Variant::Set(void* voidptr)
  218. {
  219. type = VOIDPTR;
  220. memcpy(data, &voidptr, sizeof(void*));
  221. }
  222. void Variant::Set(const Vector2f value)
  223. {
  224. type = VECTOR2;
  225. SET_VARIANT(Vector2f);
  226. }
  227. void Variant::Set(const Vector3f value)
  228. {
  229. type = VECTOR3;
  230. SET_VARIANT(Vector3f);
  231. }
  232. void Variant::Set(const Vector4f value)
  233. {
  234. type = VECTOR4;
  235. SET_VARIANT(Vector4f);
  236. }
  237. void Variant::Set(const Colourf value)
  238. {
  239. type = COLOURF;
  240. SET_VARIANT(Colourf);
  241. }
  242. void Variant::Set(const Colourb value)
  243. {
  244. type = COLOURB;
  245. SET_VARIANT(Colourb);
  246. }
  247. void Variant::Set(ScriptInterface* value)
  248. {
  249. type = SCRIPTINTERFACE;
  250. memcpy(data, &value, sizeof(ScriptInterface*));
  251. }
  252. void Variant::Set(const String& value)
  253. {
  254. if (type == STRING)
  255. {
  256. (*(String*)data) = value;
  257. }
  258. else
  259. {
  260. type = STRING;
  261. new (data) String(value);
  262. }
  263. }
  264. void Variant::Set(String&& value)
  265. {
  266. if (type == STRING)
  267. {
  268. (*(String*)data) = std::move(value);
  269. }
  270. else
  271. {
  272. type = STRING;
  273. new (data) String(std::move(value));
  274. }
  275. }
  276. void Variant::Set(const TransformPtr& value)
  277. {
  278. if (type == TRANSFORMPTR)
  279. {
  280. SET_VARIANT(TransformPtr);
  281. }
  282. else
  283. {
  284. type = TRANSFORMPTR;
  285. new (data) TransformPtr(value);
  286. }
  287. }
  288. void Variant::Set(TransformPtr&& value)
  289. {
  290. if (type == TRANSFORMPTR)
  291. {
  292. (*(TransformPtr*)data) = std::move(value);
  293. }
  294. else
  295. {
  296. type = TRANSFORMPTR;
  297. new (data) TransformPtr(std::move(value));
  298. }
  299. }
  300. void Variant::Set(const TransitionList& value)
  301. {
  302. if (type == TRANSITIONLIST)
  303. {
  304. *(TransitionList*)data = value;
  305. }
  306. else
  307. {
  308. type = TRANSITIONLIST;
  309. new (data) TransitionList(value);
  310. }
  311. }
  312. void Variant::Set(TransitionList&& value)
  313. {
  314. if (type == TRANSITIONLIST)
  315. {
  316. (*(TransitionList*)data) = std::move(value);
  317. }
  318. else
  319. {
  320. type = TRANSITIONLIST;
  321. new (data) TransitionList(std::move(value));
  322. }
  323. }
  324. void Variant::Set(const AnimationList& value)
  325. {
  326. if (type == ANIMATIONLIST)
  327. {
  328. *(AnimationList*)data = value;
  329. }
  330. else
  331. {
  332. type = ANIMATIONLIST;
  333. new (data) AnimationList(value);
  334. }
  335. }
  336. void Variant::Set(AnimationList&& value)
  337. {
  338. if (type == ANIMATIONLIST)
  339. {
  340. (*(AnimationList*)data) = std::move(value);
  341. }
  342. else
  343. {
  344. type = ANIMATIONLIST;
  345. new (data) AnimationList(std::move(value));
  346. }
  347. }
  348. void Variant::Set(const DecoratorsPtr& value)
  349. {
  350. if (type == DECORATORSPTR)
  351. {
  352. *(DecoratorsPtr*)data = value;
  353. }
  354. else
  355. {
  356. type = DECORATORSPTR;
  357. new (data) DecoratorsPtr(value);
  358. }
  359. }
  360. void Variant::Set(DecoratorsPtr&& value)
  361. {
  362. if (type == DECORATORSPTR)
  363. {
  364. (*(DecoratorsPtr*)data) = std::move(value);
  365. }
  366. else
  367. {
  368. type = DECORATORSPTR;
  369. new (data) DecoratorsPtr(std::move(value));
  370. }
  371. }
  372. void Variant::Set(const FiltersPtr& value)
  373. {
  374. if (type == FILTERSPTR)
  375. {
  376. *(FiltersPtr*)data = value;
  377. }
  378. else
  379. {
  380. type = FILTERSPTR;
  381. new (data) FiltersPtr(value);
  382. }
  383. }
  384. void Variant::Set(FiltersPtr&& value)
  385. {
  386. if (type == FILTERSPTR)
  387. {
  388. (*(FiltersPtr*)data) = std::move(value);
  389. }
  390. else
  391. {
  392. type = FILTERSPTR;
  393. new (data) FiltersPtr(std::move(value));
  394. }
  395. }
  396. void Variant::Set(const FontEffectsPtr& value)
  397. {
  398. if (type == FONTEFFECTSPTR)
  399. {
  400. *(FontEffectsPtr*)data = value;
  401. }
  402. else
  403. {
  404. type = FONTEFFECTSPTR;
  405. new (data) FontEffectsPtr(value);
  406. }
  407. }
  408. void Variant::Set(FontEffectsPtr&& value)
  409. {
  410. if (type == FONTEFFECTSPTR)
  411. {
  412. (*(FontEffectsPtr*)data) = std::move(value);
  413. }
  414. else
  415. {
  416. type = FONTEFFECTSPTR;
  417. new (data) FontEffectsPtr(std::move(value));
  418. }
  419. }
  420. void Variant::Set(const ColorStopList& value)
  421. {
  422. if (type == COLORSTOPLIST)
  423. {
  424. *(ColorStopList*)data = value;
  425. }
  426. else
  427. {
  428. type = COLORSTOPLIST;
  429. new (data) ColorStopList(value);
  430. }
  431. }
  432. void Variant::Set(ColorStopList&& value)
  433. {
  434. if (type == COLORSTOPLIST)
  435. {
  436. (*(ColorStopList*)data) = std::move(value);
  437. }
  438. else
  439. {
  440. type = COLORSTOPLIST;
  441. new (data) ColorStopList(std::move(value));
  442. }
  443. }
  444. void Variant::Set(const BoxShadowList& value)
  445. {
  446. if (type == BOXSHADOWLIST)
  447. {
  448. *(BoxShadowList*)data = value;
  449. }
  450. else
  451. {
  452. type = BOXSHADOWLIST;
  453. new (data) BoxShadowList(value);
  454. }
  455. }
  456. void Variant::Set(BoxShadowList&& value)
  457. {
  458. if (type == BOXSHADOWLIST)
  459. {
  460. (*(BoxShadowList*)data) = std::move(value);
  461. }
  462. else
  463. {
  464. type = BOXSHADOWLIST;
  465. new (data) BoxShadowList(std::move(value));
  466. }
  467. }
  468. Variant& Variant::operator=(const Variant& copy)
  469. {
  470. if (this == &copy)
  471. return *this;
  472. if (copy.type != type)
  473. Clear();
  474. Set(copy);
  475. return *this;
  476. }
  477. Variant& Variant::operator=(Variant&& other) noexcept
  478. {
  479. if (this == &other)
  480. return *this;
  481. if (other.type != type)
  482. Clear();
  483. Set(std::move(other));
  484. return *this;
  485. }
  486. #define DEFAULT_VARIANT_COMPARE(TYPE) static_cast<TYPE>(*(TYPE*)data) == static_cast<TYPE>(*(TYPE*)other.data)
  487. bool Variant::operator==(const Variant& other) const
  488. {
  489. if (type != other.type)
  490. return false;
  491. switch (type)
  492. {
  493. case BOOL: return DEFAULT_VARIANT_COMPARE(bool);
  494. case BYTE: return DEFAULT_VARIANT_COMPARE(byte);
  495. case CHAR: return DEFAULT_VARIANT_COMPARE(char);
  496. case FLOAT: return DEFAULT_VARIANT_COMPARE(float);
  497. case DOUBLE: return DEFAULT_VARIANT_COMPARE(double);
  498. case INT: return DEFAULT_VARIANT_COMPARE(int);
  499. case INT64: return DEFAULT_VARIANT_COMPARE(int64_t);
  500. case UINT: return DEFAULT_VARIANT_COMPARE(unsigned int);
  501. case UINT64: return DEFAULT_VARIANT_COMPARE(uint64_t);
  502. case STRING: return DEFAULT_VARIANT_COMPARE(String);
  503. case VECTOR2: return DEFAULT_VARIANT_COMPARE(Vector2f);
  504. case VECTOR3: return DEFAULT_VARIANT_COMPARE(Vector3f);
  505. case VECTOR4: return DEFAULT_VARIANT_COMPARE(Vector4f);
  506. case COLOURF: return DEFAULT_VARIANT_COMPARE(Colourf);
  507. case COLOURB: return DEFAULT_VARIANT_COMPARE(Colourb);
  508. case SCRIPTINTERFACE: return DEFAULT_VARIANT_COMPARE(ScriptInterface*);
  509. case VOIDPTR: return DEFAULT_VARIANT_COMPARE(void*);
  510. case TRANSFORMPTR: return DEFAULT_VARIANT_COMPARE(TransformPtr);
  511. case TRANSITIONLIST: return DEFAULT_VARIANT_COMPARE(TransitionList);
  512. case ANIMATIONLIST: return DEFAULT_VARIANT_COMPARE(AnimationList);
  513. case DECORATORSPTR: return DEFAULT_VARIANT_COMPARE(DecoratorsPtr);
  514. case FILTERSPTR: return DEFAULT_VARIANT_COMPARE(FiltersPtr);
  515. case FONTEFFECTSPTR: return DEFAULT_VARIANT_COMPARE(FontEffectsPtr);
  516. case COLORSTOPLIST: return DEFAULT_VARIANT_COMPARE(ColorStopList);
  517. case BOXSHADOWLIST: return DEFAULT_VARIANT_COMPARE(BoxShadowList);
  518. case NONE: return true;
  519. }
  520. RMLUI_ERRORMSG("Variant comparison not implemented for this type.");
  521. return false;
  522. }
  523. } // namespace Rml