Rtti.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Util/Assert.h>
  7. namespace anki
  8. {
  9. /// @addtogroup util_private
  10. /// @{
  11. template<typename T>
  12. struct ExtractType
  13. {
  14. using Type = T;
  15. };
  16. template<typename T>
  17. struct ExtractType<const T&>
  18. {
  19. using Type = T;
  20. };
  21. template<typename T>
  22. struct ExtractType<T&>
  23. {
  24. using Type = T;
  25. };
  26. template<typename T>
  27. struct ExtractType<const T*>
  28. {
  29. using Type = T;
  30. };
  31. template<typename T>
  32. struct ExtractType<T*>
  33. {
  34. using Type = T;
  35. };
  36. /// @}
  37. /// @addtogroup util_other
  38. /// @{
  39. /// Check if a class is of certain type.
  40. template<typename TTo, typename TFrom>
  41. inline Bool isa(const TFrom& c)
  42. {
  43. return TTo::classof(&c);
  44. }
  45. /// Check if a class is of certain type.
  46. template<typename TTo, typename TFrom>
  47. inline Bool isa(TFrom& c)
  48. {
  49. return TTo::classof(&c);
  50. }
  51. /// Check if a class is of certain type.
  52. template<typename TTo, typename TFrom>
  53. inline Bool isa(const TFrom* c)
  54. {
  55. return TTo::classof(c);
  56. }
  57. /// Check if a class is of certain type.
  58. template<typename TTo, typename TFrom>
  59. inline Bool isa(TFrom* c)
  60. {
  61. return TTo::classof(c);
  62. }
  63. /// Custom dynamic cast.
  64. template<typename TTo, typename TFrom>
  65. inline TTo dcast(TFrom& c)
  66. {
  67. ANKI_ASSERT(isa<typename ExtractType<TTo>::Type>(c));
  68. return static_cast<TTo>(c);
  69. }
  70. /// Custom dynamic cast.
  71. template<typename TTo, typename TFrom>
  72. inline TTo dcast(TFrom* c)
  73. {
  74. ANKI_ASSERT(isa<typename ExtractType<TTo>::Type>(c));
  75. return static_cast<TTo>(c);
  76. }
  77. /// @}
  78. } // end namespace anki