#pragma once
#include <QtCore/QObject>
#include <type_traits>
namespace rust::cxxqt1 {
template<typename Sub, typename Base>
const Base*
upcastPtr(const Sub* sub)
{
static_assert(std::is_base_of_v<Base, Sub>);
return static_cast<const Base*>(sub);
}
template<typename Sub,
typename Base,
std::enable_if_t<!std::is_base_of_v<QObject, Sub>, bool> = true>
const Sub*
downcastPtr(const Base* base)
{
static_assert(std::is_base_of_v<Base, Sub>);
static_assert(std::is_polymorphic_v<Sub>,
"Downcasting requires a polymorphic type (e.g. a type with at "
"least one virtual method)!");
return dynamic_cast<const Sub*>(base);
}
template<typename Sub,
typename Base,
std::enable_if_t<std::is_base_of_v<QObject, Sub>, bool> = true>
const Sub*
downcastPtr(const Base* base)
{
static_assert(std::is_base_of_v<Base, Sub>);
return qobject_cast<const Sub*>(base);
}
template<typename Sub, typename Base>
const Sub*
downcastPtrStatic(const Base* base)
{
static_assert(std::is_base_of_v<Base, Sub>);
static_assert(sizeof(Base) == sizeof(Sub));
return static_cast<const Sub*>(base);
}
}