Variant.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. ROCKET_STATIC_ASSERT(sizeof(Colourb) <= LOCAL_DATA_SIZE, LOCAL_DATA_TOO_SMALL_FOR_Colourb);
  35. ROCKET_STATIC_ASSERT(sizeof(Colourf) <= LOCAL_DATA_SIZE, LOCAL_DATA_TOO_SMALL_FOR_Colourf);
  36. ROCKET_STATIC_ASSERT(sizeof(String) <= LOCAL_DATA_SIZE, LOCAL_DATA_TOO_SMALL_FOR_String);
  37. }
  38. Variant::Variant( const Variant& copy ) : type(NONE)
  39. {
  40. Set(copy);
  41. }
  42. Variant::~Variant()
  43. {
  44. Clear();
  45. }
  46. void Variant::Clear()
  47. {
  48. // Free any allocated types.
  49. switch (type)
  50. {
  51. case STRING:
  52. {
  53. // Clean up the string.
  54. String* string = (String*)data;
  55. string->~String();
  56. }
  57. break;
  58. case TRANSFORMREF:
  59. {
  60. // Clean up the transform.
  61. TransformRef* transform = (TransformRef*)data;
  62. transform->~TransformRef();
  63. }
  64. break;
  65. default:
  66. break;
  67. }
  68. type = NONE;
  69. }
  70. //////////////////////////////////////////////////
  71. // Set methods
  72. //////////////////////////////////////////////////
  73. #define SET_VARIANT(type) *((type*)data) = value;
  74. void Variant::Set(const Variant& copy)
  75. {
  76. switch (copy.type)
  77. {
  78. case STRING:
  79. {
  80. // Create the string
  81. Set(*(String*)copy.data);
  82. }
  83. break;
  84. case TRANSFORMREF:
  85. {
  86. // Create the transform
  87. Set(*(TransformRef*)copy.data);
  88. }
  89. break;
  90. default:
  91. Clear();
  92. memcpy(data, copy.data, LOCAL_DATA_SIZE);
  93. break;
  94. }
  95. type = copy.type;
  96. }
  97. void Variant::Set(const byte value)
  98. {
  99. type = BYTE;
  100. SET_VARIANT(byte);
  101. }
  102. void Variant::Set(const char value)
  103. {
  104. type = CHAR;
  105. SET_VARIANT(char);
  106. }
  107. void Variant::Set(const float value)
  108. {
  109. type = FLOAT;
  110. SET_VARIANT(float);
  111. }
  112. void Variant::Set(const int value)
  113. {
  114. type = INT;
  115. SET_VARIANT(int);
  116. }
  117. void Variant::Set(const String& value)
  118. {
  119. if (type == STRING)
  120. {
  121. ((String*)data)->Assign(value);
  122. }
  123. else
  124. {
  125. type = STRING;
  126. new(data) String(value);
  127. }
  128. }
  129. void Variant::Set(const word value)
  130. {
  131. type = WORD;
  132. SET_VARIANT(word);
  133. }
  134. void Variant::Set(const char* value)
  135. {
  136. Set(String(value));
  137. }
  138. void Variant::Set(void* voidptr)
  139. {
  140. type = VOIDPTR;
  141. memcpy(data, &voidptr, sizeof(void*));
  142. }
  143. void Variant::Set(const Vector2f& value)
  144. {
  145. type = VECTOR2;
  146. SET_VARIANT(Vector2f);
  147. }
  148. void Variant::Set(const Vector3f& value)
  149. {
  150. type = VECTOR3;
  151. SET_VARIANT(Vector3f);
  152. }
  153. void Variant::Set(const Vector4f& value)
  154. {
  155. type = VECTOR4;
  156. SET_VARIANT(Vector4f);
  157. }
  158. void Variant::Set(const TransformRef& value)
  159. {
  160. if (type == TRANSFORMREF)
  161. {
  162. SET_VARIANT(TransformRef);
  163. }
  164. else
  165. {
  166. type = TRANSFORMREF;
  167. new(data) TransformRef(value);
  168. }
  169. }
  170. void Variant::Set(const Colourf& value)
  171. {
  172. type = COLOURF;
  173. SET_VARIANT(Colourf);
  174. }
  175. void Variant::Set(const Colourb& value)
  176. {
  177. type = COLOURB;
  178. SET_VARIANT(Colourb);
  179. }
  180. void Variant::Set(ScriptInterface* value)
  181. {
  182. type = SCRIPTINTERFACE;
  183. memcpy(data, &value, sizeof(ScriptInterface*));
  184. }
  185. Variant& Variant::operator=(const Variant& copy)
  186. {
  187. Set(copy);
  188. return *this;
  189. }
  190. }
  191. }