score
Home
Classes
Namespaces
Files
SafeCast.hpp
1
#pragma once
2
#include <score/tools/Debug.hpp>
3
4
#include <ossia/detail/config.hpp>
5
6
#include <exception>
7
8
#ifdef SCORE_DEBUG
9
template
<
typename
T,
typename
U>
10
T safe_cast(U* other)
11
{
// there is also a static_cast since compilers
12
// must ensure that the downcast is possible, which it
13
// does not with dynamic_cast
14
[[maybe_unused]]
auto
check =
static_cast<
T
>
(other);
15
auto
res =
dynamic_cast<
T
>
(other);
16
SCORE_ASSERT(res);
17
return
res;
18
}
19
20
template
<
typename
T,
typename
U>
21
T safe_cast(U&& other)
22
try
23
{
24
[[maybe_unused]]
auto
&& check =
static_cast<
T
>
(other);
25
auto
&& res =
dynamic_cast<
T
>
(other);
26
return
res;
27
}
28
catch
(
const
std::exception& e)
29
{
30
qDebug() << e.what();
31
SCORE_ABORT;
32
}
33
#else
34
template
<
typename
T,
typename
U>
35
constexpr OSSIA_INLINE T safe_cast(U&& other)
36
{
37
return
static_cast<
T
>
(other);
38
}
39
#endif