Variant.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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()
  49. {
  50. Clear();
  51. }
  52. void Variant::Clear()
  53. {
  54. // Free any allocated types.
  55. switch (type)
  56. {
  57. case STRING:
  58. {
  59. // Clean up the string.
  60. String* string = (String*)data;
  61. string->~String();
  62. }
  63. break;
  64. case TRANSFORMREF:
  65. {
  66. // Clean up the transform.
  67. TransformRef* transform = (TransformRef*)data;
  68. transform->~TransformRef();
  69. }
  70. break;
  71. case TRANSITIONLIST:
  72. {
  73. // Clean up the transition list.
  74. TransitionList* transition_list = (TransitionList*)data;
  75. transition_list->~TransitionList();
  76. }
  77. break;
  78. case ANIMATIONLIST:
  79. {
  80. // Clean up the transition list.
  81. AnimationList* animation_list = (AnimationList*)data;
  82. animation_list->~AnimationList();
  83. }
  84. case DECORATORLIST:
  85. {
  86. DecoratorList* decorator_list = (DecoratorList*)data;
  87. decorator_list->~DecoratorList();
  88. }
  89. break;
  90. default:
  91. break;
  92. }
  93. type = NONE;
  94. }
  95. //////////////////////////////////////////////////
  96. // Set methods
  97. //////////////////////////////////////////////////
  98. #define SET_VARIANT(type) *((type*)data) = value;
  99. void Variant::Set(const Variant& copy)
  100. {
  101. switch (copy.type)
  102. {
  103. case STRING:
  104. Set(*(String*)copy.data);
  105. break;
  106. case TRANSFORMREF:
  107. Set(*(TransformRef*)copy.data);
  108. break;
  109. case TRANSITIONLIST:
  110. Set(*(TransitionList*)copy.data);
  111. break;
  112. case ANIMATIONLIST:
  113. Set(*(AnimationList*)copy.data);
  114. break;
  115. case DECORATORLIST:
  116. Set(*(DecoratorList*)copy.data);
  117. break;
  118. default:
  119. Clear();
  120. memcpy(data, copy.data, LOCAL_DATA_SIZE);
  121. break;
  122. }
  123. type = copy.type;
  124. }
  125. void Variant::Set(const byte value)
  126. {
  127. type = BYTE;
  128. SET_VARIANT(byte);
  129. }
  130. void Variant::Set(const char value)
  131. {
  132. type = CHAR;
  133. SET_VARIANT(char);
  134. }
  135. void Variant::Set(const float value)
  136. {
  137. type = FLOAT;
  138. SET_VARIANT(float);
  139. }
  140. void Variant::Set(const int value)
  141. {
  142. type = INT;
  143. SET_VARIANT(int);
  144. }
  145. void Variant::Set(const String& value)
  146. {
  147. if (type == STRING)
  148. {
  149. (*(String*)data) = value;
  150. }
  151. else
  152. {
  153. type = STRING;
  154. new(data) String(value);
  155. }
  156. }
  157. void Variant::Set(const word value)
  158. {
  159. type = WORD;
  160. SET_VARIANT(word);
  161. }
  162. void Variant::Set(const char* value)
  163. {
  164. Set(String(value));
  165. }
  166. void Variant::Set(void* voidptr)
  167. {
  168. type = VOIDPTR;
  169. memcpy(data, &voidptr, sizeof(void*));
  170. }
  171. void Variant::Set(const Vector2f& value)
  172. {
  173. type = VECTOR2;
  174. SET_VARIANT(Vector2f);
  175. }
  176. void Variant::Set(const Vector3f& value)
  177. {
  178. type = VECTOR3;
  179. SET_VARIANT(Vector3f);
  180. }
  181. void Variant::Set(const Vector4f& value)
  182. {
  183. type = VECTOR4;
  184. SET_VARIANT(Vector4f);
  185. }
  186. void Variant::Set(const TransformRef& value)
  187. {
  188. if (type == TRANSFORMREF)
  189. {
  190. SET_VARIANT(TransformRef);
  191. }
  192. else
  193. {
  194. type = TRANSFORMREF;
  195. new(data) TransformRef(value);
  196. }
  197. }
  198. void Variant::Set(const TransitionList& value)
  199. {
  200. if (type == TRANSITIONLIST)
  201. {
  202. *(TransitionList*)data = value;
  203. }
  204. else
  205. {
  206. type = TRANSITIONLIST;
  207. new(data) TransitionList(value);
  208. }
  209. }
  210. void Variant::Set(const AnimationList& value)
  211. {
  212. if (type == ANIMATIONLIST)
  213. {
  214. *(AnimationList*)data = value;
  215. }
  216. else
  217. {
  218. type = ANIMATIONLIST;
  219. new(data) AnimationList(value);
  220. }
  221. }
  222. void Variant::Set(const DecoratorList& value)
  223. {
  224. if (type == DECORATORLIST)
  225. {
  226. *(DecoratorList*)data = value;
  227. }
  228. else
  229. {
  230. type = DECORATORLIST;
  231. new(data) DecoratorList(value);
  232. }
  233. }
  234. void Variant::Set(const Colourf& value)
  235. {
  236. type = COLOURF;
  237. SET_VARIANT(Colourf);
  238. }
  239. void Variant::Set(const Colourb& value)
  240. {
  241. type = COLOURB;
  242. SET_VARIANT(Colourb);
  243. }
  244. void Variant::Set(ScriptInterface* value)
  245. {
  246. type = SCRIPTINTERFACE;
  247. memcpy(data, &value, sizeof(ScriptInterface*));
  248. }
  249. Variant& Variant::operator=(const Variant& copy)
  250. {
  251. Set(copy);
  252. return *this;
  253. }
  254. #define DEFAULT_VARIANT_COMPARE(TYPE) static_cast<TYPE>(*(TYPE*)data) == static_cast<TYPE>(*(TYPE*)other.data)
  255. bool Variant::operator==(const Variant & other) const
  256. {
  257. if (type != other.type)
  258. return false;
  259. switch (type)
  260. {
  261. case BYTE:
  262. return DEFAULT_VARIANT_COMPARE(byte);
  263. case CHAR:
  264. return DEFAULT_VARIANT_COMPARE(char);
  265. case FLOAT:
  266. return DEFAULT_VARIANT_COMPARE(float);
  267. case INT:
  268. return DEFAULT_VARIANT_COMPARE(int);
  269. case STRING:
  270. return DEFAULT_VARIANT_COMPARE(String);
  271. case WORD:
  272. return DEFAULT_VARIANT_COMPARE(word);
  273. case VECTOR2:
  274. return DEFAULT_VARIANT_COMPARE(Vector2f);
  275. case VECTOR3:
  276. return DEFAULT_VARIANT_COMPARE(Vector3f);
  277. case VECTOR4:
  278. return DEFAULT_VARIANT_COMPARE(Vector4f);
  279. case TRANSFORMREF:
  280. return DEFAULT_VARIANT_COMPARE(TransformRef);
  281. case TRANSITIONLIST:
  282. return DEFAULT_VARIANT_COMPARE(TransitionList);
  283. case ANIMATIONLIST:
  284. return DEFAULT_VARIANT_COMPARE(AnimationList);
  285. case DECORATORLIST:
  286. return DEFAULT_VARIANT_COMPARE(DecoratorList);
  287. case COLOURF:
  288. return DEFAULT_VARIANT_COMPARE(Colourf);
  289. case COLOURB:
  290. return DEFAULT_VARIANT_COMPARE(Colourb);
  291. case SCRIPTINTERFACE:
  292. return DEFAULT_VARIANT_COMPARE(ScriptInterface*);
  293. case VOIDPTR:
  294. return DEFAULT_VARIANT_COMPARE(void*);
  295. case NONE:
  296. return true;
  297. break;
  298. }
  299. RMLUI_ERRORMSG("Variant comparison not implemented for this type.");
  300. return false;
  301. }
  302. }
  303. }