Variant.cpp 11 KB

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