diff options
author | Derek Lamberti <derek.lamberti@arm.com> | 2020-02-19 13:30:47 +0000 |
---|---|---|
committer | Derek Lamberti <derek.lamberti@arm.com> | 2020-03-09 09:17:21 +0000 |
commit | 4d4e0e2530629ae9ecfcb379bf0f27c6b18b6891 (patch) | |
tree | 5240428061fe5c57ce1c762908166432bca7d0c5 /include/armnn | |
parent | e66448491b836049df62e63e1e5151eefe3bfcf8 (diff) | |
download | armnn-4d4e0e2530629ae9ecfcb379bf0f27c6b18b6891.tar.gz |
IVGCVSW-4483 Introduce polymorphic_downcast implementation
Change-Id: I958dd719162337eb5c7e71f4ac49dd5784564b1a
Signed-off-by: Derek Lamberti <derek.lamberti@arm.com>
Diffstat (limited to 'include/armnn')
-rw-r--r-- | include/armnn/Exceptions.hpp | 17 | ||||
-rw-r--r-- | include/armnn/utility/PolymorphicDowncast.hpp | 43 |
2 files changed, 60 insertions, 0 deletions
diff --git a/include/armnn/Exceptions.hpp b/include/armnn/Exceptions.hpp index cb62079b76..05b740614d 100644 --- a/include/armnn/Exceptions.hpp +++ b/include/armnn/Exceptions.hpp @@ -143,6 +143,13 @@ public: using Exception::Exception; }; +class PolymorphicDowncastException : public Exception +{ +public: + using Exception::Exception; +}; + + template <typename ExceptionType> void ConditionalThrow(bool condition, const std::string& message) { @@ -152,6 +159,16 @@ void ConditionalThrow(bool condition, const std::string& message) } } +template <typename ExceptionType> +void ConditionalThrow(bool condition) +{ + if (!condition) + { + throw ExceptionType(); + } +} + + /// /// ComparedType must support: /// operator==(const ComparedType&) diff --git a/include/armnn/utility/PolymorphicDowncast.hpp b/include/armnn/utility/PolymorphicDowncast.hpp new file mode 100644 index 0000000000..d529867474 --- /dev/null +++ b/include/armnn/utility/PolymorphicDowncast.hpp @@ -0,0 +1,43 @@ +// +// Copyright © 2020 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include "Assert.hpp" + +#include <armnn/Exceptions.hpp> + +#include <type_traits> + +namespace armnn +{ + +// If we are testing then throw an exception, otherwise regular assert +#if defined(ARMNN_POLYMORPHIC_CAST_TESTABLE) +# define ARMNN_POLYMORPHIC_CAST_CHECK_METHOD(cond) ConditionalThrow<std::bad_cast>(cond) +#else +# define ARMNN_POLYMORPHIC_CAST_CHECK_METHOD(cond) ARMNN_ASSERT(cond) +#endif + +//Only check the condition if debug build or during testing +#if !defined(NDEBUG) || defined(ARMNN_POLYMORPHIC_CAST_TESTABLE) +# define ARMNN_POLYMORPHIC_CAST_CHECK(cond) ARMNN_POLYMORPHIC_CAST_CHECK_METHOD(cond) +#else +# define ARMNN_POLYMORPHIC_CAST_CHECK(cond) // release builds dont check the cast +#endif + + +template<typename DestType, typename SourceType> +DestType polymorphic_downcast(SourceType value) +{ + static_assert(std::is_pointer<SourceType>::value && + std::is_pointer<DestType>::value, + "polymorphic_downcast only works with pointer types."); + + ARMNN_POLYMORPHIC_CAST_CHECK(dynamic_cast<DestType>(value) == static_cast<DestType>(value)); + return static_cast<DestType>(value); +} + +} //namespace armnn
\ No newline at end of file |