ArmNN
 20.02
PolymorphicDowncast.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2020 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include "Assert.hpp"
9 
10 #include <armnn/Exceptions.hpp>
11 
12 #include <type_traits>
13 
14 namespace armnn
15 {
16 
17 // If we are testing then throw an exception, otherwise regular assert
18 #if defined(ARMNN_POLYMORPHIC_CAST_TESTABLE)
19 # define ARMNN_POLYMORPHIC_CAST_CHECK_METHOD(cond) ConditionalThrow<std::bad_cast>(cond)
20 #else
21 # define ARMNN_POLYMORPHIC_CAST_CHECK_METHOD(cond) ARMNN_ASSERT(cond)
22 #endif
23 
24 //Only check the condition if debug build or during testing
25 #if !defined(NDEBUG) || defined(ARMNN_POLYMORPHIC_CAST_TESTABLE)
26 # define ARMNN_POLYMORPHIC_CAST_CHECK(cond) ARMNN_POLYMORPHIC_CAST_CHECK_METHOD(cond)
27 #else
28 # define ARMNN_POLYMORPHIC_CAST_CHECK(cond) // release builds dont check the cast
29 #endif
30 
31 
32 template<typename DestType, typename SourceType>
33 DestType polymorphic_downcast(SourceType value)
34 {
35  static_assert(std::is_pointer<SourceType>::value &&
36  std::is_pointer<DestType>::value,
37  "polymorphic_downcast only works with pointer types.");
38 
39  ARMNN_POLYMORPHIC_CAST_CHECK(dynamic_cast<DestType>(value) == static_cast<DestType>(value));
40  return static_cast<DestType>(value);
41 }
42 
43 } //namespace armnn
DestType polymorphic_downcast(SourceType value)
Copyright (c) 2020 ARM Limited.
#define ARMNN_POLYMORPHIC_CAST_CHECK(cond)