qxLib
rtti_cast.h
Go to the documentation of this file.
1 /**
2 
3  @file rtti_cast.h
4  @author Khrapov
5  @date 9.09.2021
6  @copyright © Nick Khrapov, 2021. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <qx/rtti/rtti.h>
12 #include <qx/smart_ptr/link.h>
13 
14 #include <memory>
15 
16 namespace qx
17 {
18 
19 /**
20  @brief Returns to_t* if from_t is inherited from Y, otherwise nullptr
21  @tparam to_t - type to cast to
22  @tparam from_t - type to cast from
23  @param pointer - a pointer to test
24  @retval - to_t* if from_t is inherited from pointer class, otherwise nullptr
25 **/
26 template<class to_t, class from_t>
27  requires std::derived_from<from_t, rtti_pure_base> && std::derived_from<to_t, rtti_pure_base>
28 to_t* rtti_cast(from_t* pointer)
29 {
30  if (pointer && pointer->template is_derived_from<to_t>())
31  return static_cast<to_t*>(pointer);
32 
33  return nullptr;
34 }
35 
36 /**
37  @brief Returns to_t* if from_t is inherited from Y, otherwise nullptr
38  @tparam to_t - type to cast to
39  @tparam from_t - type to cast from
40  @param pValue - a pointer to test
41  @retval - to_t* if from_t is inherited from Y, otherwise nullptr
42 **/
43 template<class to_t, class from_t>
44  requires std::derived_from<from_t, rtti_pure_base> && std::derived_from<to_t, rtti_pure_base>
45 to_t* rtti_cast(const std::shared_ptr<from_t>& pValue)
46 {
47  if (pValue && pValue->template is_derived_from<to_t>())
48  return static_cast<to_t*>(pValue.get());
49 
50  return nullptr;
51 }
52 
53 /**
54  @brief Returns to_t* if from_t is inherited from Y, otherwise nullptr
55  @tparam to_t - type to cast to
56  @tparam from_t - type to cast from
57  @param pValue - a pointer to test
58  @retval - to_t* if from_t is inherited from Y, otherwise nullptr
59 **/
60 template<class to_t, class from_t>
61  requires std::derived_from<from_t, rtti_pure_base> && std::derived_from<to_t, rtti_pure_base>
62 to_t* rtti_cast(const std::unique_ptr<from_t>& pValue)
63 {
64  if (pValue && pValue->template is_derived_from<to_t>())
65  return static_cast<to_t*>(pValue.get());
66 
67  return nullptr;
68 }
69 
70 /**
71  @brief Returns to_t* if from_t is inherited from Y, otherwise nullptr
72  @tparam to_t - type to cast to
73  @tparam from_t - type to cast from
74  @param pValueLink - a pointer to test
75  @retval - to_t* if from_t is inherited from Y, otherwise nullptr
76 **/
77 template<class to_t, class from_t>
78  requires std::derived_from<from_t, rtti_pure_base> && std::derived_from<to_t, rtti_pure_base>
79 to_t* rtti_cast(const link<from_t>& pValueLink)
80 {
81  if (auto pValue = pValueLink.lock())
82  if (pValue->template is_derived_from<to_t>())
83  return static_cast<to_t*>(pValue.get());
84 
85  return nullptr;
86 }
87 
88 /**
89  @brief Returns to_t* if from_t is inherited from Y, otherwise nullptr
90  @details Pointer class should contain QX_RTTI_CLASS
91  @tparam to_t - type to cast to
92  @tparam from_t - type to cast from
93  @param value - a value to test
94  @retval - to_t* if from_t is inherited from Y, otherwise nullptr
95 **/
96 template<class to_t, class from_t>
97  requires std::derived_from<from_t, rtti_pure_base> && std::derived_from<to_t, rtti_pure_base>
98 to_t* rtti_cast(from_t& value)
99 {
100  if (value.template is_derived_from<to_t>())
101  return static_cast<to_t*>(&value);
102 
103  return nullptr;
104 }
105 
106 } // namespace qx
RTTI system based on polymorphism.
requires std::derived_from< from_t, rtti_pure_base > &&std::derived_from< to_t, rtti_pure_base > to_t * rtti_cast(from_t *pointer)
Returns to_t* if from_t is inherited from Y, otherwise nullptr.
Definition: rtti_cast.h:28