7#include "../utility/utility.hpp"
8#include "../macros.hpp"
15hi_export_module(hikogui.parser.placement);
22hi_warning_ignore_msvc(26492);
24hi_warning_ignore_msvc(26403);
25hi_warning_ignore_msvc(26460);
27hi_export
namespace hi::inline
v1 {
29inline bool check_alignment(
void const *ptr)
noexcept
31 return std::bit_cast<uintptr_t>(ptr) %
alignof(T) == 0;
37 static_assert(std::is_trivially_default_constructible_v<T>);
38 static_assert(std::is_trivially_destructible_v<T>);
42 template<
byte_like Byte>
45 if (offset +
sizeof(value_type) > bytes.size()) {
49 auto ptr = bytes.data() + offset;
50 offset +=
sizeof(value_type);
51 std::uninitialized_default_construct_n(
reinterpret_cast<value_type *
>(ptr), 1);
52 _ptr = std::launder(
reinterpret_cast<value_type *
>(ptr));
57 std::destroy_at(_ptr);
66 value_type *operator->()
const noexcept
71 value_type& operator*()
const noexcept
80template<
typename T,
typename Byte>
81[[nodiscard]]
auto make_placement_ptr(std::span<Byte> bytes,
std::size_t& offset)
86template<
typename T,
typename Byte>
87[[nodiscard]]
auto make_placement_ptr(std::span<Byte> bytes,
std::size_t&& offset = 0)
90 return make_placement_ptr<T>(bytes, _offset);
96 static_assert(std::is_trivially_default_constructible_v<T>);
97 static_assert(std::is_trivially_destructible_v<T>);
100 using container_type = std::span<value_type>;
101 using iterator = container_type::iterator;
102 using reference = container_type::reference;
103 using pointer = container_type::pointer;
105 template<
byte_like Byte>
108 if (offset + n *
sizeof(T) > bytes.size()) {
112 auto const ptr = bytes.data() + offset;
113 offset +=
sizeof(T) * n;
115 std::uninitialized_default_construct_n(
reinterpret_cast<value_type *
>(ptr), n);
116 _items = {std::launder(
reinterpret_cast<value_type *
>(ptr)), n};
126 std::destroy(begin(), end());
129 operator std::span<value_type>()
const noexcept
139 iterator begin()
const noexcept
141 return _items.begin();
144 iterator end()
const noexcept
149 reference operator[](ssize_t offset)
const noexcept
151 return _items[offset];
155 std::span<value_type> _items;
158template<
typename T,
byte_like Byte>
164template<
typename T,
byte_like Byte>
168 return make_placement_array<T>(bytes, _offset, n);
171template<
typename T,
byte_like Byte>
172[[nodiscard]]
auto make_placement_array(std::span<Byte> bytes,
std::size_t& offset)
174 auto const n = bytes.size() / ssizeof(T);
175 return make_placement_array<T>(bytes, offset, n);
178template<
typename T,
byte_like Byte>
179[[nodiscard]]
auto make_placement_array(std::span<Byte> bytes,
std::size_t&& offset = 0)
182 return make_placement_array<T>(bytes, _offset);
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition placement.hpp:35
Definition placement.hpp:94