Variant.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "../../Include/Rocket/Core/Variant.h"
  29. namespace Rocket {
  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(TransformRef) <= LOCAL_DATA_SIZE, "Local data too small for TransformRef");
  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. }
  42. Variant::Variant( const Variant& copy ) : type(NONE)
  43. {
  44. Set(copy);
  45. }
  46. Variant::~Variant()
  47. {
  48. Clear();
  49. }
  50. void Variant::Clear()
  51. {
  52. // Free any allocated types.
  53. switch (type)
  54. {
  55. case STRING:
  56. {
  57. // Clean up the string.
  58. String* string = (String*)data;
  59. string->~String();
  60. }
  61. break;
  62. case TRANSFORMREF:
  63. {
  64. // Clean up the transform.
  65. TransformRef* transform = (TransformRef*)data;
  66. transform->~TransformRef();
  67. }
  68. break;
  69. case TRANSITIONLIST:
  70. {
  71. // Clean up the transition list.
  72. TransitionList* transition_list = (TransitionList*)data;
  73. transition_list->~TransitionList();
  74. }
  75. break;
  76. case ANIMATIONLIST:
  77. {
  78. // Clean up the transition list.
  79. AnimationList* animation_list = (AnimationList*)data;
  80. animation_list->~AnimationList();
  81. }
  82. break;
  83. default:
  84. break;
  85. }
  86. type = NONE;
  87. }
  88. //////////////////////////////////////////////////
  89. // Set methods
  90. //////////////////////////////////////////////////
  91. #define SET_VARIANT(type) *((type*)data) = value;
  92. void Variant::Set(const Variant& copy)
  93. {
  94. switch (copy.type)
  95. {
  96. case STRING:
  97. Set(*(String*)copy.data);
  98. break;
  99. case TRANSFORMREF:
  100. Set(*(TransformRef*)copy.data);
  101. break;
  102. case TRANSITIONLIST:
  103. Set(*(TransitionList*)copy.data);
  104. break;
  105. case ANIMATIONLIST:
  106. Set(*(AnimationList*)copy.data);
  107. break;
  108. default:
  109. Clear();
  110. memcpy(data, copy.data, LOCAL_DATA_SIZE);
  111. break;
  112. }
  113. type = copy.type;
  114. }
  115. void Variant::Set(const byte value)
  116. {
  117. type = BYTE;
  118. SET_VARIANT(byte);
  119. }
  120. void Variant::Set(const char value)
  121. {
  122. type = CHAR;
  123. SET_VARIANT(char);
  124. }
  125. void Variant::Set(const float value)
  126. {
  127. type = FLOAT;
  128. SET_VARIANT(float);
  129. }
  130. void Variant::Set(const int value)
  131. {
  132. type = INT;
  133. SET_VARIANT(int);
  134. }
  135. void Variant::Set(const String& value)
  136. {
  137. if (type == STRING)
  138. {
  139. (*(String*)data) = value;
  140. }
  141. else
  142. {
  143. type = STRING;
  144. new(data) String(value);
  145. }
  146. }
  147. void Variant::Set(const word value)
  148. {
  149. type = WORD;
  150. SET_VARIANT(word);
  151. }
  152. void Variant::Set(const char* value)
  153. {
  154. Set(String(value));
  155. }
  156. void Variant::Set(void* voidptr)
  157. {
  158. type = VOIDPTR;
  159. memcpy(data, &voidptr, sizeof(void*));
  160. }
  161. void Variant::Set(const Vector2f& value)
  162. {
  163. type = VECTOR2;
  164. SET_VARIANT(Vector2f);
  165. }
  166. void Variant::Set(const Vector3f& value)
  167. {
  168. type = VECTOR3;
  169. SET_VARIANT(Vector3f);
  170. }
  171. void Variant::Set(const Vector4f& value)
  172. {
  173. type = VECTOR4;
  174. SET_VARIANT(Vector4f);
  175. }
  176. void Variant::Set(const TransformRef& value)
  177. {
  178. if (type == TRANSFORMREF)
  179. {
  180. SET_VARIANT(TransformRef);
  181. }
  182. else
  183. {
  184. type = TRANSFORMREF;
  185. new(data) TransformRef(value);
  186. }
  187. }
  188. void Variant::Set(const TransitionList& value)
  189. {
  190. if (type == TRANSITIONLIST)
  191. {
  192. *(TransitionList*)data = value;
  193. }
  194. else
  195. {
  196. type = TRANSITIONLIST;
  197. new(data) TransitionList(value);
  198. }
  199. }
  200. void Variant::Set(const AnimationList& value)
  201. {
  202. if (type == ANIMATIONLIST)
  203. {
  204. *(AnimationList*)data = value;
  205. }
  206. else
  207. {
  208. type = ANIMATIONLIST;
  209. new(data) AnimationList(value);
  210. }
  211. }
  212. void Variant::Set(const Colourf& value)
  213. {
  214. type = COLOURF;
  215. SET_VARIANT(Colourf);
  216. }
  217. void Variant::Set(const Colourb& value)
  218. {
  219. type = COLOURB;
  220. SET_VARIANT(Colourb);
  221. }
  222. void Variant::Set(ScriptInterface* value)
  223. {
  224. type = SCRIPTINTERFACE;
  225. memcpy(data, &value, sizeof(ScriptInterface*));
  226. }
  227. Variant& Variant::operator=(const Variant& copy)
  228. {
  229. Set(copy);
  230. return *this;
  231. }
  232. #define DEFAULT_VARIANT_COMPARE(TYPE) static_cast<TYPE>(*(TYPE*)data) == static_cast<TYPE>(*(TYPE*)other.data)
  233. bool Variant::operator==(const Variant & other) const
  234. {
  235. if (type != other.type)
  236. return false;
  237. switch (type)
  238. {
  239. case BYTE:
  240. return DEFAULT_VARIANT_COMPARE(byte);
  241. case CHAR:
  242. return DEFAULT_VARIANT_COMPARE(char);
  243. case FLOAT:
  244. return DEFAULT_VARIANT_COMPARE(float);
  245. case INT:
  246. return DEFAULT_VARIANT_COMPARE(int);
  247. case STRING:
  248. return DEFAULT_VARIANT_COMPARE(String);
  249. case WORD:
  250. return DEFAULT_VARIANT_COMPARE(word);
  251. case VECTOR2:
  252. return DEFAULT_VARIANT_COMPARE(Vector2f);
  253. case VECTOR3:
  254. return DEFAULT_VARIANT_COMPARE(Vector3f);
  255. case VECTOR4:
  256. return DEFAULT_VARIANT_COMPARE(Vector4f);
  257. case TRANSFORMREF:
  258. return DEFAULT_VARIANT_COMPARE(TransformRef);
  259. case TRANSITIONLIST:
  260. return DEFAULT_VARIANT_COMPARE(TransitionList);
  261. case ANIMATIONLIST:
  262. return DEFAULT_VARIANT_COMPARE(AnimationList);
  263. case COLOURF:
  264. return DEFAULT_VARIANT_COMPARE(Colourf);
  265. case COLOURB:
  266. return DEFAULT_VARIANT_COMPARE(Colourb);
  267. case SCRIPTINTERFACE:
  268. return DEFAULT_VARIANT_COMPARE(ScriptInterface*);
  269. case VOIDPTR:
  270. return DEFAULT_VARIANT_COMPARE(void*);
  271. case NONE:
  272. return true;
  273. break;
  274. }
  275. ROCKET_ERRORMSG("Variant comparison not implemented for this type.");
  276. return false;
  277. }
  278. }
  279. }