Variant.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <Rocket/Core/Header.h>
  30. #include <Rocket/Core/Types.h>
  31. #include <Rocket/Core/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. COLOURF = 'g',
  65. COLOURB = 'h',
  66. SCRIPTINTERFACE = 'p',
  67. VOIDPTR = '*',
  68. };
  69. /// Clears the data structure stored by the variant.
  70. void Clear();
  71. /// Gets the current internal representation type.
  72. /// @return The type of data stored in the variant internally.
  73. Type GetType() const;
  74. /// Shares another variant's data with this variant.
  75. /// @param[in] copy Variant to share data.
  76. void Set(const Variant& copy);
  77. /// Sets a byte value on this variant.
  78. /// @param[in] value New value to set.
  79. void Set(const byte value);
  80. /// Sets a signed char value on this variant.
  81. /// @param[in] value New value to set.
  82. void Set(const char value);
  83. /// Sets a float value on this variant.
  84. /// @param[in] value New value to set.
  85. void Set(const float value);
  86. /// Sets a signed int value on this variant.
  87. /// @param[in] value New value to set.
  88. void Set(const int value);
  89. /// Sets a word value on this variant.
  90. /// @param[in] value New value to set.
  91. void Set(const word value);
  92. /// Sets a constant C string value on this variant.
  93. /// @param[in] value New value to set.
  94. void Set(const char* value);
  95. /// Sets a generic void* value on this variant.
  96. /// @param[in] value New value to set.
  97. void Set(void* value);
  98. /// Sets an EMP string value on this variant.
  99. /// @param[in] value New value to set.
  100. void Set(const String& value);
  101. /// Sets a Vector2f value on this variant.
  102. /// @param[in] value New value to set.
  103. void Set(const Vector2f& value);
  104. /// Sets a Colourf value on this variant.
  105. /// @param[in] value New value to set.
  106. void Set(const Colourf& value);
  107. /// Sets a Colourb value on this variant.
  108. /// @param[in] value New value to set.
  109. void Set(const Colourb& value);
  110. /// Sets a script object value on this variant.
  111. /// @param[in] value New value to set.
  112. void Set(ScriptInterface* value);
  113. /// Templatised data accessor. TypeConverters will be used to attempt to convert from the
  114. /// internal representation to the requested representation.
  115. /// @return Data in the requested type.
  116. template< typename T >
  117. T Get() const;
  118. /// Templatised data accessor. TypeConverters will be used to attempt to convert from the
  119. /// internal representation to the requested representation.
  120. /// @param[out] value Data in the requested type.
  121. /// @return True if the value was converted and returned, false if no data was stored in the variant.
  122. template< typename T >
  123. bool GetInto(T& value) const;
  124. /// Assigns another variant's internal data to this variant.
  125. /// @param[in] copy Variant to share data.
  126. Variant& operator=(const Variant& copy);
  127. private:
  128. // Actual variant data, reference counted across variants.
  129. class DataBlock
  130. {
  131. public:
  132. DataBlock();
  133. ~DataBlock();
  134. void Clear();
  135. Type type;
  136. char data[16];
  137. void* data_ptr;
  138. mutable int reference_count;
  139. };
  140. DataBlock* data_block;
  141. void NewDataBlock(Type type);
  142. void ReleaseDataBlock();
  143. };
  144. #include <Rocket/Core/Variant.inl>
  145. }
  146. }
  147. #endif