blob: 0b73ba1cc12a95416d32c09f5193e9234f35222c (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#pragma once
#include <type_traits>
#include <typeinfo>
#include <typeindex>
#include <string>
#include <Common/Exception.h>
#include <base/demangle.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
}
/** Perform static_cast in release build.
* Checks type by comparing typeid and throw an exception in debug build.
* The exact match of the type is checked. That is, cast to the ancestor will be unsuccessful.
*/
template <typename To, typename From>
inline To assert_cast(From && from)
{
#ifndef NDEBUG
try
{
if constexpr (std::is_pointer_v<To>)
{
if (typeid(*from) == typeid(std::remove_pointer_t<To>))
return static_cast<To>(from);
}
else
{
if (typeid(from) == typeid(To))
return static_cast<To>(from);
}
}
catch (const std::exception & e)
{
throw DB::Exception::createDeprecated(e.what(), DB::ErrorCodes::LOGICAL_ERROR);
}
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Bad cast from type {} to {}",
demangle(typeid(from).name()), demangle(typeid(To).name()));
#else
return static_cast<To>(from);
#endif
}
|