Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
Cast.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4
5namespace Stroika::Foundation::Debug {
6
7 /*
8 ********************************************************************************
9 ************************** Debug::UncheckedDynamicCast *************************
10 ********************************************************************************
11 */
12 template <typename T, typename T1>
13 inline T UncheckedDynamicCast (T1&& arg) noexcept
14 {
15 static_assert (is_reference_v<T> or is_pointer_v<T>);
16 if constexpr (is_pointer_v<T>) {
17 Require (arg != nullptr);
18 }
20 using DECAYED_T [[maybe_unused]] = conditional_t<is_reference_v<T>, remove_cvref_t<T>, remove_pointer_t<remove_cvref_t<T>>>; // remove_reference_t
21 if constexpr (is_reference_v<T>) {
22 // must special case here cuz if we compare two references wont compare POINTERS, but instead call operator== on the underlying T type
23 Require (static_cast<const DECAYED_T*> (&arg) == dynamic_cast<const DECAYED_T*> (&arg));
24 AssertMember (&arg, DECAYED_T);
25 }
26 else {
27 Require (static_cast<T> (arg) == dynamic_cast<T> (arg));
28 AssertMember (arg, DECAYED_T);
29 }
30 }
31 return static_cast<T> (arg);
32 }
33
34 /*
35 ********************************************************************************
36 ************************* Debug::UncheckedDynamicPointerCast *******************
37 ********************************************************************************
38 */
39 template <typename T, typename T1>
40 inline std::shared_ptr<T> UncheckedDynamicPointerCast (const std::shared_ptr<T1>& arg) noexcept
41 {
42 //Require (dynamic_pointer_cast<T> (arg) != nullptr); -- checked redundantly in UncheckedDynamicCast
43 if (const auto p = UncheckedDynamicCast<T*> (arg.get ())) {
44 return shared_ptr<T>{std::move (arg), p};
45 }
46 return {};
47 }
48
49}
#define qStroika_Foundation_Debug_AssertionsChecked
The qStroika_Foundation_Debug_AssertionsChecked flag determines if assertions are checked and validat...
Definition Assertions.h:49
#define AssertMember(p, c)
Definition Assertions.h:315
T UncheckedDynamicCast(T1 &&arg) noexcept
return the same value as dynamic_cast<T> would have, except instead of checking nullptr,...
Definition Cast.inl:13
std::shared_ptr< T > UncheckedDynamicPointerCast(const std::shared_ptr< T1 > &arg) noexcept
Produce the same result as dynamic_pointer_cast if the successful case (non-null) - with better perfo...
Definition Cast.inl:40