mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-10-18 22:40:50 +00:00
97f2a2213e
* Move some code outside dbms/src folder * Fix paths
77 lines
1.9 KiB
C++
77 lines
1.9 KiB
C++
#pragma once
|
|
|
|
#include <type_traits>
|
|
#include <typeinfo>
|
|
#include <typeindex>
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
#include <ext/shared_ptr_helper.h>
|
|
#include <Common/Exception.h>
|
|
#include <common/demangle.h>
|
|
|
|
|
|
namespace DB
|
|
{
|
|
namespace ErrorCodes
|
|
{
|
|
extern const int BAD_CAST;
|
|
}
|
|
}
|
|
|
|
|
|
/** Checks type by comparing typeid.
|
|
* The exact match of the type is checked. That is, cast to the ancestor will be unsuccessful.
|
|
* In the rest, behaves like a dynamic_cast.
|
|
*/
|
|
template <typename To, typename From>
|
|
std::enable_if_t<std::is_reference_v<To>, To> typeid_cast(From & from)
|
|
{
|
|
try
|
|
{
|
|
if ((typeid(From) == typeid(To)) || (typeid(from) == typeid(To)))
|
|
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);
|
|
}
|
|
|
|
|
|
template <typename To, typename From>
|
|
std::enable_if_t<std::is_pointer_v<To>, To> typeid_cast(From * from)
|
|
{
|
|
try
|
|
{
|
|
if ((typeid(From) == typeid(std::remove_pointer_t<To>)) || (typeid(*from) == typeid(std::remove_pointer_t<To>)))
|
|
return static_cast<To>(from);
|
|
else
|
|
return nullptr;
|
|
}
|
|
catch (const std::exception & e)
|
|
{
|
|
throw DB::Exception(e.what(), DB::ErrorCodes::BAD_CAST);
|
|
}
|
|
}
|
|
|
|
|
|
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);
|
|
}
|
|
}
|