Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Cast.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. 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 DISABLE_COMPILER_GCC_WARNING_START ("GCC diagnostic ignored \"-Wunused-local-typedefs\"");
21 using DECAYED_T = conditional_t<is_reference_v<T>, remove_cvref_t<T>, remove_pointer_t<remove_cvref_t<T>>>; // remove_reference_t
22 DISABLE_COMPILER_GCC_WARNING_END ("GCC diagnostic ignored \"-Wunused-local-typedefs\"");
23 if constexpr (is_reference_v<T>) {
24 // must special case here cuz if we compare two references wont compare POINTERS, but instead call operator== on the underlying T type
25 Require (static_cast<const DECAYED_T*> (&arg) == dynamic_cast<const DECAYED_T*> (&arg));
26 AssertMember (&arg, DECAYED_T);
27 }
28 else {
29 Require (static_cast<T> (arg) == dynamic_cast<T> (arg));
30 AssertMember (arg, DECAYED_T);
31 }
32 }
33 return static_cast<T> (arg);
34 }
35
36 /*
37 ********************************************************************************
38 ************************* Debug::UncheckedDynamicPointerCast *******************
39 ********************************************************************************
40 */
41 template <typename T, typename T1>
42 inline std::shared_ptr<T> UncheckedDynamicPointerCast (const std::shared_ptr<T1>& arg) noexcept
43 {
44 //Require (dynamic_pointer_cast<T> (arg) != nullptr); -- checked redundantly in UncheckedDynamicCast
45 if (const auto p = UncheckedDynamicCast<T*> (arg.get ())) {
46 return shared_ptr<T>{std::move (arg), p};
47 }
48 return {};
49 }
50
51}
#define qStroika_Foundation_Debug_AssertionsChecked
The qStroika_Foundation_Debug_AssertionsChecked flag determines if assertions are checked and validat...
Definition Assertions.h:48
#define AssertMember(p, c)
Definition Assertions.h:312
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:42