Variant.inl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // Constructs a variant with internal data.
  28. template< typename T >
  29. Variant::Variant(const T& t)
  30. {
  31. data_block = NULL;
  32. Set( t );
  33. }
  34. // Templatised data accessor.
  35. template< typename T >
  36. bool Variant::GetInto(T& value) const
  37. {
  38. switch (GetType())
  39. {
  40. case BYTE:
  41. return TypeConverter< byte, T >::Convert(*(byte*)data_block->data_ptr, value);
  42. break;
  43. case CHAR:
  44. return TypeConverter< char, T >::Convert(*(char*)data_block->data_ptr, value);
  45. break;
  46. case FLOAT:
  47. return TypeConverter< float, T >::Convert(*(float*)data_block->data_ptr, value);
  48. break;
  49. case INT:
  50. return TypeConverter< int, T >::Convert(*(int*)data_block->data_ptr, value);
  51. break;
  52. case STRING:
  53. return TypeConverter< String, T >::Convert(*(String*)data_block->data_ptr, value);
  54. break;
  55. case WORD:
  56. return TypeConverter< word, T >::Convert(*(word*)data_block->data_ptr, value);
  57. break;
  58. case VECTOR2:
  59. return TypeConverter< Vector2f, T >::Convert(*(Vector2f*)data_block->data_ptr, value);
  60. break;
  61. case COLOURF:
  62. return TypeConverter< Colourf, T >::Convert(*(Colourf*)data_block->data_ptr, value);
  63. break;
  64. case COLOURB:
  65. return TypeConverter< Colourb, T >::Convert(*(Colourb*)data_block->data_ptr, value);
  66. break;
  67. case SCRIPTINTERFACE:
  68. {
  69. ScriptInterface* interface = (ScriptInterface*) data_block->data_ptr;
  70. return TypeConverter< ScriptInterface*, T >::Convert(interface, value);
  71. }
  72. break;
  73. case VOIDPTR:
  74. return TypeConverter< void*, T >::Convert(data_block->data_ptr, value);
  75. break;
  76. }
  77. return false;
  78. }
  79. // Templatised data accessor.
  80. template< typename T >
  81. T Variant::Get() const
  82. {
  83. T value;
  84. GetInto(value);
  85. return value;
  86. }