GLSL Struct 1.4.8
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 _GLSL_STRUCT_EXPORT scalar_traits {};
41
42 #pragma region CHECKS
43 #pragma region IS_SCALAR
44 /**
45 * @var is_glsl_scalar_v
46 * @brief Bool value which is true if type can be converted to glsl scalar type
47 * @ingroup glslstruct
48 * @tparam T type for which converter to glsl scalar type should be defined
49 */
50
51 /**
52 * @struct is_glsl_scalar
53 * @brief struct with bool_constant which is true if type can be converted to glsl scalar type
54 * @ingroup glslstruct
55 * @tparam T type for which converter to glsl scalar type should be defined
56 */
57
59 /**
60 * @brief Concept defining which type can be converted to glsl scalar type
61 * @ingroup glslstruct
62 * @tparam T type for which converter to glsl scalar type should be defined
63 */
64 template<class T>
65 concept glsl_scalar = requires {
66 { scalar_traits<T>::get_value_type() } -> std::convertible_to<ValueType>;
67 { scalar_traits<T>::get_data(std::declval<const T&>()) } -> std::convertible_to<scalar_data>;
68 { scalar_traits<T>::get_value(std::declval<const scalar_data&>()) } -> std::convertible_to<T>;
69 };
70
71 template<class T>
72 static _GLSL_STRUCT_CONSTEXPR17 bool is_glsl_scalar_v = glsl_scalar<T>;
73
74 template<class T>
75 struct is_glsl_scalar : std::bool_constant<is_glsl_scalar_v<T> > {};
76 #else
77 template<class T, class = void>
78 struct is_glsl_scalar : std::false_type {};
79
80 template<class T>
81 struct is_glsl_scalar<T,
82 std::void_t<std::enable_if_t<std::is_convertible_v<decltype(scalar_traits<T>::get_value_type()), ValueType> >,
83 std::enable_if_t<std::is_convertible_v<decltype(scalar_traits<T>::get_data(std::declval<const T&>())), scalar_data> >,
84 std::enable_if_t<std::is_convertible_v<decltype(scalar_traits<T>::get_value(std::declval<const scalar_data&>())), T> > > >
85 : std::true_type {};
86
87 template<class T>
88 static _GLSL_STRUCT_CONSTEXPR17 bool is_glsl_scalar_v = is_glsl_scalar<T>::value;
89 #endif
90 #pragma endregion
91
92 namespace utils {
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 } // namespace utils
127
128 #pragma endregion
129} // namespace glslstruct
130
131 #endif
132#endif
#define _GLSL_STRUCT_EXPORT
This is for exporting symbols in shared library setup.
Definition config.hpp:251
#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