KTL
Loading...
Searching...
No Matches
packed_ptr_fwd.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <type_traits>
5
6namespace ktl
7{
8 namespace detail
9 {
10 template<typename T>
11 constexpr T min_range(size_t bits)
12 {
13 if constexpr (std::is_unsigned_v<T> || std::is_enum_v<T>)
14 return static_cast<T>(0ULL);
15 else
16 return static_cast<T>(-(1LL << (bits - 1LL)));
17 }
18
19 template<typename T>
20 constexpr T max_range(size_t bits)
21 {
22 if constexpr (std::is_unsigned_v<T> || std::is_enum_v<T>)
23 return static_cast<T>((1ULL << bits) - 1ULL);
24 else
25 return static_cast<T>(((1LL << bits) - 1LL) - (1LL << (bits - 1LL)));
26 }
27
28
29 // Function pointer types
30 template<typename T, typename = void>
31 struct is_function_pointer : std::false_type {};
32
33 template<typename R, typename... Ts>
34 struct is_function_pointer<R(*)(Ts...), std::void_t<R(*)(Ts...)>> : std::true_type {};
35
36 template<typename T>
38
39
40 // Get underlying type for alignment
41 template<typename T, typename = void>
43 {
44 using type = T;
45 };
46
47 // Enum types
48 template<typename T>
49 struct underlying_type<T, std::enable_if_t<std::is_enum_v<T>>>
50 {
51 using type = std::underlying_type_t<T>;
52 };
53
54 // Pointer types, but not function pointers
55 template<typename T>
56 struct underlying_type<T, std::enable_if_t<std::is_pointer_v<T> && !is_function_pointer_v<T>>>
57 {
58 using type = std::remove_pointer_t<T>;
59 };
60
61 template<typename T>
63 }
64
65 template<
66 typename PtrT,
67 typename IntT,
70 size_t Alignment = alignof(detail::underlying_type_t<PtrT>)>
71 class packed_ptr;
72}
A packed pointer-like type which takes advantage of any bits that don't get used due to alignment.
Definition packed_ptr.h:18
typename underlying_type< T >::type underlying_type_t
Definition packed_ptr_fwd.h:62
constexpr uintmax_t log2(uintmax_t n) noexcept
Definition bits.h:7
constexpr bool has_no_value_type_v
Definition meta.h:17
constexpr T min_range(size_t bits)
Definition packed_ptr_fwd.h:11
constexpr bool is_function_pointer_v
Definition packed_ptr_fwd.h:37
constexpr T max_range(size_t bits)
Definition packed_ptr_fwd.h:20
Definition cascading.h:16
Definition packed_ptr_fwd.h:31
std::underlying_type_t< T > type
Definition packed_ptr_fwd.h:51
Definition packed_ptr_fwd.h:43
T type
Definition packed_ptr_fwd.h:44