diff options
author | Jan Eilers <jan.eilers@arm.com> | 2020-04-10 12:40:24 +0100 |
---|---|---|
committer | Jim Flynn <jim.flynn@arm.com> | 2020-04-10 18:17:02 +0000 |
commit | 76bc728bc1681ed216ffe6f7720f3f57b5137fab (patch) | |
tree | 0363480577a7e0dbb3759e66cad19debed62170f /include | |
parent | dc0c6ed9f8b993e63f492f203d7d7080ab4c835c (diff) | |
download | armnn-76bc728bc1681ed216ffe6f7720f3f57b5137fab.tar.gz |
IVGCVSW-4483 Introduce PolymorphicPointerDowncast
* as replacement for boost::polymorphic_pointer_downcast
* added PolymorphicPointerDowncast
* added related unit test
* added description to PolymorphicDowncast
Signed-off-by: Jan Eilers <jan.eilers@arm.com>
Change-Id: I47e94344c1c21941865549a5632cfb7cad804d35
Diffstat (limited to 'include')
-rw-r--r-- | include/armnn/utility/PolymorphicDowncast.hpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/include/armnn/utility/PolymorphicDowncast.hpp b/include/armnn/utility/PolymorphicDowncast.hpp index b4a5cad314..8f052370e5 100644 --- a/include/armnn/utility/PolymorphicDowncast.hpp +++ b/include/armnn/utility/PolymorphicDowncast.hpp @@ -9,6 +9,7 @@ #include <armnn/Exceptions.hpp> +#include <memory> #include <type_traits> namespace armnn @@ -29,6 +30,46 @@ namespace armnn #endif +namespace utility +{ +// static_pointer_cast overload for std::shared_ptr +template <class T1, class T2> +std::shared_ptr<T1> StaticPointerCast (const std::shared_ptr<T2>& sp) +{ + return std::static_pointer_cast<T1>(sp); +} + +// dynamic_pointer_cast overload for std::shared_ptr +template <class T1, class T2> +std::shared_ptr<T1> DynamicPointerCast (const std::shared_ptr<T2>& sp) +{ + return std::dynamic_pointer_cast<T1>(sp); +} + +// static_pointer_cast overload for raw pointers +template<class T1, class T2> +inline T1* StaticPointerCast(T2 *ptr) +{ + return static_cast<T1*>(ptr); +} + +// dynamic_pointer_cast overload for raw pointers +template<class T1, class T2> +inline T1* DynamicPointerCast(T2 *ptr) +{ + return dynamic_cast<T1*>(ptr); +} + +} // namespace utility + +/// Polymorphic downcast for build in pointers only +/// +/// Usage: Child* pChild = PolymorphicDowncast<Child*>(pBase); +/// +/// \tparam DestType Pointer type to the target object (Child pointer type) +/// \tparam SourceType Pointer type to the source object (Base pointer type) +/// \param value Pointer to the source object +/// \return Pointer of type DestType (Pointer of type child) template<typename DestType, typename SourceType> DestType PolymorphicDowncast(SourceType value) { @@ -40,4 +81,21 @@ DestType PolymorphicDowncast(SourceType value) return static_cast<DestType>(value); } + +/// Polymorphic downcast for shared pointers and build in pointers +/// +/// Usage: auto pChild = PolymorphicPointerDowncast<Child>(pBase) +/// +/// \tparam DestType Type of the target object (Child type) +/// \tparam SourceType Pointer type to the source object (Base (shared) pointer type) +/// \param value Pointer to the source object +/// \return Pointer of type DestType ((Shared) pointer of type child) +template<typename DestType, typename SourceType> +auto PolymorphicPointerDowncast(const SourceType& value) +{ + ARMNN_POLYMORPHIC_CAST_CHECK(utility::DynamicPointerCast<DestType>(value) + == utility::StaticPointerCast<DestType>(value)); + return utility::StaticPointerCast<DestType>(value); +} + } //namespace armnn
\ No newline at end of file |