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>
|
2019-11-09 15:31:56 +00:00
|
|
|
#include <memory>
|
2017-04-19 03:36:50 +00:00
|
|
|
#include <string>
|
|
|
|
|
2019-11-09 15:31:56 +00:00
|
|
|
#include <ext/shared_ptr_helper.h>
|
2017-04-19 03:36:50 +00:00
|
|
|
#include <Common/Exception.h>
|
2018-04-09 13:52:39 +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-12-23 01:05:29 +00:00
|
|
|
* The exact match of the type is checked. That is, cast to the ancestor will be unsuccessful.
|
2017-05-07 20:25:26 +00:00
|
|
|
* In the rest, behaves like a dynamic_cast.
|
2014-06-26 00:58:14 +00:00
|
|
|
*/
|
|
|
|
template <typename To, typename From>
|
2017-12-25 04:01:46 +00:00
|
|
|
std::enable_if_t<std::is_reference_v<To>, To> typeid_cast(From & from)
|
2014-06-26 00:58:14 +00:00
|
|
|
{
|
2019-03-15 17:09:14 +00:00
|
|
|
try
|
|
|
|
{
|
2019-11-09 15:31:56 +00:00
|
|
|
if ((typeid(From) == typeid(To)) || (typeid(from) == typeid(To)))
|
2019-03-15 17:09:14 +00:00
|
|
|
return static_cast<To>(from);
|
|
|
|
}
|
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
|
|
|
throw DB::Exception(e.what(), DB::ErrorCodes::BAD_CAST);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw DB::Exception("Bad cast from type " + demangle(typeid(from).name()) + " to " + demangle(typeid(To).name()),
|
|
|
|
DB::ErrorCodes::BAD_CAST);
|
2014-06-26 00:58:14 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 15:31:56 +00:00
|
|
|
|
2014-06-26 00:58:14 +00:00
|
|
|
template <typename To, typename From>
|
2019-11-09 15:31:56 +00:00
|
|
|
std::enable_if_t<std::is_pointer_v<To>, To> typeid_cast(From * from)
|
2014-06-26 00:58:14 +00:00
|
|
|
{
|
2019-03-12 12:41:57 +00:00
|
|
|
try
|
|
|
|
{
|
2019-11-09 15:31:56 +00:00
|
|
|
if ((typeid(From) == typeid(std::remove_pointer_t<To>)) || (typeid(*from) == typeid(std::remove_pointer_t<To>)))
|
2019-03-12 12:41:57 +00:00
|
|
|
return static_cast<To>(from);
|
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
|
|
|
throw DB::Exception(e.what(), DB::ErrorCodes::BAD_CAST);
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
}
|
2019-11-09 15:31:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
template <typename To, typename From>
|
|
|
|
std::enable_if_t<ext::is_shared_ptr_v<To>, To> typeid_cast(const std::shared_ptr<From> & from)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if ((typeid(From) == typeid(typename To::element_type)) || (typeid(*from) == typeid(typename To::element_type)))
|
|
|
|
return std::static_pointer_cast<typename To::element_type>(from);
|
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
|
|
|
throw DB::Exception(e.what(), DB::ErrorCodes::BAD_CAST);
|
|
|
|
}
|
|
|
|
}
|