Rtti.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (C) 2009-present, 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. /// @addtogroup util_private
  9. /// @{
  10. template<typename T>
  11. struct ExtractType
  12. {
  13. using Type = T;
  14. };
  15. template<typename T>
  16. struct ExtractType<const T&>
  17. {
  18. using Type = T;
  19. };
  20. template<typename T>
  21. struct ExtractType<T&>
  22. {
  23. using Type = T;
  24. };
  25. template<typename T>
  26. struct ExtractType<const T*>
  27. {
  28. using Type = T;
  29. };
  30. template<typename T>
  31. struct ExtractType<T*>
  32. {
  33. using Type = T;
  34. };
  35. /// @}
  36. /// @addtogroup util_other
  37. /// @{
  38. /// Check if a class is of certain type.
  39. template<typename TTo, typename TFrom>
  40. inline Bool isa(const TFrom& c)
  41. {
  42. return TTo::classof(&c);
  43. }
  44. /// Check if a class is of certain type.
  45. template<typename TTo, typename TFrom>
  46. inline Bool isa(TFrom& c)
  47. {
  48. return TTo::classof(&c);
  49. }
  50. /// Check if a class is of certain type.
  51. template<typename TTo, typename TFrom>
  52. inline Bool isa(const TFrom* c)
  53. {
  54. return TTo::classof(c);
  55. }
  56. /// Check if a class is of certain type.
  57. template<typename TTo, typename TFrom>
  58. inline Bool isa(TFrom* c)
  59. {
  60. return TTo::classof(c);
  61. }
  62. /// Custom dynamic cast.
  63. template<typename TTo, typename TFrom>
  64. inline TTo dcast(TFrom& c)
  65. {
  66. ANKI_ASSERT(isa<typename ExtractType<TTo>::Type>(c));
  67. return static_cast<TTo>(c);
  68. }
  69. /// Custom dynamic cast.
  70. template<typename TTo, typename TFrom>
  71. inline TTo dcast(TFrom* c)
  72. {
  73. ANKI_ASSERT(isa<typename ExtractType<TTo>::Type>(c));
  74. return static_cast<TTo>(c);
  75. }
  76. /// @}
  77. } // end namespace anki