diff options
Diffstat (limited to 'arm_compute/core/utils')
-rw-r--r-- | arm_compute/core/utils/misc/Cast.h | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/arm_compute/core/utils/misc/Cast.h b/arm_compute/core/utils/misc/Cast.h index f6c91dd2de..5d9d1b0eca 100644 --- a/arm_compute/core/utils/misc/Cast.h +++ b/arm_compute/core/utils/misc/Cast.h @@ -41,7 +41,7 @@ namespace cast * * @param[in] v Value to cast * - * @return The casted type + * @return The casted value */ template <typename Target, typename Source> inline Target polymorphic_cast(Source *v) @@ -62,7 +62,7 @@ inline Target polymorphic_cast(Source *v) * * @param[in] v Value to cast * - * @return The casted type + * @return The casted value */ template <typename Target, typename Source> inline Target polymorphic_downcast(Source *v) @@ -70,6 +70,49 @@ inline Target polymorphic_downcast(Source *v) ARM_COMPUTE_ERROR_ON(dynamic_cast<Target>(v) != static_cast<Target>(v)); return static_cast<Target>(v); } + +/** Polymorphic cast between two unique pointer types + * + * @warning Will throw an exception if cast cannot take place + * + * @tparam Target Target to cast type + * @tparam Source Source from cast type + * @tparam Deleter Deleter function type + * + * @param[in] v Value to cast + * + * @return The casted value + */ +template <typename Target, typename Source, typename Deleter> +std::unique_ptr<Target, Deleter> polymorphic_cast_unique_ptr(std::unique_ptr<Source, Deleter> &&v) +{ + if(dynamic_cast<Target *>(v.get()) == nullptr) + { + throw std::bad_cast(); + } + auto r = static_cast<Target *>(v.release()); + return std::unique_ptr<Target, Deleter>(r, std::move(v.get_deleter())); +} + +/** Polymorphic down cast between two unique pointer types + * + * @warning Will assert if cannot take place + * + * @tparam Target Target to cast type + * @tparam Source Source from cast type + * @tparam Deleter Deleter function type + * + * @param[in] v Value to cast + * + * @return The casted value + */ +template <typename Target, typename Source, typename Deleter> +std::unique_ptr<Target, Deleter> polymorphic_downcast_unique_ptr(std::unique_ptr<Source, Deleter> &&v) +{ + ARM_COMPUTE_ERROR_ON(dynamic_cast<Target *>(v.get()) != static_cast<Target *>(v.get())); + auto r = static_cast<Target *>(v.release()); + return std::unique_ptr<Target, Deleter>(r, std::move(v.get_deleter())); +} } // namespace cast } // namespace utils } // namespace arm_compute |