ClickHouse/dbms/Common/typeid_cast.h

77 lines
1.9 KiB
C++
Raw Normal View History

#pragma once
#include <type_traits>
#include <typeinfo>
#include <typeindex>
2019-11-09 15:31:56 +00:00
#include <memory>
#include <string>
2019-11-09 15:31:56 +00:00
#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.
2017-05-07 20:25:26 +00:00
* In the rest, behaves like a dynamic_cast.
*/
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)
{
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);
}
2019-11-09 15:31:56 +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)
{
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>)))
return static_cast<To>(from);
else
return nullptr;
}
catch (const std::exception & e)
{
throw DB::Exception(e.what(), DB::ErrorCodes::BAD_CAST);
}
}
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);
}
}