| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_BASE_POINTER_HPP |
| 4 |
#define MPT_BASE_POINTER_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/macros.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
|
| 11 |
#include <cstddef> |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
namespace mpt { |
| 16 |
inline namespace MPT_INLINE_NS { |
| 17 |
|
| 18 |
|
| 19 |
inline constexpr int arch_bits = sizeof(void *) * 8; |
| 20 |
inline constexpr std::size_t pointer_size = sizeof(void *); |
| 21 |
|
| 22 |
|
| 23 |
template <typename Tdst, typename Tsrc> |
| 24 |
struct pointer_cast_helper { |
| 25 |
static constexpr Tdst cast(const Tsrc & src) noexcept { |
| 26 |
return src; |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
template <typename Tdst, typename Tptr> |
| 31 |
struct pointer_cast_helper<Tdst, const Tptr *> { |
| 32 |
static constexpr Tdst cast(const Tptr * const & src) noexcept { |
| 33 |
return reinterpret_cast<const Tdst>(src); |
| 34 |
} |
| 35 |
}; |
| 36 |
template <typename Tdst, typename Tptr> |
| 37 |
struct pointer_cast_helper<Tdst, Tptr *> { |
| 38 |
static constexpr Tdst cast(const Tptr * const & src) noexcept { |
| 39 |
return reinterpret_cast<const Tdst>(src); |
| 40 |
} |
| 41 |
}; |
| 42 |
|
| 43 |
|
| 44 |
template <typename Tdst, typename Tsrc> |
| 45 |
constexpr Tdst pointer_cast(const Tsrc & src) noexcept { |
| 46 |
return pointer_cast_helper<Tdst, Tsrc>::cast(src); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
template <typename T> |
| 51 |
class void_ptr { |
| 52 |
private: |
| 53 |
T * m_ptr = nullptr; |
| 54 |
public: |
| 55 |
MPT_FORCEINLINE explicit void_ptr(void * ptr) |
| 56 |
: m_ptr(reinterpret_cast<T *>(ptr)) { |
| 57 |
return; |
| 58 |
} |
| 59 |
MPT_FORCEINLINE T & operator*() { |
| 60 |
return *m_ptr; |
| 61 |
} |
| 62 |
MPT_FORCEINLINE T * operator->() { |
| 63 |
return m_ptr; |
| 64 |
} |
| 65 |
MPT_FORCEINLINE operator void *() { |
| 66 |
return m_ptr; |
| 67 |
} |
| 68 |
}; |
| 69 |
|
| 70 |
|
| 71 |
} // namespace MPT_INLINE_NS |
| 72 |
} // namespace mpt |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
#endif // MPT_BASE_POINTER_HPP |