Variant.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. }
  46. Variant::Variant(const Variant& copy)
  47. {
  48. Set(copy);
  49. }
  50. Variant::Variant(Variant&& other) noexcept
  51. {
  52. Set(std::move(other));
  53. }
  54. Variant::~Variant()
  55. {
  56. Clear();
  57. }
  58. void Variant::Clear()
  59. {
  60. // Free any allocated types.
  61. switch (type)
  62. {
  63. case STRING:
  64. {
  65. // Clean up the string.
  66. String* string = (String*)data;
  67. string->~String();
  68. }
  69. break;
  70. case TRANSFORMPTR:
  71. {
  72. // Clean up the transform.
  73. TransformPtr* transform = (TransformPtr*)data;
  74. transform->~TransformPtr();
  75. }
  76. break;
  77. case TRANSITIONLIST:
  78. {
  79. // Clean up the transition list.
  80. TransitionList* transition_list = (TransitionList*)data;
  81. transition_list->~TransitionList();
  82. }
  83. break;
  84. case ANIMATIONLIST:
  85. {
  86. // Clean up the transition list.
  87. AnimationList* animation_list = (AnimationList*)data;
  88. animation_list->~AnimationList();
  89. }
  90. break;
  91. case DECORATORSPTR:
  92. {
  93. DecoratorsPtr* decorators = (DecoratorsPtr*)data;
  94. decorators->~DecoratorsPtr();
  95. }
  96. break;
  97. case FILTERSPTR:
  98. {
  99. FiltersPtr* decorators = (FiltersPtr*)data;
  100. decorators->~FiltersPtr();
  101. }
  102. break;
  103. case FONTEFFECTSPTR:
  104. {
  105. FontEffectsPtr* font_effects = (FontEffectsPtr*)data;
  106. font_effects->~shared_ptr();
  107. }
  108. break;
  109. case BOXSHADOWLIST:
  110. {
  111. BoxShadowList* value = (BoxShadowList*)data;
  112. value->~BoxShadowList();
  113. }
  114. break;
  115. default: break;
  116. }
  117. type = NONE;
  118. }
  119. #define SET_VARIANT(type) *((type*)data) = value;
  120. void Variant::Set(const Variant& copy)
  121. {
  122. switch (copy.type)
  123. {
  124. case STRING: Set(*reinterpret_cast<const String*>(copy.data)); break;
  125. case TRANSFORMPTR: Set(*reinterpret_cast<const TransformPtr*>(copy.data)); break;
  126. case TRANSITIONLIST: Set(*reinterpret_cast<const TransitionList*>(copy.data)); break;
  127. case ANIMATIONLIST: Set(*reinterpret_cast<const AnimationList*>(copy.data)); break;
  128. case DECORATORSPTR: Set(*reinterpret_cast<const DecoratorsPtr*>(copy.data)); break;
  129. case FILTERSPTR: Set(*reinterpret_cast<const FiltersPtr*>(copy.data)); break;
  130. case FONTEFFECTSPTR: Set(*reinterpret_cast<const FontEffectsPtr*>(copy.data)); break;
  131. case BOXSHADOWLIST: Set(*reinterpret_cast<const BoxShadowList*>(copy.data)); break;
  132. default:
  133. memcpy(data, copy.data, LOCAL_DATA_SIZE);
  134. type = copy.type;
  135. break;
  136. }
  137. RMLUI_ASSERT(type == copy.type);
  138. }
  139. void Variant::Set(Variant&& other)
  140. {
  141. switch (other.type)
  142. {
  143. case STRING: Set(std::move(*reinterpret_cast<String*>(other.data))); break;
  144. case TRANSFORMPTR: Set(std::move(*reinterpret_cast<TransformPtr*>(other.data))); break;
  145. case TRANSITIONLIST: Set(std::move(*reinterpret_cast<TransitionList*>(other.data))); break;
  146. case ANIMATIONLIST: Set(std::move(*reinterpret_cast<AnimationList*>(other.data))); break;
  147. case DECORATORSPTR: Set(std::move(*reinterpret_cast<DecoratorsPtr*>(other.data))); break;
  148. case FILTERSPTR: Set(std::move(*reinterpret_cast<FiltersPtr*>(other.data))); break;
  149. case FONTEFFECTSPTR: Set(std::move(*reinterpret_cast<FontEffectsPtr*>(other.data))); break;
  150. case BOXSHADOWLIST: Set(std::move(*reinterpret_cast<BoxShadowList*>(other.data))); break;
  151. default:
  152. memcpy(data, other.data, LOCAL_DATA_SIZE);
  153. type = other.type;
  154. break;
  155. }
  156. RMLUI_ASSERT(type == other.type);
  157. }
  158. void Variant::Set(const bool value)
  159. {
  160. type = BOOL;
  161. SET_VARIANT(bool);
  162. }
  163. void Variant::Set(const byte value)
  164. {
  165. type = BYTE;
  166. SET_VARIANT(byte);
  167. }
  168. void Variant::Set(const char value)
  169. {
  170. type = CHAR;
  171. SET_VARIANT(char);
  172. }
  173. void Variant::Set(const float value)
  174. {
  175. type = FLOAT;
  176. SET_VARIANT(float);
  177. }
  178. void Variant::Set(const double value)
  179. {
  180. type = DOUBLE;
  181. SET_VARIANT(double);
  182. }
  183. void Variant::Set(const int value)
  184. {
  185. type = INT;
  186. SET_VARIANT(int);
  187. }
  188. void Variant::Set(const int64_t value)
  189. {
  190. type = INT64;
  191. SET_VARIANT(int64_t);
  192. }
  193. void Variant::Set(const unsigned int value)
  194. {
  195. type = UINT;
  196. SET_VARIANT(unsigned int);
  197. }
  198. void Variant::Set(const uint64_t value)
  199. {
  200. type = UINT64;
  201. SET_VARIANT(uint64_t);
  202. }
  203. void Variant::Set(const char* value)
  204. {
  205. Set(String(value));
  206. }
  207. void Variant::Set(void* voidptr)
  208. {
  209. type = VOIDPTR;
  210. memcpy(data, &voidptr, sizeof(void*));
  211. }
  212. void Variant::Set(const Vector2f value)
  213. {
  214. type = VECTOR2;
  215. SET_VARIANT(Vector2f);
  216. }
  217. void Variant::Set(const Vector3f value)
  218. {
  219. type = VECTOR3;
  220. SET_VARIANT(Vector3f);
  221. }
  222. void Variant::Set(const Vector4f value)
  223. {
  224. type = VECTOR4;
  225. SET_VARIANT(Vector4f);
  226. }
  227. void Variant::Set(const Colourf value)
  228. {
  229. type = COLOURF;
  230. SET_VARIANT(Colourf);
  231. }
  232. void Variant::Set(const Colourb value)
  233. {
  234. type = COLOURB;
  235. SET_VARIANT(Colourb);
  236. }
  237. void Variant::Set(ScriptInterface* value)
  238. {
  239. type = SCRIPTINTERFACE;
  240. memcpy(data, &value, sizeof(ScriptInterface*));
  241. }
  242. void Variant::Set(const String& value)
  243. {
  244. if (type == STRING)
  245. {
  246. (*(String*)data) = value;
  247. }
  248. else
  249. {
  250. type = STRING;
  251. new (data) String(value);
  252. }
  253. }
  254. void Variant::Set(String&& value)
  255. {
  256. if (type == STRING)
  257. {
  258. (*(String*)data) = std::move(value);
  259. }
  260. else
  261. {
  262. type = STRING;
  263. new (data) String(std::move(value));
  264. }
  265. }
  266. void Variant::Set(const TransformPtr& value)
  267. {
  268. if (type == TRANSFORMPTR)
  269. {
  270. SET_VARIANT(TransformPtr);
  271. }
  272. else
  273. {
  274. type = TRANSFORMPTR;
  275. new (data) TransformPtr(value);
  276. }
  277. }
  278. void Variant::Set(TransformPtr&& value)
  279. {
  280. if (type == TRANSFORMPTR)
  281. {
  282. (*(TransformPtr*)data) = std::move(value);
  283. }
  284. else
  285. {
  286. type = TRANSFORMPTR;
  287. new (data) TransformPtr(std::move(value));
  288. }
  289. }
  290. void Variant::Set(const TransitionList& value)
  291. {
  292. if (type == TRANSITIONLIST)
  293. {
  294. *(TransitionList*)data = value;
  295. }
  296. else
  297. {
  298. type = TRANSITIONLIST;
  299. new (data) TransitionList(value);
  300. }
  301. }
  302. void Variant::Set(TransitionList&& value)
  303. {
  304. if (type == TRANSITIONLIST)
  305. {
  306. (*(TransitionList*)data) = std::move(value);
  307. }
  308. else
  309. {
  310. type = TRANSITIONLIST;
  311. new (data) TransitionList(std::move(value));
  312. }
  313. }
  314. void Variant::Set(const AnimationList& value)
  315. {
  316. if (type == ANIMATIONLIST)
  317. {
  318. *(AnimationList*)data = value;
  319. }
  320. else
  321. {
  322. type = ANIMATIONLIST;
  323. new (data) AnimationList(value);
  324. }
  325. }
  326. void Variant::Set(AnimationList&& value)
  327. {
  328. if (type == ANIMATIONLIST)
  329. {
  330. (*(AnimationList*)data) = std::move(value);
  331. }
  332. else
  333. {
  334. type = ANIMATIONLIST;
  335. new (data) AnimationList(std::move(value));
  336. }
  337. }
  338. void Variant::Set(const DecoratorsPtr& value)
  339. {
  340. if (type == DECORATORSPTR)
  341. {
  342. *(DecoratorsPtr*)data = value;
  343. }
  344. else
  345. {
  346. type = DECORATORSPTR;
  347. new (data) DecoratorsPtr(value);
  348. }
  349. }
  350. void Variant::Set(DecoratorsPtr&& value)
  351. {
  352. if (type == DECORATORSPTR)
  353. {
  354. (*(DecoratorsPtr*)data) = std::move(value);
  355. }
  356. else
  357. {
  358. type = DECORATORSPTR;
  359. new (data) DecoratorsPtr(std::move(value));
  360. }
  361. }
  362. void Variant::Set(const FiltersPtr& value)
  363. {
  364. if (type == FILTERSPTR)
  365. {
  366. *(FiltersPtr*)data = value;
  367. }
  368. else
  369. {
  370. type = FILTERSPTR;
  371. new (data) FiltersPtr(value);
  372. }
  373. }
  374. void Variant::Set(FiltersPtr&& value)
  375. {
  376. if (type == FILTERSPTR)
  377. {
  378. (*(FiltersPtr*)data) = std::move(value);
  379. }
  380. else
  381. {
  382. type = FILTERSPTR;
  383. new (data) FiltersPtr(std::move(value));
  384. }
  385. }
  386. void Variant::Set(const FontEffectsPtr& value)
  387. {
  388. if (type == FONTEFFECTSPTR)
  389. {
  390. *(FontEffectsPtr*)data = value;
  391. }
  392. else
  393. {
  394. type = FONTEFFECTSPTR;
  395. new (data) FontEffectsPtr(value);
  396. }
  397. }
  398. void Variant::Set(FontEffectsPtr&& value)
  399. {
  400. if (type == FONTEFFECTSPTR)
  401. {
  402. (*(FontEffectsPtr*)data) = std::move(value);
  403. }
  404. else
  405. {
  406. type = FONTEFFECTSPTR;
  407. new (data) FontEffectsPtr(std::move(value));
  408. }
  409. }
  410. void Variant::Set(const BoxShadowList& value)
  411. {
  412. if (type == BOXSHADOWLIST)
  413. {
  414. *(BoxShadowList*)data = value;
  415. }
  416. else
  417. {
  418. type = BOXSHADOWLIST;
  419. new (data) BoxShadowList(value);
  420. }
  421. }
  422. void Variant::Set(BoxShadowList&& value)
  423. {
  424. if (type == BOXSHADOWLIST)
  425. {
  426. (*(BoxShadowList*)data) = std::move(value);
  427. }
  428. else
  429. {
  430. type = BOXSHADOWLIST;
  431. new (data) BoxShadowList(std::move(value));
  432. }
  433. }
  434. Variant& Variant::operator=(const Variant& copy)
  435. {
  436. if (this == &copy)
  437. return *this;
  438. if (copy.type != type)
  439. Clear();
  440. Set(copy);
  441. return *this;
  442. }
  443. Variant& Variant::operator=(Variant&& other) noexcept
  444. {
  445. if (this == &other)
  446. return *this;
  447. if (other.type != type)
  448. Clear();
  449. Set(std::move(other));
  450. return *this;
  451. }
  452. #define DEFAULT_VARIANT_COMPARE(TYPE) static_cast<TYPE>(*(TYPE*)data) == static_cast<TYPE>(*(TYPE*)other.data)
  453. bool Variant::operator==(const Variant& other) const
  454. {
  455. if (type != other.type)
  456. return false;
  457. switch (type)
  458. {
  459. case BOOL: return DEFAULT_VARIANT_COMPARE(bool);
  460. case BYTE: return DEFAULT_VARIANT_COMPARE(byte);
  461. case CHAR: return DEFAULT_VARIANT_COMPARE(char);
  462. case FLOAT: return DEFAULT_VARIANT_COMPARE(float);
  463. case DOUBLE: return DEFAULT_VARIANT_COMPARE(double);
  464. case INT: return DEFAULT_VARIANT_COMPARE(int);
  465. case INT64: return DEFAULT_VARIANT_COMPARE(int64_t);
  466. case UINT: return DEFAULT_VARIANT_COMPARE(unsigned int);
  467. case UINT64: return DEFAULT_VARIANT_COMPARE(uint64_t);
  468. case STRING: return DEFAULT_VARIANT_COMPARE(String);
  469. case VECTOR2: return DEFAULT_VARIANT_COMPARE(Vector2f);
  470. case VECTOR3: return DEFAULT_VARIANT_COMPARE(Vector3f);
  471. case VECTOR4: return DEFAULT_VARIANT_COMPARE(Vector4f);
  472. case COLOURF: return DEFAULT_VARIANT_COMPARE(Colourf);
  473. case COLOURB: return DEFAULT_VARIANT_COMPARE(Colourb);
  474. case SCRIPTINTERFACE: return DEFAULT_VARIANT_COMPARE(ScriptInterface*);
  475. case VOIDPTR: return DEFAULT_VARIANT_COMPARE(void*);
  476. case TRANSFORMPTR: return DEFAULT_VARIANT_COMPARE(TransformPtr);
  477. case TRANSITIONLIST: return DEFAULT_VARIANT_COMPARE(TransitionList);
  478. case ANIMATIONLIST: return DEFAULT_VARIANT_COMPARE(AnimationList);
  479. case DECORATORSPTR: return DEFAULT_VARIANT_COMPARE(DecoratorsPtr);
  480. case FILTERSPTR: return DEFAULT_VARIANT_COMPARE(FiltersPtr);
  481. case FONTEFFECTSPTR: return DEFAULT_VARIANT_COMPARE(FontEffectsPtr);
  482. case BOXSHADOWLIST: return DEFAULT_VARIANT_COMPARE(BoxShadowList);
  483. case NONE: return true;
  484. }
  485. RMLUI_ERRORMSG("Variant comparison not implemented for this type.");
  486. return false;
  487. }
  488. } // namespace Rml