2014-06-26 00:58:14 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
#include <typeinfo>
|
2017-04-19 03:36:50 +00:00
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
|
{
|
|
|
|
|
extern const int BAD_CAST;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Проверяет совпадение типа путём сравнения typeid-ов.
|
|
|
|
|
* Проверяется точное совпадение типа. То есть, cast в предка будет неуспешным.
|
|
|
|
|
* В остальном, ведёт себя как dynamic_cast.
|
|
|
|
|
*/
|
|
|
|
|
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-04-19 03:36:50 +00:00
|
|
|
|
throw DB::Exception("Bad cast from type " + std::string(typeid(from).name) + " to " + std::string(typeid(To).name),
|
|
|
|
|
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
|
|
|
|
}
|