From: Mikko Rasa Date: Sat, 17 Jun 2023 21:58:32 +0000 (+0300) Subject: Add a dynamic cast function for Owned X-Git-Url: https://git.tdb.fi/?a=commitdiff_plain;h=eaab9bb5ad98b2ef5987698cc5fc7eb7e39ee7b2;p=libs%2Fgame.git Add a dynamic cast function for Owned --- diff --git a/source/game/owned.h b/source/game/owned.h index 9b9d99c..e51244d 100644 --- a/source/game/owned.h +++ b/source/game/owned.h @@ -18,6 +18,9 @@ class Owned: public Handle template friend class Owned; + template + friend Owned dynamic_owned_cast(Owned &&); + public: Owned() = default; @@ -141,6 +144,20 @@ void Owned::destroy() pool.destroy(this->ptr); } + +template +Owned dynamic_owned_cast(Owned &&src) +{ + T *ptr = dynamic_cast(src.get()); + if(!ptr) + return Owned(); + + Owned result; + result.ptr = ptr; + src.ptr = nullptr; + return result; +} + } // namespace Msp::Game #endif