BsAny.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsDebug.h"
  4. #include <algorithm>
  5. #include <typeinfo>
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup General
  9. * @{
  10. */
  11. /** Class capable of storing any general type, and safely extracting the proper type from the internal data. */
  12. class Any
  13. {
  14. private:
  15. class DataBase
  16. {
  17. public:
  18. virtual ~DataBase()
  19. { }
  20. virtual DataBase* clone() const = 0;
  21. };
  22. template <typename ValueType>
  23. class Data : public DataBase
  24. {
  25. public:
  26. Data(const ValueType& value)
  27. :value(value)
  28. { }
  29. virtual DataBase* clone() const override
  30. {
  31. return new Data(value);
  32. }
  33. ValueType value;
  34. };
  35. public:
  36. Any()
  37. :mData(nullptr)
  38. { }
  39. template <typename ValueType>
  40. Any(const ValueType& value)
  41. :mData(bs_new<Data<ValueType>>(value))
  42. { }
  43. Any(const Any& other)
  44. :mData(other.mData != nullptr ? other.mData->clone() : nullptr)
  45. { }
  46. ~Any()
  47. {
  48. if (mData != nullptr)
  49. bs_delete(mData);
  50. }
  51. /** Swaps the contents of this object with another. */
  52. Any& swap(Any& rhs)
  53. {
  54. std::swap(mData, rhs.mData);
  55. return *this;
  56. }
  57. template <typename ValueType>
  58. Any& operator= (const ValueType& rhs)
  59. {
  60. Any(rhs).swap(*this);
  61. return *this;
  62. }
  63. Any& operator= (const Any& rhs)
  64. {
  65. Any(rhs).swap(*this);
  66. return *this;
  67. }
  68. /** Returns true if no type is set. */
  69. bool empty() const
  70. {
  71. return mData == nullptr;
  72. }
  73. private:
  74. template <typename ValueType>
  75. friend ValueType* any_cast(Any*);
  76. template <typename ValueType>
  77. friend ValueType* any_cast_unsafe(Any*);
  78. DataBase* mData;
  79. };
  80. /**
  81. * Returns a pointer to the internal data of the specified type.
  82. *
  83. * @note Will return null if cast fails.
  84. */
  85. template <typename ValueType>
  86. ValueType* any_cast(Any* operand)
  87. {
  88. if (operand != nullptr)
  89. return &static_cast<Any::Data<ValueType>*>(operand->mData)->value;
  90. else
  91. return nullptr;
  92. }
  93. /**
  94. * Returns a const pointer to the internal data of the specified type.
  95. *
  96. * @note Will return null if cast fails.
  97. */
  98. template <typename ValueType>
  99. const ValueType* any_cast(const Any* operand)
  100. {
  101. return any_cast<ValueType>(const_cast<Any*>(operand));
  102. }
  103. /**
  104. * Returns a copy of the internal data of the specified type.
  105. *
  106. * @note Throws an exception if cast fails.
  107. */
  108. template <typename ValueType>
  109. ValueType any_cast(const Any& operand)
  110. {
  111. return *any_cast<ValueType>(const_cast<Any*>(&operand));
  112. }
  113. /**
  114. * Returns a copy of the internal data of the specified type.
  115. *
  116. * @note Throws an exception if cast fails.
  117. */
  118. template <typename ValueType>
  119. ValueType any_cast(Any& operand)
  120. {
  121. return *any_cast<ValueType>(&operand);
  122. }
  123. /**
  124. * Returns a reference to the internal data of the specified type.
  125. *
  126. * @note Throws an exception if cast fails.
  127. */
  128. template <typename ValueType>
  129. const ValueType& any_cast_ref(const Any & operand)
  130. {
  131. return *any_cast<ValueType>(const_cast<Any*>(&operand));
  132. }
  133. /**
  134. * Returns a reference to the internal data of the specified type.
  135. *
  136. * @note Throws an exception if cast fails.
  137. */
  138. template <typename ValueType>
  139. ValueType& any_cast_ref(Any& operand)
  140. {
  141. return *any_cast<ValueType>(&operand);
  142. }
  143. /** Casts a type without performing any kind of checks. */
  144. template <typename ValueType>
  145. ValueType* any_cast_unsafe(Any* operand)
  146. {
  147. return &static_cast<Any::Data<ValueType>*>(operand->mData)->value;
  148. }
  149. /** Casts a type without performing any kind of checks. */
  150. template <typename ValueType>
  151. const ValueType* any_cast_unsafe(const Any* operand)
  152. {
  153. return any_cast_unsafe<ValueType>(const_cast<Any*>(operand));
  154. }
  155. /** @} */
  156. }