stellarlib 0.1.0
Loading...
Searching...
No Matches
type_traits.hpp
1/* clang-format off */
2
3/*
4 stellarlib
5 Copyright (C) 2025-2026 Domán Zana
6
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for any damages
9 arising from the use of this software.
10
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute it
13 freely, subject to the following restrictions:
14
15 1. The origin of this software must not be misrepresented; you must not
16 claim that you wrote the original software. If you use this software
17 in a product, an acknowledgment in the product documentation would be
18 appreciated but is not required.
19 2. Altered source versions must be plainly marked as such, and must not be
20 misrepresented as being the original software.
21 3. This notice may not be removed or altered from any source distribution.
22*/
23
24#ifndef STELLARLIB_EXT_TYPE_TRAITS_HPP
25#define STELLARLIB_EXT_TYPE_TRAITS_HPP
26
27#include <array>
28#include <cstddef>
29#include <tuple>
30#include <type_traits>
31
32/**
33 * @brief Standard library extensions
34 */
35namespace stellarlib::ext
36{
37/**
38 * @brief Expands a parameter pack into a repeated type
39 * @tparam Pack Parameter pack used to drive expansion
40 * @tparam T Type to substitute for each element
41 */
42template <typename Pack, typename T>
43using expand_as = T;
44
45/**
46 * @brief Expands a parameter pack into a repeated value
47 * @tparam Pack Parameter pack used to drive expansion
48 * @tparam VALUE Value to substitute for each element
49 */
50template <typename Pack, auto VALUE>
51static constexpr auto expand_as_v{VALUE};
52
53/**
54 * @brief Evaluates whether T is eligible for bit-wise relocation
55 * @tparam T Type to evaluate
56 */
57template <typename T>
58using is_trivially_relocatable = std::bool_constant<std::is_trivially_move_constructible_v<T> && std::is_trivially_destructible_v<T>>;
59
60/**
61 * @brief Evaluates whether T is eligible for bit-wise relocation
62 * @tparam T Type to evaluate
63 */
64template <typename T>
66
67/**
68 * @brief Compile-time padding for explicit field layout alignment
69 * @tparam AlignTo Type whose alignment is used as the target
70 * @tparam Fields Types whose combined size is being padded
71 */
72template <typename AlignTo, typename ...Fields>
73struct padding final
74{
75 /**
76 * @brief Size of the padding in bytes
77 */
78 static constexpr std::size_t size{(alignof(AlignTo) - (sizeof(Fields) + ...) % alignof(AlignTo)) % alignof(AlignTo)};
79
80 /**
81 * @brief Padding bytes
82 */
83 [[no_unique_address]] std::conditional_t<static_cast<bool>(size), std::array<std::byte, size>, std::tuple<>> bytes;
84};
85}
86
87#endif
Standard library extensions.
Definition bit.hpp:33
T expand_as
Expands a parameter pack into a repeated type.
Definition type_traits.hpp:43
std::bool_constant< std::is_trivially_move_constructible_v< T > &&std::is_trivially_destructible_v< T > > is_trivially_relocatable
Evaluates whether T is eligible for bit-wise relocation.
Definition type_traits.hpp:58
constexpr bool is_trivially_relocatable_v
Evaluates whether T is eligible for bit-wise relocation.
Definition type_traits.hpp:65
Compile-time padding for explicit field layout alignment.
Definition type_traits.hpp:74
std::conditional_t< static_cast< bool >(size), std::array< std::byte, size >, std::tuple<> > bytes
Padding bytes.
Definition type_traits.hpp:83
static constexpr std::size_t size
Size of the padding in bytes.
Definition type_traits.hpp:78