2019-03-15 13:12:11 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
|
2019-03-15 17:22:19 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2019-03-15 13:12:11 +00:00
|
|
|
|
2019-03-15 17:09:14 +00:00
|
|
|
/* This base class adds public methods:
|
|
|
|
* - Derived * as<Derived>()
|
|
|
|
* - const Derived * as<Derived>() const
|
|
|
|
* - Derived & as<Derived &>()
|
|
|
|
* - const Derived & as<Derived &>() const
|
|
|
|
*/
|
|
|
|
|
2019-03-15 13:12:11 +00:00
|
|
|
template <class Base>
|
2019-03-15 17:22:19 +00:00
|
|
|
class TypePromotion
|
|
|
|
{
|
2019-03-15 13:12:11 +00:00
|
|
|
private:
|
|
|
|
/// Need a helper-struct to fight the lack of the function-template partial specialization.
|
2019-03-15 16:14:13 +00:00
|
|
|
template <class T, bool is_const, bool is_ref = std::is_reference_v<T>>
|
2019-03-15 13:12:11 +00:00
|
|
|
struct CastHelper;
|
|
|
|
|
|
|
|
template <class T>
|
2019-03-15 16:14:13 +00:00
|
|
|
struct CastHelper<T, false, true>
|
2019-03-15 13:12:11 +00:00
|
|
|
{
|
|
|
|
auto & value(Base * ptr) { return typeid_cast<T>(*ptr); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct CastHelper<T, true, true>
|
|
|
|
{
|
2019-03-15 16:14:13 +00:00
|
|
|
auto & value(const Base * ptr) { return typeid_cast<std::add_lvalue_reference_t<std::add_const_t<std::remove_reference_t<T>>>>(*ptr); }
|
2019-03-15 13:12:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct CastHelper<T, false, false>
|
|
|
|
{
|
|
|
|
auto * value(Base * ptr) { return typeid_cast<T *>(ptr); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
2019-03-15 16:14:13 +00:00
|
|
|
struct CastHelper<T, true, false>
|
2019-03-15 13:12:11 +00:00
|
|
|
{
|
2019-03-15 16:14:13 +00:00
|
|
|
auto * value(const Base * ptr) { return typeid_cast<std::add_const_t<T> *>(ptr); }
|
2019-03-15 13:12:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
template <class Derived>
|
2019-03-15 16:14:13 +00:00
|
|
|
auto as() -> std::invoke_result_t<decltype(&CastHelper<Derived, false>::value), CastHelper<Derived, false>, Base *>
|
2019-03-15 13:12:11 +00:00
|
|
|
{
|
|
|
|
// TODO: if we do downcast to base type, then just return |this|.
|
2019-03-15 16:14:13 +00:00
|
|
|
return CastHelper<Derived, false>().value(static_cast<Base *>(this));
|
2019-03-15 13:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
2019-03-15 16:14:13 +00:00
|
|
|
auto as() const -> std::invoke_result_t<decltype(&CastHelper<Derived, true>::value), CastHelper<Derived, true>, const Base *>
|
2019-03-15 13:12:11 +00:00
|
|
|
{
|
|
|
|
// TODO: if we do downcast to base type, then just return |this|.
|
2019-03-15 16:14:13 +00:00
|
|
|
return CastHelper<Derived, true>().value(static_cast<const Base *>(this));
|
2019-03-15 13:12:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-13 10:37:13 +00:00
|
|
|
}
|