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