osb/source/core/StarCasting.hpp

91 lines
2.2 KiB
C++
Raw Permalink Normal View History

#pragma once
2023-06-20 04:33:09 +00:00
#include "StarException.hpp"
#include "StarFormat.hpp"
namespace Star {
STAR_EXCEPTION(PointerConvertException, StarException);
template <typename Type1, typename Type2>
bool is(Type2* p) {
return (bool)dynamic_cast<Type1*>(p);
}
template <typename Type1, typename Type2>
bool is(Type2 const* p) {
return (bool)dynamic_cast<Type1 const*>(p);
}
template <typename Type1, typename Type2>
bool is(shared_ptr<Type2> const& p) {
return (bool)dynamic_cast<Type1*>(p.get());
}
template <typename Type1, typename Type2>
bool is(shared_ptr<Type2 const> const& p) {
return (bool)dynamic_cast<Type1 const*>(p.get());
}
template <typename Type1, typename Type2>
bool ris(Type2& r) {
return (bool)dynamic_cast<Type1*>(&r);
}
template <typename Type1, typename Type2>
bool ris(Type2 const& r) {
return (bool)dynamic_cast<Type1 const*>(&r);
}
template <typename Type1, typename Type2>
Type1* as(Type2* p) {
return dynamic_cast<Type1*>(p);
}
template <typename Type1, typename Type2>
Type1 const* as(Type2 const* p) {
return dynamic_cast<Type1 const*>(p);
}
template <typename Type1, typename Type2>
shared_ptr<Type1> as(shared_ptr<Type2> const& p) {
return dynamic_pointer_cast<Type1>(p);
}
template <typename Type1, typename Type2>
shared_ptr<Type1 const> as(shared_ptr<Type2 const> const& p) {
return dynamic_pointer_cast<Type1 const>(p);
}
template <typename Type, typename Ptr>
auto convert(Ptr const& p) -> decltype(as<Type>(p)) {
if (!p)
2023-06-27 10:23:44 +00:00
throw PointerConvertException::format("Could not convert from nullptr to {}", typeid(Type).name());
2023-06-20 04:33:09 +00:00
else if (auto a = as<Type>(p))
return a;
else
2023-06-27 10:23:44 +00:00
throw PointerConvertException::format("Could not convert from {} to {}", typeid(*p).name(), typeid(Type).name());
2023-06-20 04:33:09 +00:00
}
template <typename Type1, typename Type2>
Type1& rconvert(Type2& r) {
return *dynamic_cast<Type1*>(&r);
}
template <typename Type1, typename Type2>
Type1 const& rconvert(Type2 const& r) {
return *dynamic_cast<Type1 const*>(&r);
}
template <typename Type>
weak_ptr<Type> asWeak(shared_ptr<Type> const& p) {
return weak_ptr<Type>(p);
}
template <typename Type>
weak_ptr<Type const> asWeak(shared_ptr<Type const> const& p) {
return weak_ptr<Type>(p);
}
}