Variant.cpp 10 KB

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