aboutsummaryrefslogtreecommitdiff
path: root/source/10-common/RTTI.hpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2023-09-20 23:58:58 -0700
committerrtk0c <[email protected]>2023-09-20 23:58:58 -0700
commitf138311d2d2e0cc9ba0496d523bb46f2c1c9fb73 (patch)
treef96100a813a4ffb28dcd074455d3a2f8ee426430 /source/10-common/RTTI.hpp
Copy from the PlasticSCM repo, replace vendored glm wtih conan
Diffstat (limited to 'source/10-common/RTTI.hpp')
-rw-r--r--source/10-common/RTTI.hpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/source/10-common/RTTI.hpp b/source/10-common/RTTI.hpp
new file mode 100644
index 0000000..bd9475b
--- /dev/null
+++ b/source/10-common/RTTI.hpp
@@ -0,0 +1,44 @@
+#pragma once
+
+#include <cassert>
+
+template <typename T, typename TBase>
+bool is_a(TBase* t) {
+ assert(t != nullptr);
+ return T::IsInstance(t);
+}
+
+template <typename T, typename TBase>
+bool is_a_nullable(TBase* t) {
+ if (t) {
+ return is_a<T, TBase>(t);
+ } else {
+ return false;
+ }
+}
+
+template <typename T, typename TBase>
+T* dyn_cast(TBase* t) {
+ assert(t != nullptr);
+ if (T::IsInstance(t)) {
+ return static_cast<T*>(t);
+ } else {
+ return nullptr;
+ }
+}
+
+template <typename T, typename TBase>
+const T* dyn_cast(const TBase* t) {
+ assert(t != nullptr);
+ if (T::IsInstance(t)) {
+ return static_cast<const T*>(t);
+ } else {
+ return nullptr;
+ }
+}
+
+template <typename T, typename TBase>
+T* dyn_cast_nullable(TBase* t) {
+ if (!t) return nullptr;
+ return dyn_cast<T, TBase>(t);
+}