Variant.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 <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. Variant::Type Variant::GetType() const
  71. {
  72. return type;
  73. }
  74. //////////////////////////////////////////////////
  75. // Set methods
  76. //////////////////////////////////////////////////
  77. #define SET_VARIANT(type) *((type*)data) = value;
  78. void Variant::Set(const Variant& copy)
  79. {
  80. switch (copy.type)
  81. {
  82. case STRING:
  83. {
  84. // Create the string
  85. Set(*(String*)copy.data);
  86. }
  87. break;
  88. case TRANSFORMREF:
  89. {
  90. // Create the transform
  91. Set(*(TransformRef*)copy.data);
  92. }
  93. break;
  94. default:
  95. Clear();
  96. memcpy(data, copy.data, LOCAL_DATA_SIZE);
  97. break;
  98. }
  99. type = copy.type;
  100. }
  101. void Variant::Set(const byte value)
  102. {
  103. type = BYTE;
  104. SET_VARIANT(byte);
  105. }
  106. void Variant::Set(const char value)
  107. {
  108. type = CHAR;
  109. SET_VARIANT(char);
  110. }
  111. void Variant::Set(const float value)
  112. {
  113. type = FLOAT;
  114. SET_VARIANT(float);
  115. }
  116. void Variant::Set(const int value)
  117. {
  118. type = INT;
  119. SET_VARIANT(int);
  120. }
  121. void Variant::Set(const String& value)
  122. {
  123. if (type == STRING)
  124. {
  125. ((String*)data)->Assign(value);
  126. }
  127. else
  128. {
  129. type = STRING;
  130. new(data) String(value);
  131. }
  132. }
  133. void Variant::Set(const word value)
  134. {
  135. type = WORD;
  136. SET_VARIANT(word);
  137. }
  138. void Variant::Set(const char* value)
  139. {
  140. Set(String(value));
  141. }
  142. void Variant::Set(void* voidptr)
  143. {
  144. type = VOIDPTR;
  145. memcpy(data, &voidptr, sizeof(void*));
  146. }
  147. void Variant::Set(const Vector2f& value)
  148. {
  149. type = VECTOR2;
  150. SET_VARIANT(Vector2f);
  151. }
  152. void Variant::Set(const Vector3f& value)
  153. {
  154. type = VECTOR3;
  155. SET_VARIANT(Vector3f);
  156. }
  157. void Variant::Set(const Vector4f& value)
  158. {
  159. type = VECTOR4;
  160. SET_VARIANT(Vector4f);
  161. }
  162. void Variant::Set(const TransformRef& value)
  163. {
  164. if (type == TRANSFORMREF)
  165. {
  166. SET_VARIANT(TransformRef);
  167. }
  168. else
  169. {
  170. type = TRANSFORMREF;
  171. new(data) TransformRef(value);
  172. }
  173. }
  174. void Variant::Set(const Colourf& value)
  175. {
  176. type = COLOURF;
  177. SET_VARIANT(Colourf);
  178. }
  179. void Variant::Set(const Colourb& value)
  180. {
  181. type = COLOURB;
  182. SET_VARIANT(Colourb);
  183. }
  184. void Variant::Set(ScriptInterface* value)
  185. {
  186. type = SCRIPTINTERFACE;
  187. memcpy(data, &value, sizeof(ScriptInterface*));
  188. }
  189. Variant& Variant::operator=(const Variant& copy)
  190. {
  191. Set(copy);
  192. return *this;
  193. }
  194. }
  195. }