aboutsummaryrefslogtreecommitdiff
path: root/include/armnn/utility/PolymorphicDowncast.hpp
blob: d529867474d92220041f99ecd3df6d9939633177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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