Variant.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #ifndef ROCKETVARIANT_H
  28. #define ROCKETVARIANT_H
  29. #include "Header.h"
  30. #include "Types.h"
  31. #include "TypeConverter.h"
  32. #include <list>
  33. namespace Rocket {
  34. namespace Core {
  35. /**
  36. Variant is a container that can store a selection of basic types. The variant will store the
  37. value in the native form corresponding to the version of Set that was called.
  38. Get is templated to convert from the stored form to the requested form by using a TypeConverter.
  39. @author Lloyd Weehuizen
  40. */
  41. class ROCKETCORE_API Variant
  42. {
  43. public:
  44. Variant();
  45. /// Templatised constructors don't work for the copy constructor, so we have to define it
  46. /// explicitly.
  47. Variant(const Variant&);
  48. /// Constructs a variant with internal data.
  49. /// @param[in] t Data of a supported type to set on the variant.
  50. template< typename T >
  51. Variant(const T& t);
  52. ~Variant();
  53. /// Type of data stored in the variant.
  54. enum Type
  55. {
  56. NONE = '-',
  57. BYTE = 'b',
  58. CHAR = 'c',
  59. FLOAT = 'f',
  60. INT = 'i',
  61. STRING = 's',
  62. WORD = 'w',
  63. VECTOR2 = '2',
  64. VECTOR3 = '3',
  65. VECTOR4 = '4',
  66. COLOURF = 'g',
  67. COLOURB = 'h',
  68. SCRIPTINTERFACE = 'p',
  69. TRANSFORMREF = 't',
  70. TRANSITIONLIST = 'T',
  71. ANIMATIONLIST = 'A',
  72. VOIDPTR = '*',
  73. };
  74. /// Clears the data structure stored by the variant.
  75. void Clear();
  76. /// Gets the current internal representation type.
  77. /// @return The type of data stored in the variant internally.
  78. inline Type GetType() const;
  79. /// Shares another variant's data with this variant.
  80. /// @param[in] copy Variant to share data.
  81. void Set(const Variant& copy);
  82. /// Clear and set a new value to this variant.
  83. /// @param[in] t New value to set.
  84. template<typename T>
  85. void Reset(const T& t);
  86. /// Templatised data accessor. TypeConverters will be used to attempt to convert from the
  87. /// internal representation to the requested representation.
  88. /// @return Data in the requested type.
  89. template< typename T >
  90. T Get() const;
  91. /// Templatised data accessor. TypeConverters will be used to attempt to convert from the
  92. /// internal representation to the requested representation.
  93. /// @param[out] value Data in the requested type.
  94. /// @return True if the value was converted and returned, false if no data was stored in the variant.
  95. template< typename T >
  96. bool GetInto(T& value) const;
  97. /// Assigns another variant's internal data to this variant.
  98. /// @param[in] copy Variant to share data.
  99. Variant& operator=(const Variant& copy);
  100. bool operator==(const Variant& other) const;
  101. bool operator!=(const Variant& other) const { return !(*this == other); }
  102. private:
  103. void Set(const byte value);
  104. void Set(const char value);
  105. void Set(const float value);
  106. void Set(const int value);
  107. void Set(const word value);
  108. void Set(const char* value);
  109. void Set(void* value);
  110. void Set(const String& value);
  111. void Set(const Vector2f& value);
  112. void Set(const Vector3f& value);
  113. void Set(const Vector4f& value);
  114. void Set(const TransformRef& value);
  115. void Set(const TransitionList& value);
  116. void Set(const AnimationList& value);
  117. void Set(const Colourf& value);
  118. void Set(const Colourb& value);
  119. void Set(ScriptInterface* value);
  120. #ifdef ROCKET_ARCH_64
  121. static const int LOCAL_DATA_SIZE = 40; // Required for Strings
  122. #else
  123. static const int LOCAL_DATA_SIZE = 24;
  124. #endif
  125. Type type;
  126. char data[LOCAL_DATA_SIZE];
  127. };
  128. }
  129. }
  130. #include "Variant.inl"
  131. #endif