1 |
|
2 |
|
3 |
|
4 | #pragma once
|
5 | #include <type_traits>
|
6 | #include <utility>
|
7 |
|
8 | namespace Microsoft::Common::Utilities {
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | template <typename TResultTypePtr, typename TOriginalTypePtr>
|
19 | inline TResultTypePtr CheckedReinterpretCast(TOriginalTypePtr p) noexcept {
|
20 | using TResultType = typename std::remove_pointer<TResultTypePtr>::type;
|
21 | using TOriginalType = typename std::remove_pointer<TOriginalTypePtr>::type;
|
22 |
|
23 | static_assert(
|
24 | std::is_pointer<TResultTypePtr>::value && std::is_pointer<TOriginalTypePtr>::value &&
|
25 | std::is_integral<TResultType>::value && std::is_integral<TOriginalType>::value &&
|
26 | sizeof(TResultType) == sizeof(TOriginalType),
|
27 | "CheckedReinterpretCast can only be used to cast from T1* to T2*, where "
|
28 | "T1 and T2 are integral types of the same size.");
|
29 |
|
30 | return reinterpret_cast<TResultTypePtr>(p);
|
31 | }
|
32 |
|
33 |
|
34 | template <typename T, std::size_t N>
|
35 | constexpr std::size_t ArraySize(T(&)[N]) noexcept {
|
36 | return N;
|
37 | }
|
38 |
|
39 | }
|