IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
span.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/mem_data.hxx>
6#include <ice/mem_memory.hxx>
9#include <array>
10
11namespace ice
12{
13
15 template<typename Type>
17 {
18 using ValueType = Type;
20 using Iterator = Type*;
21 using ReverseIterator = std::reverse_iterator<Type*>;
22 using ConstIterator = Type const*;
23 using ConstReverseIterator = std::reverse_iterator<Type const*>;
26
29
30 constexpr Span() noexcept;
31 constexpr Span(ice::Span<Type>&& other) noexcept = default;
32 constexpr Span(ice::Span<Type> const& other) noexcept = default;
33 constexpr Span(Type* location, ice::ncount count) noexcept;
34 constexpr Span(Type* from, Type* to) noexcept;
35
36 template<ice::u64 Size>
37 constexpr Span(Type(&location)[Size]) noexcept;
38
39 constexpr auto operator=(ice::Span<Type>&& other) noexcept -> ice::Span<Type>& = default;
40 constexpr auto operator=(ice::Span<Type> const& other) noexcept -> ice::Span<Type>& = default;
41
42 // API Requirements Of: Contiguous Container
43 template<typename Self>
44 constexpr auto data(this Self& self) noexcept -> ice::container::ValuePtr<Self> { return self._data; }
45 constexpr auto size(this Span const& self) noexcept -> ice::ncount { return { self._count, sizeof(ValueType) }; }
46
47 // API Requirements Of: Data and Memory
48 constexpr auto data_view(this Span const& self) noexcept -> ice::Data;
49 constexpr auto memory_view(this Span const& self) noexcept -> ice::Memory
50 requires(not std::is_const_v<ValueType>);
51
52 // Implicit type conversions
53 constexpr operator ice::Span<Type const>() noexcept { return { _data, _count }; }
54 constexpr operator ice::Span<Type const>() const noexcept { return { _data, _count }; }
55 };
56
57 template<typename T> Span(ice::Span<T>&&) noexcept -> Span<T>;
58 template<typename T> Span(ice::Span<T> const&) noexcept -> Span<T>;
59 template<typename T, ContainerLogic Logic, template<typename, ContainerLogic> typename Container> Span(Container<T, Logic> const&) noexcept -> Span<T>;
60 template<typename T, ice::u64 Size> Span(T(&)[Size]) noexcept -> Span<T>;
61
62 template<typename Type, size_t Size>
63 static constexpr auto make_span(std::array<Type, Size>& std_array) noexcept -> Span<Type>;
64 template<typename Type, size_t Size>
65 static constexpr auto make_span(std::array<Type, Size> const& std_array) noexcept -> Span<Type const>;
66
67 template<typename Type>
68 constexpr Span<Type>::Span() noexcept
69 : _count{ 0 }
70 , _data{ nullptr }
71 { }
72
73 template<typename Type>
74 constexpr Span<Type>::Span(Type* location, ice::ncount count) noexcept
75 : _count{ count.native() }
76 , _data{ location }
77 { }
78
79 template<typename Type>
80 constexpr Span<Type>::Span(Type* from, Type* to) noexcept
81 : _count{ static_cast<ice::u64>(to - from) }
82 , _data{ from }
83 { }
84
85 template<typename Type>
86 template<ice::u64 Size>
87 constexpr Span<Type>::Span(Type(&location)[Size]) noexcept
88 : _count{ Size }
89 , _data{ location }
90 { }
91
92 template<typename Type>
93 inline constexpr auto Span<Type>::data_view(this Span const& self) noexcept -> ice::Data
94 {
95 return ice::Data{
96 .location = self.data(),
97 .size = self.size(),
98 .alignment = ice::align_of<Type>
99 };
100 }
101
102 template<typename Type>
103 inline constexpr auto Span<Type>::memory_view(this Span const& self) noexcept -> ice::Memory
104 requires(not std::is_const_v<ValueType>)
105 {
106 return ice::Memory{
107 .location = self.data(),
108 .size = self.size(),
109 .alignment = ice::align_of<Type>
110 };
111 }
112
113 template<typename Type, size_t Size>
114 inline constexpr auto make_span(std::array<Type, Size>& std_array) noexcept -> Span<Type>
115 {
116 return Span<Type>{ std_array.data(), std_array.size() };
117 }
118
119 template<typename Type, size_t Size>
120 inline constexpr auto make_span(std::array<Type, Size> const& std_array) noexcept -> Span<Type const>
121 {
122 return Span<Type const>{ std_array.data(), std_array.size() };
123 }
124
126
127 // TODO: Move to a data reader type
128 namespace data
129 {
130
131 template<typename T>
132 requires (std::is_trivially_copyable_v<T> && !std::is_pointer_v<T>)
133 inline auto read_span(
134 ice::Data source,
136 ice::Span<T const>& out_value
137 ) noexcept -> ice::Data
138 {
140 out_value._count = count;
141 out_value._data = reinterpret_cast<T const*>(source.location);
142
143 source.location = out_value.data() + count;
144 source.size = ice::usize::subtract(source.size, out_value.size());
146 return source;
147 }
148
149 } // namespace data
150
151} // namespace ice
#define ICE_ASSERT_CORE(expression)
Definition assert_core.hxx:43
A concept that ensures only types that can be trivially copyable can be 'forced' to use trifial Logic...
Definition container_logic.hxx:25
Definition associative_container.hxx:8
Definition span.hxx:129
auto read_span(ice::Data source, ice::ncount count, ice::Span< T const > &out_value) noexcept -> ice::Data
Definition span.hxx:133
SPDX-License-Identifier: MIT.
Definition array.hxx:12
Span(ice::Span< T > &&) noexcept -> Span< T >
ContainerLogic
The logic implemented by a collectiont type when working with data. (Copying, Moving,...
Definition container_logic.hxx:13
std::uint64_t u64
Definition types.hxx:27
constexpr ice::ualign align_of
Definition mem_info.hxx:15
constexpr auto count(T const (&)[Size]) noexcept -> ice::u32
Definition base.hxx:43
static constexpr auto make_span(std::array< Type, Size > &std_array) noexcept -> Span< Type >
Definition span.hxx:114
Definition mem_data.hxx:17
ice::ualign alignment
Definition mem_data.hxx:20
void const * location
Definition mem_data.hxx:18
ice::usize size
Definition mem_data.hxx:19
Definition mem_memory.hxx:13
A view into an array of objects laid out in contiguous memory.
Definition span.hxx:17
ice::container::ConstCorrectContainerValueType< ContainerT > const * ConstIterator
Definition span.hxx:22
constexpr Span() noexcept
Definition span.hxx:68
constexpr auto memory_view(this Span const &self) noexcept -> ice::Memory requires(not std::is_const_v< ValueType >)
Definition span.hxx:103
std::reverse_iterator< ice::container::ConstCorrectContainerValueType< ContainerT > const * > ConstReverseIterator
Definition span.hxx:23
ice::container::ConstCorrectContainerValueType< ContainerT > ConstContainerValueType
Definition span.hxx:19
std::reverse_iterator< ice::container::ConstCorrectContainerValueType< ContainerT > * > ReverseIterator
Definition span.hxx:21
ice::container::ConstCorrectContainerValueType< ContainerT > * Iterator
Definition span.hxx:20
ice::container::ConstCorrectContainerValueType< ContainerT > ValueType
Definition span.hxx:18
constexpr auto data(this Self &self) noexcept -> ice::container::ValuePtr< Self >
Definition span.hxx:44
ice::concepts::ContiguousContainerTag ContainerTag
Definition span.hxx:25
constexpr auto size(this Span const &self) noexcept -> ice::ncount
Definition span.hxx:45
constexpr auto data_view(this Span const &self) noexcept -> ice::Data
Definition span.hxx:93
Definition container_concepts.hxx:56
Definition contiguous_container.hxx:14
Definition ncount.hxx:14
ice::detail::nvalue_base_utype base_type
Definition nvalue.hxx:75
static constexpr auto subtract(ice::usize left, ice::usize right) noexcept -> ice::usize
Perform a checked subtraction, ensuring 'left' is greater or equal to 'right'.
Definition mem_size_types.hxx:94