GLSL Struct 1.4.8
glslstruct
Loading...
Searching...
No Matches
array_traits.hpp
1/*
2 * glslstruct - a C++ library designed to easily represent GLSL's Uniform Buffer Objects (UBOs) and Shader Storage Buffer Objects (SSBOs) in C++.
3 *
4 * Licensed under the BSD 3-Clause License with Attribution Requirement.
5 * See the LICENSE file for details: https://github.com/MAIPA01/glslstruct/blob/main/LICENSE
6 *
7 * Copyright (c) 2025, Patryk Antosik (MAIPA01)
8 */
9
10#pragma once
11#ifndef _GLSL_STRUCT_ARRAY_TRAITS_HPP_
12 #define _GLSL_STRUCT_ARRAY_TRAITS_HPP_
13
14 #include <glslstruct/config.hpp>
15
17_GLSL_STRUCT_ERROR("This is only available for c++17 and greater!");
18 #else
19
20 #include <glslstruct/libs.hpp>
21
22namespace glslstruct::utils {
23 #pragma region ARRAY_TRAITS
24
25 /**
26 * @brief Array Traits
27 * @ingroup utils
28 * @tparam T array type
29 */
30 template<class T>
31 struct array_traits {};
32
33 /**
34 * @brief Array Traits overload for std::vector
35 * @ingroup utils
36 * @tparam T value type
37 */
38 template<class T>
39 struct array_traits<std::vector<T> > {
40 /// @brief value type
41 using value_type = T;
42
43 /// @brief pointer to vector data
44 static _GLSL_STRUCT_CONSTEXPR17 const T* data(const std::vector<T>& vec) { return vec.data(); }
45
46 /// @brief size of vector
47 static _GLSL_STRUCT_CONSTEXPR17 size_t size(const std::vector<T>& vec) { return vec.size(); }
48 };
49
50 /**
51 * @brief Array Traits overload for std::array
52 * @ingroup utils
53 * @tparam T value type
54 * @tparam N array size
55 */
56 template<class T, size_t N>
57 struct array_traits<std::array<T, N> > {
58 /// @brief value type
59 using value_type = T;
60
61 /// @brief static size of array
62 static _GLSL_STRUCT_CONSTEXPR17 size_t static_size = N;
63
64 /// @brief pointer to array data
65 static _GLSL_STRUCT_CONSTEXPR17 const T* data(const std::array<T, N>& array) { return array.data(); }
66
67 /// @brief array size
68 static _GLSL_STRUCT_CONSTEXPR17 size_t size(const std::array<T, N>& array) { return array.size(); }
69 };
70
71 /**
72 * @brief Array Traits overload for c-style array
73 * @ingroup utils
74 * @tparam T value type
75 * @tparam N array size
76 */
77 template<class T, size_t N>
78 struct array_traits<T[N]> {
79 /// @brief value type
80 using value_type = T;
81
82 /// @brief static size of array
83 static _GLSL_STRUCT_CONSTEXPR17 size_t static_size = N;
84
85 /// @brief pointer to c-style array data
86 static _GLSL_STRUCT_CONSTEXPR17 const T* data(const T (&array)[N]) { return array; }
87
88 /// @brief c-style array size
89 static _GLSL_STRUCT_CONSTEXPR17 size_t size(const T (&)[N]) { return N; }
90 };
91
92 /**
93 * @brief Array Traits overload for pointer to c-style array
94 * @ingroup utils
95 * @tparam T value type
96 * @tparam N array size
97 */
98 template<class T, size_t N>
99 struct array_traits<T (*)[N]> {
100 /// @brief value type
101 using value_type = T;
102
103 /// @brief static size of array
104 static _GLSL_STRUCT_CONSTEXPR17 size_t static_size = N;
105
106 /// @brief pointer to c-style array data
107 static _GLSL_STRUCT_CONSTEXPR17 const T* data(const T (*array)[N]) { return *array; }
108
109 /// @brief c-style array size
110 static _GLSL_STRUCT_CONSTEXPR17 size_t size(const T (*)[N]) { return N; }
111 };
112
114 /**
115 * @brief Array Traits overload for std::span
116 * @ingroup utils
117 * @tparam T value type
118 */
119 template<class T>
120 struct array_traits<std::span<T> > {
121 /// @brief value type
122 using value_type = T;
123
124 /// @brief pointer to span data
125 static _GLSL_STRUCT_CONSTEXPR17 const T* data(const std::span<const T> span) { return span.data(); }
126
127 /// @brief span size
128 static _GLSL_STRUCT_CONSTEXPR17 size_t size(const std::span<const T> span) { return span.size(); }
129 };
130 #endif
131 #pragma endregion
132
133 #pragma region ARRAY_CHECKS
134 /**
135 * @var bool is_array_v
136 * @brief checks if T has array traits defined
137 * @ingroup utils
138 */
139
140 /**
141 * @var bool is_static_size_array_v
142 * @brief checks if T has array traits defined and additional static_size member of type size_t
143 * @ingroup utils
144 */
145
147 /**
148 * @brief Array concept
149 * @ingroup utils
150 */
151 template<class T>
152 concept array = requires (const T& value) {
153 typename array_traits<T>::value_type;
154 { array_traits<T>::data(value) } -> std::same_as<const typename array_traits<T>::value_type*>;
155 { array_traits<T>::size(value) } -> std::same_as<size_t>;
156 };
157
158 /**
159 * @brief Static size array concept
160 * @ingroup utils
161 */
162 template<class T>
163 concept static_size_array = array<T> && requires {
164 { array_traits<T>::static_size } -> std::convertible_to<size_t>;
165 };
166
167 template<class T>
168 static _GLSL_STRUCT_CONSTEXPR17 bool is_array_v = array<T>;
169
170 template<class T>
171 static _GLSL_STRUCT_CONSTEXPR17 bool is_static_size_array_v = static_size_array<T>;
172 #else
173 /**
174 * @brief Is Array Test Struct
175 * @ingroup utils
176 */
177 template<class T, class = void>
178 struct is_array : std::false_type {};
179
180 template<class T>
181 struct is_array<T, std::void_t<typename array_traits<T>::value_type,
182 std::enable_if_t<std::is_same_v<decltype(array_traits<T>::data(std::declval<const T&>())),
183 const typename array_traits<T>::value_type*> >,
184 std::enable_if_t<std::is_same_v<decltype(array_traits<T>::size(std::declval<const T&>())), size_t> > > >
185 : std::true_type {};
186
187 template<class T>
188 static _GLSL_STRUCT_CONSTEXPR17 bool is_array_v = is_array<T>::value;
189
190 /**
191 * @brief Is Static Size Array Test Struct
192 * @ingroup utils
193 */
194 template<class T, class = void>
195 struct is_static_size_array : std::false_type {};
196
197 template<class T>
198 struct is_static_size_array<T, std::void_t<std::enable_if_t<is_array_v<T> >,
199 std::enable_if_t<std::is_convertible_v<decltype(array_traits<T>::static_size), size_t> > > >
200 : std::true_type {};
201
202 template<class T>
203 static _GLSL_STRUCT_CONSTEXPR17 bool is_static_size_array_v = is_static_size_array<T>::value;
204 #endif
205 #pragma endregion
206
207 #pragma region ARRAY_TRAITS_VALUES
208 /**
209 * @brief get value_type of array
210 * @ingroup utils
211 */
213 template<array T>
214 #else
215 template<class T, std::enable_if_t<is_array_v<T>, bool> = true>
216 #endif
217 using array_value_type_t = _GLSL_STRUCT_TYPENAME17 array_traits<T>::value_type;
218
219 /**
220 * @brief get static_size of static size array
221 * @ingroup utils
222 */
224 template<static_size_array T>
225 #else
226 template<class T, std::enable_if_t<is_static_size_array_v<T>, bool> = true>
227 #endif
228 static _GLSL_STRUCT_CONSTEXPR17 size_t array_static_size_v = array_traits<T>::static_size;
229
230 /**
231 * @brief get pointer to data of array
232 * @ingroup utils
233 */
235 template<array T>
236 #else
237 template<class T, std::enable_if_t<is_array_v<T>, bool> = true>
238 #endif
239 [[nodiscard]] static _GLSL_STRUCT_CONSTEXPR17 const array_value_type_t<T>* get_array_data(const T& value) {
240 return array_traits<T>::data(value);
241 }
242
243 /**
244 * @brief get size of array
245 * @ingroup utils
246 */
248 template<array T>
249 #else
250 template<class T, std::enable_if_t<is_array_v<T>, bool> = true>
251 #endif
252 [[nodiscard]] static _GLSL_STRUCT_CONSTEXPR17 size_t get_array_size(const T& value) {
253 return array_traits<T>::size(value);
254 }
255
256 #pragma endregion
257
258 #pragma region IS_ARRAY_OF
259
260 /**
261 * @brief helper struct for is_array_of_v check
262 * @tparam Test test template struct with static value bool
263 * @tparam T type to checks
264 * @tparam IsArray check if T is array type
265 * @tparam Args additional Test arguments
266 */
267 template<template<class, class...> class Test, class T, bool IsArray = false, class... Args>
268 struct is_array_of : std::false_type {};
269
270 template<template<class, class...> class Test, class T, class... Args>
271 struct is_array_of<Test, T, true, Args...> : Test<array_value_type_t<T>, Args...> {};
272
273 /**
274 * @brief checks if value type is array of type and if type passes given test template struct
275 * @ingroup utils
276 */
277 template<template<class, class...> class Test, class T, class... Args>
278 static _GLSL_STRUCT_CONSTEXPR17 bool is_array_of_v = is_array_of<Test, T, is_array_v<T>, Args...>::value;
279
280 /**
281 * @brief helper struct for is_static_size_array_of_v check
282 * @ingroup utils
283 * @tparam Test test template struct with static value bool
284 * @tparam T type to checks
285 * @tparam IsArray check if T is array type
286 * @tparam Args additional Test arguments
287 */
288 template<template<class, class...> class Test, class T, bool IsArray = false, class... Args>
289 struct is_static_size_array_of_impl : std::false_type {};
290
291 template<template<class, class...> class Test, class T, class... Args>
292 struct is_static_size_array_of_impl<Test, T, true, Args...> : Test<array_value_type_t<T>, Args...> {};
293
294 /**
295 * @brief checks if value type is array of type and if type passes given test template struct
296 * @tparam Test test template struct with static value bool
297 * @tparam T type to checks
298 * @tparam Args additional Test arguments
299 * @ingroup utils
300 */
301 template<template<class, class...> class Test, class T, class... Args>
302 struct is_static_size_array_of : is_static_size_array_of_impl<Test, T, is_array_v<T>, Args...> {};
303
304 /**
305 * @brief checks if value type is array of type
306 * @ingroup utils
307 */
308 template<template<class, class...> class Test, class T, class... Args>
309 static _GLSL_STRUCT_CONSTEXPR17 bool is_static_size_array_of_v = is_static_size_array_of<Test, T, Args...>::value;
310 #pragma endregion
311} // namespace glslstruct::utils
312
313 #endif
314#endif
#define _GLSL_STRUCT_TYPENAME17
Definition config.hpp:216
#define _GLSL_STRUCT_HAS_CXX20
check if compiler has c++ version greater or equal to c++20 and if user enabled c++20 features using ...
Definition config.hpp:142
#define _GLSL_STRUCT_HAS_CXX17
check if compiler has c++ version greater or equal to c++17
Definition config.hpp:130
#define _GLSL_STRUCT_CONSTEXPR17
constexpr for c++17 and higher
Definition config.hpp:196