Variant.cpp 9.8 KB

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