Variant.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. memcpy(data, copy.data, LOCAL_DATA_SIZE);
  120. break;
  121. }
  122. type = copy.type;
  123. }
  124. void Variant::Set(const byte value)
  125. {
  126. type = BYTE;
  127. SET_VARIANT(byte);
  128. }
  129. void Variant::Set(const char value)
  130. {
  131. type = CHAR;
  132. SET_VARIANT(char);
  133. }
  134. void Variant::Set(const float value)
  135. {
  136. type = FLOAT;
  137. SET_VARIANT(float);
  138. }
  139. void Variant::Set(const int value)
  140. {
  141. type = INT;
  142. SET_VARIANT(int);
  143. }
  144. void Variant::Set(const String& value)
  145. {
  146. if (type == STRING)
  147. {
  148. (*(String*)data) = value;
  149. }
  150. else
  151. {
  152. type = STRING;
  153. new(data) String(value);
  154. }
  155. }
  156. void Variant::Set(const word value)
  157. {
  158. type = WORD;
  159. SET_VARIANT(word);
  160. }
  161. void Variant::Set(const char* value)
  162. {
  163. Set(String(value));
  164. }
  165. void Variant::Set(void* voidptr)
  166. {
  167. type = VOIDPTR;
  168. memcpy(data, &voidptr, sizeof(void*));
  169. }
  170. void Variant::Set(const Vector2f& value)
  171. {
  172. type = VECTOR2;
  173. SET_VARIANT(Vector2f);
  174. }
  175. void Variant::Set(const Vector3f& value)
  176. {
  177. type = VECTOR3;
  178. SET_VARIANT(Vector3f);
  179. }
  180. void Variant::Set(const Vector4f& value)
  181. {
  182. type = VECTOR4;
  183. SET_VARIANT(Vector4f);
  184. }
  185. void Variant::Set(const TransformRef& value)
  186. {
  187. if (type == TRANSFORMREF)
  188. {
  189. SET_VARIANT(TransformRef);
  190. }
  191. else
  192. {
  193. type = TRANSFORMREF;
  194. new(data) TransformRef(value);
  195. }
  196. }
  197. void Variant::Set(const TransitionList& value)
  198. {
  199. if (type == TRANSITIONLIST)
  200. {
  201. *(TransitionList*)data = value;
  202. }
  203. else
  204. {
  205. type = TRANSITIONLIST;
  206. new(data) TransitionList(value);
  207. }
  208. }
  209. void Variant::Set(const AnimationList& value)
  210. {
  211. if (type == ANIMATIONLIST)
  212. {
  213. *(AnimationList*)data = value;
  214. }
  215. else
  216. {
  217. type = ANIMATIONLIST;
  218. new(data) AnimationList(value);
  219. }
  220. }
  221. void Variant::Set(const DecoratorList& value)
  222. {
  223. if (type == DECORATORLIST)
  224. {
  225. *(DecoratorList*)data = value;
  226. }
  227. else
  228. {
  229. type = DECORATORLIST;
  230. new(data) DecoratorList(value);
  231. }
  232. }
  233. void Variant::Set(const Colourf& value)
  234. {
  235. type = COLOURF;
  236. SET_VARIANT(Colourf);
  237. }
  238. void Variant::Set(const Colourb& value)
  239. {
  240. type = COLOURB;
  241. SET_VARIANT(Colourb);
  242. }
  243. void Variant::Set(ScriptInterface* value)
  244. {
  245. type = SCRIPTINTERFACE;
  246. memcpy(data, &value, sizeof(ScriptInterface*));
  247. }
  248. Variant& Variant::operator=(const Variant& copy)
  249. {
  250. if (copy.type != type)
  251. Clear();
  252. Set(copy);
  253. return *this;
  254. }
  255. #define DEFAULT_VARIANT_COMPARE(TYPE) static_cast<TYPE>(*(TYPE*)data) == static_cast<TYPE>(*(TYPE*)other.data)
  256. bool Variant::operator==(const Variant & other) const
  257. {
  258. if (type != other.type)
  259. return false;
  260. switch (type)
  261. {
  262. case BYTE:
  263. return DEFAULT_VARIANT_COMPARE(byte);
  264. case CHAR:
  265. return DEFAULT_VARIANT_COMPARE(char);
  266. case FLOAT:
  267. return DEFAULT_VARIANT_COMPARE(float);
  268. case INT:
  269. return DEFAULT_VARIANT_COMPARE(int);
  270. case STRING:
  271. return DEFAULT_VARIANT_COMPARE(String);
  272. case WORD:
  273. return DEFAULT_VARIANT_COMPARE(word);
  274. case VECTOR2:
  275. return DEFAULT_VARIANT_COMPARE(Vector2f);
  276. case VECTOR3:
  277. return DEFAULT_VARIANT_COMPARE(Vector3f);
  278. case VECTOR4:
  279. return DEFAULT_VARIANT_COMPARE(Vector4f);
  280. case TRANSFORMREF:
  281. return DEFAULT_VARIANT_COMPARE(TransformRef);
  282. case TRANSITIONLIST:
  283. return DEFAULT_VARIANT_COMPARE(TransitionList);
  284. case ANIMATIONLIST:
  285. return DEFAULT_VARIANT_COMPARE(AnimationList);
  286. case DECORATORLIST:
  287. return DEFAULT_VARIANT_COMPARE(DecoratorList);
  288. case COLOURF:
  289. return DEFAULT_VARIANT_COMPARE(Colourf);
  290. case COLOURB:
  291. return DEFAULT_VARIANT_COMPARE(Colourb);
  292. case SCRIPTINTERFACE:
  293. return DEFAULT_VARIANT_COMPARE(ScriptInterface*);
  294. case VOIDPTR:
  295. return DEFAULT_VARIANT_COMPARE(void*);
  296. case NONE:
  297. return true;
  298. break;
  299. }
  300. RMLUI_ERRORMSG("Variant comparison not implemented for this type.");
  301. return false;
  302. }
  303. }
  304. }