Variant.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 "precompiled.h"
  29. #include "../../Include/RmlUi/Core/Variant.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(TransformRef) <= LOCAL_DATA_SIZE, "Local data too small for TransformRef");
  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(DecoratorList) <= LOCAL_DATA_SIZE, "Local data too small for DecoratorList");
  43. }
  44. Variant::Variant(const Variant& copy) : type(NONE)
  45. {
  46. Set(copy);
  47. }
  48. Variant::Variant(Variant&& other) : 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 TRANSFORMREF:
  69. {
  70. // Clean up the transform.
  71. TransformRef* transform = (TransformRef*)data;
  72. transform->~TransformRef();
  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. case DECORATORLIST:
  89. {
  90. DecoratorList* decorator_list = (DecoratorList*)data;
  91. decorator_list->~DecoratorList();
  92. }
  93. break;
  94. default:
  95. break;
  96. }
  97. type = NONE;
  98. }
  99. //////////////////////////////////////////////////
  100. // Set methods
  101. //////////////////////////////////////////////////
  102. #define SET_VARIANT(type) *((type*)data) = value;
  103. void Variant::Set(const Variant& copy)
  104. {
  105. switch (copy.type)
  106. {
  107. case STRING:
  108. Set(*(String*)copy.data);
  109. break;
  110. case TRANSFORMREF:
  111. Set(*(TransformRef*)copy.data);
  112. break;
  113. case TRANSITIONLIST:
  114. Set(*(TransitionList*)copy.data);
  115. break;
  116. case ANIMATIONLIST:
  117. Set(*(AnimationList*)copy.data);
  118. break;
  119. case DECORATORLIST:
  120. Set(*(DecoratorList*)copy.data);
  121. break;
  122. default:
  123. memcpy(data, copy.data, LOCAL_DATA_SIZE);
  124. type = copy.type;
  125. break;
  126. }
  127. RMLUI_ASSERT(type == copy.type);
  128. }
  129. void Variant::Set(Variant&& other)
  130. {
  131. switch (other.type)
  132. {
  133. case STRING:
  134. Set(std::move(*(String*)other.data));
  135. break;
  136. case TRANSFORMREF:
  137. Set(std::move(*(TransformRef*)other.data));
  138. break;
  139. case TRANSITIONLIST:
  140. Set(std::move(*(TransitionList*)other.data));
  141. break;
  142. case ANIMATIONLIST:
  143. Set(std::move(*(AnimationList*)other.data));
  144. break;
  145. case DECORATORLIST:
  146. Set(std::move(*(DecoratorList*)other.data));
  147. break;
  148. default:
  149. memcpy(data, other.data, LOCAL_DATA_SIZE);
  150. type = other.type;
  151. break;
  152. }
  153. RMLUI_ASSERT(type == other.type);
  154. }
  155. void Variant::Set(const byte value)
  156. {
  157. type = BYTE;
  158. SET_VARIANT(byte);
  159. }
  160. void Variant::Set(const char value)
  161. {
  162. type = CHAR;
  163. SET_VARIANT(char);
  164. }
  165. void Variant::Set(const float value)
  166. {
  167. type = FLOAT;
  168. SET_VARIANT(float);
  169. }
  170. void Variant::Set(const int value)
  171. {
  172. type = INT;
  173. SET_VARIANT(int);
  174. }
  175. void Variant::Set(const word value)
  176. {
  177. type = WORD;
  178. SET_VARIANT(word);
  179. }
  180. void Variant::Set(const char* value)
  181. {
  182. Set(String(value));
  183. }
  184. void Variant::Set(void* voidptr)
  185. {
  186. type = VOIDPTR;
  187. memcpy(data, &voidptr, sizeof(void*));
  188. }
  189. void Variant::Set(const Vector2f& value)
  190. {
  191. type = VECTOR2;
  192. SET_VARIANT(Vector2f);
  193. }
  194. void Variant::Set(const Vector3f& value)
  195. {
  196. type = VECTOR3;
  197. SET_VARIANT(Vector3f);
  198. }
  199. void Variant::Set(const Vector4f& value)
  200. {
  201. type = VECTOR4;
  202. SET_VARIANT(Vector4f);
  203. }
  204. void Variant::Set(const Colourf& value)
  205. {
  206. type = COLOURF;
  207. SET_VARIANT(Colourf);
  208. }
  209. void Variant::Set(const Colourb& value)
  210. {
  211. type = COLOURB;
  212. SET_VARIANT(Colourb);
  213. }
  214. void Variant::Set(ScriptInterface* value)
  215. {
  216. type = SCRIPTINTERFACE;
  217. memcpy(data, &value, sizeof(ScriptInterface*));
  218. }
  219. void Variant::Set(const String& value)
  220. {
  221. if (type == STRING)
  222. {
  223. (*(String*)data) = value;
  224. }
  225. else
  226. {
  227. type = STRING;
  228. new(data) String(value);
  229. }
  230. }
  231. void Variant::Set(String&& value)
  232. {
  233. if (type == STRING)
  234. {
  235. (*(String*)data) = std::move(value);
  236. }
  237. else
  238. {
  239. type = STRING;
  240. new(data) String(std::move(value));
  241. }
  242. }
  243. void Variant::Set(const TransformRef& value)
  244. {
  245. if (type == TRANSFORMREF)
  246. {
  247. SET_VARIANT(TransformRef);
  248. }
  249. else
  250. {
  251. type = TRANSFORMREF;
  252. new(data) TransformRef(value);
  253. }
  254. }
  255. void Variant::Set(TransformRef&& value)
  256. {
  257. if (type == TRANSFORMREF)
  258. {
  259. (*(TransformRef*)data) = std::move(value);
  260. }
  261. else
  262. {
  263. type = TRANSFORMREF;
  264. new(data) TransformRef(std::move(value));
  265. }
  266. }
  267. void Variant::Set(const TransitionList& value)
  268. {
  269. if (type == TRANSITIONLIST)
  270. {
  271. *(TransitionList*)data = value;
  272. }
  273. else
  274. {
  275. type = TRANSITIONLIST;
  276. new(data) TransitionList(value);
  277. }
  278. }
  279. void Variant::Set(TransitionList&& value)
  280. {
  281. if (type == TRANSITIONLIST)
  282. {
  283. (*(TransitionList*)data) = std::move(value);
  284. }
  285. else
  286. {
  287. type = TRANSITIONLIST;
  288. new(data) TransitionList(std::move(value));
  289. }
  290. }
  291. void Variant::Set(const AnimationList& value)
  292. {
  293. if (type == ANIMATIONLIST)
  294. {
  295. *(AnimationList*)data = value;
  296. }
  297. else
  298. {
  299. type = ANIMATIONLIST;
  300. new(data) AnimationList(value);
  301. }
  302. }
  303. void Variant::Set(AnimationList&& value)
  304. {
  305. if (type == ANIMATIONLIST)
  306. {
  307. (*(AnimationList*)data) = std::move(value);
  308. }
  309. else
  310. {
  311. type = ANIMATIONLIST;
  312. new(data) AnimationList(std::move(value));
  313. }
  314. }
  315. void Variant::Set(const DecoratorList& value)
  316. {
  317. if (type == DECORATORLIST)
  318. {
  319. *(DecoratorList*)data = value;
  320. }
  321. else
  322. {
  323. type = DECORATORLIST;
  324. new(data) DecoratorList(value);
  325. }
  326. }
  327. void Variant::Set(DecoratorList&& value)
  328. {
  329. if (type == DECORATORLIST)
  330. {
  331. (*(DecoratorList*)data) = std::move(value);
  332. }
  333. else
  334. {
  335. type = DECORATORLIST;
  336. new(data) DecoratorList(std::move(value));
  337. }
  338. }
  339. Variant& Variant::operator=(const Variant& copy)
  340. {
  341. if (copy.type != type)
  342. Clear();
  343. Set(copy);
  344. return *this;
  345. }
  346. Variant& Variant::operator=(Variant&& other)
  347. {
  348. if (other.type != type)
  349. Clear();
  350. Set(std::move(other));
  351. return *this;
  352. }
  353. #define DEFAULT_VARIANT_COMPARE(TYPE) static_cast<TYPE>(*(TYPE*)data) == static_cast<TYPE>(*(TYPE*)other.data)
  354. bool Variant::operator==(const Variant & other) const
  355. {
  356. if (type != other.type)
  357. return false;
  358. switch (type)
  359. {
  360. case BYTE:
  361. return DEFAULT_VARIANT_COMPARE(byte);
  362. case CHAR:
  363. return DEFAULT_VARIANT_COMPARE(char);
  364. case FLOAT:
  365. return DEFAULT_VARIANT_COMPARE(float);
  366. case INT:
  367. return DEFAULT_VARIANT_COMPARE(int);
  368. case STRING:
  369. return DEFAULT_VARIANT_COMPARE(String);
  370. case WORD:
  371. return DEFAULT_VARIANT_COMPARE(word);
  372. case VECTOR2:
  373. return DEFAULT_VARIANT_COMPARE(Vector2f);
  374. case VECTOR3:
  375. return DEFAULT_VARIANT_COMPARE(Vector3f);
  376. case VECTOR4:
  377. return DEFAULT_VARIANT_COMPARE(Vector4f);
  378. case TRANSFORMREF:
  379. return DEFAULT_VARIANT_COMPARE(TransformRef);
  380. case TRANSITIONLIST:
  381. return DEFAULT_VARIANT_COMPARE(TransitionList);
  382. case ANIMATIONLIST:
  383. return DEFAULT_VARIANT_COMPARE(AnimationList);
  384. case DECORATORLIST:
  385. return DEFAULT_VARIANT_COMPARE(DecoratorList);
  386. case COLOURF:
  387. return DEFAULT_VARIANT_COMPARE(Colourf);
  388. case COLOURB:
  389. return DEFAULT_VARIANT_COMPARE(Colourb);
  390. case SCRIPTINTERFACE:
  391. return DEFAULT_VARIANT_COMPARE(ScriptInterface*);
  392. case VOIDPTR:
  393. return DEFAULT_VARIANT_COMPARE(void*);
  394. case NONE:
  395. return true;
  396. break;
  397. }
  398. RMLUI_ERRORMSG("Variant comparison not implemented for this type.");
  399. return false;
  400. }
  401. }
  402. }