ClickHouse/dbms/src/Common/typeid_cast.h

50 lines
1.1 KiB
C++
Raw Normal View History

#pragma once
#include <type_traits>
#include <typeinfo>
#include <typeindex>
#include <string>
#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)
{
if (typeid(from) == typeid(To))
return static_cast<To>(from);
else
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>
To typeid_cast(From * from)
{
try
{
if (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);
}
}