Variant.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. default:
  59. break;
  60. }
  61. type = NONE;
  62. }
  63. Variant::Type Variant::GetType() const
  64. {
  65. return type;
  66. }
  67. //////////////////////////////////////////////////
  68. // Set methods
  69. //////////////////////////////////////////////////
  70. #define SET_VARIANT(type) *((type*)data) = value;
  71. void Variant::Set(const Variant& copy)
  72. {
  73. switch (copy.type)
  74. {
  75. case STRING:
  76. {
  77. // Create the string
  78. Set(*(String*)copy.data);
  79. }
  80. break;
  81. default:
  82. Clear();
  83. memcpy(data, copy.data, LOCAL_DATA_SIZE);
  84. break;
  85. }
  86. type = copy.type;
  87. }
  88. void Variant::Set(const byte value)
  89. {
  90. type = BYTE;
  91. SET_VARIANT(byte);
  92. }
  93. void Variant::Set(const char value)
  94. {
  95. type = CHAR;
  96. SET_VARIANT(char);
  97. }
  98. void Variant::Set(const float value)
  99. {
  100. type = FLOAT;
  101. SET_VARIANT(float);
  102. }
  103. void Variant::Set(const int value)
  104. {
  105. type = INT;
  106. SET_VARIANT(int);
  107. }
  108. void Variant::Set(const String& value)
  109. {
  110. if (type == STRING)
  111. {
  112. ((String*)data)->Assign(value);
  113. }
  114. else
  115. {
  116. type = STRING;
  117. new(data) String(value);
  118. }
  119. }
  120. void Variant::Set(const word value)
  121. {
  122. type = WORD;
  123. SET_VARIANT(word);
  124. }
  125. void Variant::Set(const char* value)
  126. {
  127. Set(String(value));
  128. }
  129. void Variant::Set(void* voidptr)
  130. {
  131. type = VOIDPTR;
  132. memcpy(data, &voidptr, sizeof(void*));
  133. }
  134. void Variant::Set(const Vector2f& value)
  135. {
  136. type = VECTOR2;
  137. SET_VARIANT(Vector2f);
  138. }
  139. void Variant::Set(const Colourf& value)
  140. {
  141. type = COLOURF;
  142. SET_VARIANT(Colourf);
  143. }
  144. void Variant::Set(const Colourb& value)
  145. {
  146. type = COLOURB;
  147. SET_VARIANT(Colourb);
  148. }
  149. void Variant::Set(ScriptInterface* value)
  150. {
  151. type = SCRIPTINTERFACE;
  152. memcpy(data, &value, sizeof(ScriptInterface*));
  153. }
  154. Variant& Variant::operator=(const Variant& copy)
  155. {
  156. Set(copy);
  157. return *this;
  158. }
  159. }
  160. }