#ifndef STAR_CASTING_HPP #define STAR_CASTING_HPP #include "StarException.hpp" #include "StarFormat.hpp" namespace Star { STAR_EXCEPTION(PointerConvertException, StarException); template bool is(Type2* p) { return (bool)dynamic_cast(p); } template bool is(Type2 const* p) { return (bool)dynamic_cast(p); } template bool is(shared_ptr const& p) { return (bool)dynamic_cast(p.get()); } template bool is(shared_ptr const& p) { return (bool)dynamic_cast(p.get()); } template bool ris(Type2& r) { return (bool)dynamic_cast(&r); } template bool ris(Type2 const& r) { return (bool)dynamic_cast(&r); } template Type1* as(Type2* p) { return dynamic_cast(p); } template Type1 const* as(Type2 const* p) { return dynamic_cast(p); } template shared_ptr as(shared_ptr const& p) { return dynamic_pointer_cast(p); } template shared_ptr as(shared_ptr const& p) { return dynamic_pointer_cast(p); } template auto convert(Ptr const& p) -> decltype(as(p)) { if (!p) throw PointerConvertException::format("Could not convert from nullptr to %s", typeid(Type).name()); else if (auto a = as(p)) return a; else throw PointerConvertException::format("Could not convert from %s to %s", typeid(*p).name(), typeid(Type).name()); } template Type1& rconvert(Type2& r) { return *dynamic_cast(&r); } template Type1 const& rconvert(Type2 const& r) { return *dynamic_cast(&r); } template weak_ptr asWeak(shared_ptr const& p) { return weak_ptr(p); } template weak_ptr asWeak(shared_ptr const& p) { return weak_ptr(p); } } #endif