Skip to content

Commit 17e73ad

Browse files
authored
Add chain_cast
1 parent a6f215f commit 17e73ad

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

loader/include/Geode/utils/casts.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,23 @@ namespace geode::cast {
6363
}
6464
return nullptr;
6565
}
66+
/**
67+
* Repeatedly apply static_cast in order.
68+
* Useful for casting an object to a modify class, which would
69+
* otherwise require multiple casts.
70+
*/
71+
template <typename Next, typename... Chain, typename Original>
72+
static constexpr decltype(auto) chain_cast(Original&& original) {
73+
if constexpr (sizeof...(Chain) == 0) {
74+
return static_cast<Next>(std::forward<Original>(original));
75+
} else {
76+
return chain_cast<Chain...>(static_cast<Next>(std::forward<Original>(original)));
77+
}
78+
}
79+
80+
template <typename Original>
81+
static constexpr decltype(auto) chain_cast(Original&& original) {
82+
return std::forward<Original>(original);
83+
}
84+
6685
}

0 commit comments

Comments
 (0)