#pragma once #include #include #include #include namespace geode::cast { /** * Alias for static_cast */ template static constexpr T as(F const v) { return static_cast(v); } /** * Cast from anything to anything else, * provided they are the same size */ template static constexpr T union_cast(F v) { static_assert(sizeof(F) == sizeof(T), "union_cast: R and T don't match in size!"); union { T t; F f; } x; x.f = v; return x.t; } /** * Reference casting. Does a pointer-to-pointer * cast but uses reference syntactic sugar to * look cleaner. */ template static constexpr T reference_cast(F v) { return reinterpret_cast(v); } /** * Cast an adjusted this pointer to it's base pointer */ template static constexpr T base_cast(F obj) { return reinterpret_cast(dynamic_cast(obj)); } /** * Cast based on RTTI. This is a replacement for * dynamic_cast, since it doesn't work for gd. */ template static T typeid_cast(F obj) { if (std::string(typeid(*obj).name()) == typeid(std::remove_pointer_t).name()) return reinterpret_cast(obj); else return nullptr; } }