From 4e5730e1fcef150ce2f13f52a278890589ca96ad Mon Sep 17 00:00:00 2001 From: rtk0c Date: Fri, 16 Apr 2021 16:49:28 -0700 Subject: More work on workflows - WorkflowStep -> WorkflowNode - Added initial kinds of WorkflowNode's --- core/src/Utils/Macros.hpp | 8 +++++++- core/src/Utils/RTTI.hpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ core/src/Utils/Variant.hpp | 27 +++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 core/src/Utils/RTTI.hpp create mode 100644 core/src/Utils/Variant.hpp (limited to 'core/src/Utils') diff --git a/core/src/Utils/Macros.hpp b/core/src/Utils/Macros.hpp index cb949b2..ab846f4 100644 --- a/core/src/Utils/Macros.hpp +++ b/core/src/Utils/Macros.hpp @@ -1,6 +1,12 @@ -#pragma once +#pragma once #define CONCAT_IMPL(a, b) a##b #define CONCAT(a, b) CONCAT_IMPL(a, b) #define UNIQUE_NAME(prefix) CONCAT(prefix, __LINE__) + +#if defined(_MSC_VER) +# define UNREACHABLE __assume(false) +#elif defined(__clang__) || defined(__GNUC__) +# define UUNREACHABLE __builtin_unreachable() +#endif diff --git a/core/src/Utils/RTTI.hpp b/core/src/Utils/RTTI.hpp new file mode 100644 index 0000000..bc0d289 --- /dev/null +++ b/core/src/Utils/RTTI.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include + +template +bool is_a(TBase* t) { + assert(t != nullptr); + return T::IsInstance(t); +} + +template +bool is_a_nullable(TBase* t) { + if (t) { + return is_a(t); + } else { + return false; + } +} + +template +T* dyn_cast(TBase* t) { + assert(t != nullptr); + if (T::IsInstance(t)) { + return static_cast(t); + } else { + return nullptr; + } +} + +template +const T* dyn_cast(const TBase* t) { + assert(t != nullptr); + if (T::IsInstance(t)) { + return static_cast(t); + } else { + return nullptr; + } +} + +template +T* dyn_cast_nullable(TBase* t) { + if (!t) return nullptr; + return dyn_cast(t); +} diff --git a/core/src/Utils/Variant.hpp b/core/src/Utils/Variant.hpp new file mode 100644 index 0000000..7fdb2dc --- /dev/null +++ b/core/src/Utils/Variant.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include +#include + +template +struct Overloaded : Ts... { using Ts::operator()...; }; +template +Overloaded(Ts...) -> Overloaded; + +template +struct VariantCastProxy { + std::variant v; + + template + operator std::variant() const { + return std::visit( + [](auto&& arg) -> std::variant { return arg; }, + v); + } +}; + +/// Use snake_case naming to align with `static_cast`, `dynamic_cast`, etc.. +template +auto variant_cast(std::variant v) -> VariantCastProxy { + return { std::move(v) }; +} -- cgit v1.3.1