BsAny.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsDebug.h"
  4. #include <algorithm>
  5. #include <typeinfo>
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * @brief Class capable of storing any general type, and safely extracting
  10. * the proper type from the internal data.
  11. */
  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. /**
  52. * @brief Swaps the contents of this object with another.
  53. */
  54. Any& swap(Any& rhs)
  55. {
  56. std::swap(mData, rhs.mData);
  57. return *this;
  58. }
  59. template <typename ValueType>
  60. Any& operator= (const ValueType& rhs)
  61. {
  62. Any(rhs).swap(*this);
  63. return *this;
  64. }
  65. Any& operator= (const Any& rhs)
  66. {
  67. Any(rhs).swap(*this);
  68. return *this;
  69. }
  70. /**
  71. * @brief Returns true if no type is set.
  72. */
  73. bool empty() const
  74. {
  75. return mData == nullptr;
  76. }
  77. private:
  78. template <typename ValueType>
  79. friend ValueType* any_cast(Any*);
  80. template <typename ValueType>
  81. friend ValueType* any_cast_unsafe(Any*);
  82. DataBase* mData;
  83. };
  84. /**
  85. * @brief Returns a pointer to the internal data of the specified type.
  86. *
  87. * @note Will return null if cast fails.
  88. */
  89. template <typename ValueType>
  90. ValueType* any_cast(Any* operand)
  91. {
  92. if (operand != nullptr)
  93. return &static_cast<Any::Data<ValueType>*>(operand->mData)->value;
  94. else
  95. return nullptr;
  96. }
  97. /**
  98. * @brief Returns a const pointer to the internal data of the specified type.
  99. *
  100. * @note Will return null if cast fails.
  101. */
  102. template <typename ValueType>
  103. const ValueType* any_cast(const Any* operand)
  104. {
  105. return any_cast<ValueType>(const_cast<Any*>(operand));
  106. }
  107. /**
  108. * @brief Returns a copy of the internal data of the specified type.
  109. *
  110. * @note Throws an exception if cast fails.
  111. */
  112. template <typename ValueType>
  113. ValueType any_cast(const Any& operand)
  114. {
  115. return *any_cast<ValueType>(const_cast<Any*>(&operand));
  116. }
  117. /**
  118. * @brief Returns a copy of the internal data of the specified type.
  119. *
  120. * @note Throws an exception if cast fails.
  121. */
  122. template <typename ValueType>
  123. ValueType any_cast(Any& operand)
  124. {
  125. return *any_cast<ValueType>(&operand);
  126. }
  127. /**
  128. * @brief Returns a reference to the internal data of the specified type.
  129. *
  130. * @note Throws an exception if cast fails.
  131. */
  132. template <typename ValueType>
  133. const ValueType& any_cast_ref(const Any & operand)
  134. {
  135. return *any_cast<ValueType>(const_cast<Any*>(&operand));
  136. }
  137. /**
  138. * @brief Returns a reference to the internal data of the specified type.
  139. *
  140. * @note Throws an exception if cast fails.
  141. */
  142. template <typename ValueType>
  143. ValueType& any_cast_ref(Any& operand)
  144. {
  145. return *any_cast<ValueType>(&operand);
  146. }
  147. /**
  148. * @brief Casts a type without performing any kind of checks.
  149. */
  150. template <typename ValueType>
  151. ValueType* any_cast_unsafe(Any* operand)
  152. {
  153. return &static_cast<Any::Data<ValueType>*>(operand->mData)->value;
  154. }
  155. /**
  156. * @brief Casts a type without performing any kind of checks.
  157. */
  158. template <typename ValueType>
  159. const ValueType* any_cast_unsafe(const Any* operand)
  160. {
  161. return any_cast_unsafe<ValueType>(const_cast<Any*>(operand));
  162. }
  163. }