Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarCasting.hpp
diff options
context:
space:
mode:
authorKae <80987908+Novaenia@users.noreply.github.com>2023-06-20 14:33:09 +1000
committerKae <80987908+Novaenia@users.noreply.github.com>2023-06-20 14:33:09 +1000
commit6352e8e3196f78388b6c771073f9e03eaa612673 (patch)
treee23772f79a7fbc41bc9108951e9e136857484bf4 /source/core/StarCasting.hpp
parent6741a057e5639280d85d0f88ba26f000baa58f61 (diff)
everything everywhere
all at once
Diffstat (limited to 'source/core/StarCasting.hpp')
-rw-r--r--source/core/StarCasting.hpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/source/core/StarCasting.hpp b/source/core/StarCasting.hpp
new file mode 100644
index 0000000..64305af
--- /dev/null
+++ b/source/core/StarCasting.hpp
@@ -0,0 +1,93 @@
+#ifndef STAR_CASTING_HPP
+#define STAR_CASTING_HPP
+
+#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)
+ throw PointerConvertException::format("Could not convert from nullptr to %s", typeid(Type).name());
+ else if (auto a = as<Type>(p))
+ return a;
+ else
+ throw PointerConvertException::format("Could not convert from %s to %s", typeid(*p).name(), typeid(Type).name());
+}
+
+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);
+}
+
+}
+
+#endif