GLSL Struct 1.4.0
glslstruct
Loading...
Searching...
No Matches
scalar_traits_concept.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_SCALAR_TRAITS_CONCEPT_HPP_
12 #define _GLSL_STRUCT_SCALAR_TRAITS_CONCEPT_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/utils/array_traits.hpp>
21 #include <glslstruct/utils/ValueType.hpp>
22
23namespace glslstruct {
24 class scalar_data;
25
26 /**
27 * @brief Contains all static functions needed for layout and struct classes to interpret given scalar type as glsl scalar value.
28 * @ingroup glslstruct
29 * @tparam T scalar type for which user want to define own glsl converter
30 * @details static functions that need to be declared:
31 * @code{.cpp}
32 * ValueType get_value_type(); // -> returns scalar type
33 * scalar_data get_data(const T&); // -> returns bytes data in scalar_data container
34 * // (preferable is to use already declared type conversions
35 * // bool, int, unsigned int, float or double)
36 * T get_value(const scalar_data&); // -> returns value read from scalar_data container
37 * @endcode
38 */
39 template<class T>
40 struct scalar_traits {};
41
42 namespace utils {
43 #pragma region CHECKS
44 #pragma region IS_SCALAR
45 /**
46 * @var is_glsl_scalar_v
47 * @brief Bool value which is true if type can be converted to glsl scalar type
48 * @ingroup utils
49 * @tparam T type for which converter to glsl scalar type should be defined
50 */
51
52 /**
53 * @struct is_glsl_scalar
54 * @brief struct with bool_constant which is true if type can be converted to glsl scalar type
55 * @ingroup utils
56 * @tparam T type for which converter to glsl scalar type should be defined
57 */
58
60 /**
61 * @brief Concept defining which type can be converted to glsl scalar type
62 * @ingroup utils
63 * @tparam T type for which converter to glsl scalar type should be defined
64 */
65 template<class T>
66 concept glsl_scalar = requires {
67 { scalar_traits<T>::get_value_type() } -> std::same_as<ValueType>;
68 { scalar_traits<T>::get_data(std::declval<const T&>()) } -> std::same_as<scalar_data>;
69 { scalar_traits<T>::get_value(std::declval<const scalar_data&>()) } -> std::same_as<T>;
70 };
71
72 template<class T>
73 static _GLSL_STRUCT_CONSTEXPR17 bool is_glsl_scalar_v = glsl_scalar<T>;
74
75 template<class T>
76 struct is_glsl_scalar : std::bool_constant<is_glsl_scalar_v<T> > {};
77 #else
78 template<class T, class = void>
79 struct is_glsl_scalar : std::false_type {};
80
81 template<class T>
82 struct is_glsl_scalar<T,
83 std::void_t<std::enable_if_t<std::is_same_v<ValueType, decltype(scalar_traits<T>::get_value_type())> >,
84 std::enable_if_t<std::is_same_v<scalar_data, decltype(scalar_traits<T>::get_data(std::declval<const T&>()))> >,
85 std::enable_if_t<std::is_same_v<T, decltype(scalar_traits<T>::get_value(std::declval<const scalar_data&>()))> > > >
86 : std::true_type {};
87
88 template<class T>
89 static _GLSL_STRUCT_CONSTEXPR17 bool is_glsl_scalar_v = is_glsl_scalar<T>::value;
90 #endif
91 #pragma endregion
92
93 #pragma region IS_SCALARS_ARRAY
94 /**
95 * @brief Bool value which is true if type V is array of types that passes is_glsl_scalar test
96 * @ingroup utils
97 * @tparam V type that is checked
98 */
99 template<class V>
100 static _GLSL_STRUCT_CONSTEXPR17 bool is_glsl_scalars_array_v = is_array_of_v<is_glsl_scalar, V>;
101
102 /**
103 * @brief Bool value which is true if type V is static size array of types that passes is_glsl_scalar test
104 * @ingroup utils
105 * @tparam V type that is checked
106 */
107 template<class V>
108 static _GLSL_STRUCT_CONSTEXPR17 bool is_glsl_scalars_static_size_array_v = is_static_size_array_of_v<is_glsl_scalar, V>;
109
111 /**
112 * @brief Concept which is true if type V is array of types that passes is_glsl_scalar test
113 * @ingroup utils
114 * @tparam V type that is checked
115 */
116 template<class V> concept glsl_scalars_array = is_glsl_scalars_array_v<V>;
117
118 /**
119 * @brief Concept which is true if type V is static size array of types that passes is_glsl_scalar test
120 * @ingroup utils
121 * @tparam V type that is checked
122 */
123 template<class V> concept glsl_scalars_static_size_array = is_glsl_scalars_static_size_array_v<V>;
124 #endif
125 #pragma endregion
126 #pragma endregion
127 } // namespace utils
128} // namespace glslstruct
129
130 #endif
131#endif
#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