Loading...
Searching...
No Matches
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
9template <typename T, typename U>
10T 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
20template <typename T, typename U>
21T safe_cast(U&& other)
22try
23{
24 [[maybe_unused]] auto&& check = static_cast<T>(other);
25 auto&& res = dynamic_cast<T>(other);
26 return res;
27}
28catch(const std::exception& e)
29{
30 qDebug() << e.what();
31 SCORE_ABORT;
32}
33#else
34template <typename T, typename U>
35constexpr OSSIA_INLINE T safe_cast(U&& other)
36{
37 return static_cast<T>(other);
38}
39#endif