2014-06-26 00:58:14 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <typeinfo>
|
2017-09-01 18:21:01 +00:00
|
|
|
#include <typeindex>
|
2017-04-19 03:36:50 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <Common/Exception.h>
|
2017-12-08 01:34:52 +00:00
|
|
|
#include <Common/demangle.h>
|
2017-04-19 03:36:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_CAST;
|
|
|
|
}
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
|
|
|
2017-05-10 04:00:19 +00:00
|
|
|
/** Checks type by comparing typeid.
|
2017-05-07 20:25:26 +00:00
|
|
|
* The exact match of the type is checked. That is, cast in the ancestor will be unsuccessful.
|
|
|
|
* In the rest, behaves like a dynamic_cast.
|
2014-06-26 00:58:14 +00:00
|
|
|
*/
|
|
|
|
template <typename To, typename From>
|
|
|
|
typename std::enable_if<std::is_reference<To>::value, To>::type typeid_cast(From & from)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (typeid(from) == typeid(To))
|
|
|
|
return static_cast<To>(from);
|
|
|
|
else
|
2017-12-08 01:34:52 +00:00
|
|
|
throw DB::Exception("Bad cast from type " + demangle(typeid(from).name()) + " to " + demangle(typeid(To).name()),
|
2017-04-19 03:36:50 +00:00
|
|
|
DB::ErrorCodes::BAD_CAST);
|
2014-06-26 00:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename To, typename From>
|
|
|
|
To typeid_cast(From * from)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (typeid(*from) == typeid(typename std::remove_pointer<To>::type))
|
|
|
|
return static_cast<To>(from);
|
|
|
|
else
|
|
|
|
return nullptr;
|
2014-06-26 00:58:14 +00:00
|
|
|
}
|