Skip to content

Commit cd4dfd0

Browse files
AbitTheGrayGroovounet
authored andcommitted
Fix const, add more tests
1 parent 8ca503e commit cd4dfd0

File tree

7 files changed

+459
-186
lines changed

7 files changed

+459
-186
lines changed

glm/gtx/range.hpp

+14-43
Original file line numberDiff line numberDiff line change
@@ -22,54 +22,29 @@
2222
#endif
2323

2424
#include "../gtc/type_ptr.hpp"
25-
#include "../gtc/vec1.hpp"
25+
#include "type_trait.hpp"
2626

2727
namespace glm
2828
{
2929
/// @addtogroup gtx_range
3030
/// @{
3131

32-
# if GLM_COMPILER & GLM_COMPILER_VC
33-
# pragma warning(push)
34-
# pragma warning(disable : 4100) // unreferenced formal parameter
35-
# endif
36-
37-
template<typename T, qualifier Q>
38-
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR length_t components(vec<1, T, Q> const& v)
39-
{
40-
return v.length();
41-
}
42-
43-
template<typename T, qualifier Q>
44-
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR length_t components(vec<2, T, Q> const& v)
45-
{
46-
return v.length();
47-
}
48-
49-
template<typename T, qualifier Q>
50-
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR length_t components(vec<3, T, Q> const& v)
51-
{
52-
return v.length();
53-
}
54-
55-
template<typename T, qualifier Q>
56-
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR length_t components(vec<4, T, Q> const& v)
57-
{
58-
return v.length();
59-
}
60-
61-
template<typename T, qualifier Q>
62-
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR length_t components(qua<T, Q> const& v)
63-
{
64-
return v.length();
65-
}
32+
#if GLM_COMPILER & GLM_COMPILER_VC
33+
# pragma warning(push)
34+
# pragma warning(disable : 4100) // unreferenced formal parameter
35+
#endif
6636

37+
/// @warning This is not same as `type<genType>::components`, calling this returns total elements (for mat4 returns 16 instead of 4).
6738
template<typename genType>
68-
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR length_t components(genType const& m)
39+
/*GLM_DEPRECATED*/ GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR length_t components(genType const& v)
6940
{
70-
return m.length() * m[0].length();
41+
return type<std::remove_cv<genType>::type>::elements;
7142
}
7243

44+
#if GLM_COMPILER & GLM_COMPILER_VC
45+
# pragma warning(pop)
46+
#endif
47+
7348
template<typename genType>
7449
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR typename genType::value_type const * begin(genType const& v)
7550
{
@@ -79,7 +54,7 @@ namespace glm
7954
template<typename genType>
8055
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR typename genType::value_type const * end(genType const& v)
8156
{
82-
return begin(v) + components(v);
57+
return begin(v) + type<std::remove_cv<genType>::type>::elements;
8358
}
8459

8560
template<typename genType>
@@ -91,12 +66,8 @@ namespace glm
9166
template<typename genType>
9267
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR typename genType::value_type * end(genType& v)
9368
{
94-
return begin(v) + components(v);
69+
return begin(v) + type<std::remove_cv<genType>::type>::elements;
9570
}
9671

97-
# if GLM_COMPILER & GLM_COMPILER_VC
98-
# pragma warning(pop)
99-
# endif
100-
10172
/// @}
10273
}//namespace glm

glm/gtx/span.hpp

+49-108
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,29 @@
2020
# pragma message("GLM: GLM_GTX_span extension included")
2121
#endif
2222

23-
#if !(GLM_LANG & GLM_LANG_CXX11)
24-
// This requirement is due to `std::enable_if`
23+
// `std::enable_if` support (and few more)
24+
// Required for all functions below
25+
#if !(GLM_LANG & GLM_LANG_CXX11_FLAG)
2526
# error "GLM_GTX_span requiers at least C++11, using C++20 or C++23 is recommended for full functionality"
2627
#endif
2728

29+
// GLM_MESSAGES info
2830
#if GLM_MESSAGES == GLM_ENABLE && !defined(GLM_EXT_INCLUDED)
29-
# if (GLM_LANG & GLM_LANG_CXX20) && defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
31+
# if (GLM_LANG & GLM_LANG_CXX20_FLAG) && defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
3032
# pragma message("GLM: GLM_GTX_span extension will include std::span")
3133
# endif
32-
# if (GLM_LANG & GLM_LANG_CXX23) && defined(__cpp_lib_mdspan) && __cpp_lib_mdspan >= 202207L
34+
# if (GLM_LANG & GLM_LANG_CXX23_FLAG) && defined(__cpp_lib_mdspan) && __cpp_lib_mdspan >= 202207L
3335
# pragma message("GLM: GLM_GTX_span extension will include std::mdspan")
3436
# endif
3537
#endif
3638

37-
#include "../gtc/type_precision.hpp"
3839
#include "../gtc/type_ptr.hpp"
40+
#include "type_trait.hpp"
3941

4042
#include <valarray>
41-
#include <type_traits>
4243

43-
#if GLM_LANG & GLM_LANG_CXX20
44+
// Version-specific includes
45+
#if GLM_LANG & GLM_LANG_CXX20_FLAG
4446
// Feature testing
4547
# include <version>
4648

@@ -60,141 +62,80 @@ namespace glm
6062
/// @addtogroup gtx_span
6163
/// @{
6264

65+
# if (GLM_LANG & GLM_LANG_CXX20_FLAG)
6366
template<typename T>
64-
struct is_vec : std::false_type {};
65-
template<length_t L, typename T, qualifier Q>
66-
struct is_vec<vec<L, T, Q>> : std::true_type {};
67-
68-
template<typename T>
69-
struct is_quat : std::false_type {};
70-
template<typename T, qualifier Q>
71-
struct is_quat<qua<T, Q>> : std::true_type {};
72-
73-
template<typename T>
74-
struct is_mat : std::false_type {};
75-
template<length_t L1, length_t L2, typename T, qualifier Q>
76-
struct is_mat<mat<L1, L2, T, Q>> : std::true_type {};
77-
78-
#if (GLM_LANG & GLM_LANG_CXX17)
79-
template<typename T>
80-
inline constexpr bool is_vec_v = is_vec<T>::value;
81-
template<typename T>
82-
inline constexpr bool is_quat_v = is_quat<T>::value;
83-
template<typename T>
84-
inline constexpr bool is_mat_v = is_mat<T>::value;
85-
#endif
86-
87-
#if (GLM_LANG & GLM_LANG_CXX20)
88-
template<typename T>
89-
requires is_vec<T>::value || is_quat<T>::value
90-
#else
91-
template<typename T, typename = typename std::enable_if<is_vec<T>::value || is_quat<T>::value>::type>
92-
#endif
93-
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR length_t components()
94-
{
95-
return T::length();
96-
}
97-
#if (GLM_LANG & GLM_LANG_CXX20)
98-
template<typename T>
99-
requires is_mat<T>::value
100-
#else
101-
template<typename T, typename = typename std::enable_if<is_mat<T>::value>::type>
102-
#endif
103-
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR length_t components()
104-
{
105-
return T::length() * T::col_type::length();
106-
}
107-
# if GLM_COMPILER & GLM_COMPILER_VC
108-
# pragma warning(push)
109-
# pragma warning(disable : 4100) // unreferenced formal parameter
110-
# endif
111-
112-
/// Utility function if you don't have the type and dont use `decltype` (it is from C++11 so this function won't exist for earlier anyway)
113-
#if (GLM_LANG & GLM_LANG_CXX20)
114-
template<typename T>
115-
requires is_vec<T>::value || is_quat<T>::value
116-
#else
117-
template<typename T, typename = typename std::enable_if<is_vec<T>::value || is_quat<T>::value>::type>
118-
#endif
119-
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR length_t components(T const&)
120-
{
121-
return components<T>();
122-
}
123-
# if GLM_COMPILER & GLM_COMPILER_VC
124-
# pragma warning(pop)
67+
requires (type<std::remove_cvref_t<T>>::elements > 0)
68+
# else
69+
template<typename T, typename = typename std::enable_if<
70+
(
71+
type<
72+
typename std::remove_reference<
73+
typename std::remove_cv<T>::type
74+
>::type
75+
>::elements > 0
76+
)>::type>
12577
# endif
126-
127-
#if (GLM_LANG & GLM_LANG_CXX20)
128-
template<typename T>
129-
requires is_vec<T>::value || is_quat<T>::value || is_mat<T>::value
130-
#else
131-
template<typename T, typename = typename std::enable_if<is_vec<T>::value || is_quat<T>::value || is_mat<T>::value>::type>
132-
#endif
13378
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR std::valarray<typename T::value_type> valarray(T const& v)
13479
{
135-
return std::valarray<typename T::value_type>(value_ptr(v), components<T>());
80+
return std::valarray<typename T::value_type>(value_ptr(v), type<T>::elements);
13681
}
13782

138-
#if (GLM_LANG & GLM_LANG_CXX20) && defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
83+
#if (GLM_LANG & GLM_LANG_CXX20_FLAG) && defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
13984

140-
#if (GLM_LANG & GLM_LANG_CXX20)
14185
template<typename T>
142-
requires is_vec<T>::value || is_quat<T>::value || is_mat<T>::value
143-
#else
144-
template<typename T, typename = typename std::enable_if<is_vec<T>::value || is_quat<T>::value || is_mat<T>::value>::type>
145-
#endif
86+
requires (type<std::remove_cvref_t<T>>::elements > 0)
14687
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR std::span<typename T::value_type> span(T & v)
14788
{
148-
return std::span<typename T::value_type>(value_ptr(v), components<T>());
89+
using TN = std::remove_cvref_t<T>;
90+
return std::span<typename T::value_type>(value_ptr(v), type<TN>::elements);
14991
}
15092

151-
#if (GLM_LANG & GLM_LANG_CXX20)
15293
template<typename T>
153-
requires is_vec<T>::value || is_quat<T>::value || is_mat<T>::value
154-
#else
155-
template<typename T, typename = typename std::enable_if<is_vec<T>::value || is_quat<T>::value || is_mat<T>::value>::type>
156-
#endif
94+
requires (type<std::remove_cvref_t<T>>::elements > 0)
15795
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR std::span<const typename T::value_type> span(T const& v)
15896
{
159-
return std::span<const typename T::value_type>(value_ptr(v), components<T>());
97+
using TN = std::remove_cvref_t<T>;
98+
return std::span<const typename T::value_type>(value_ptr(v), type<TN>::elements);
16099
}
161100

162101
#endif
163102

164-
#if (GLM_LANG & GLM_LANG_CXX23) && defined(__cpp_lib_mdspan) && __cpp_lib_mdspan >= 202207L
103+
#if (GLM_LANG & GLM_LANG_CXX23_FLAG) && defined(__cpp_lib_mdspan) && __cpp_lib_mdspan >= 202207L
165104

166-
#if (GLM_LANG & GLM_LANG_CXX20)
167105
template<typename T>
168-
requires is_vec<T>::value || is_quat<T>::value
169-
#else
170-
template<typename T, typename = typename std::enable_if<is_vec<T>::value || is_quat<T>::value>::type>
171-
#endif
106+
requires (type<std::remove_cvref_t<T>>::rows == 1)
172107
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR std::mdspan<typename T::value_type> span(T & v)
173108
{
174-
return std::mdspan<typename T::value_type>(value_ptr(v), components<T>());
109+
using TN = std::remove_cvref_t<T>;
110+
static_assert(type<TN>::cols >= 1);
111+
return std::mdspan<typename T::value_type>(value_ptr(v), type<TN>::cols);
175112
}
176113

177-
#if (GLM_LANG & GLM_LANG_CXX20)
178114
template<typename T>
179-
requires is_vec<T>::value || is_quat<T>::value
180-
#else
181-
template<typename T, typename = typename std::enable_if<is_vec<T>::value || is_quat<T>::value>::type>
182-
#endif
115+
requires (type<std::remove_cvref_t<T>>::rows == 1)
183116
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR std::mdspan<const typename T::value_type> span(T const& v)
184117
{
185-
return std::mdspan<const typename T::value_type>(value_ptr(v), components<T>());
118+
using TN = std::remove_cvref_t<T>;
119+
static_assert(type<TN>::cols >= 1);
120+
return std::mdspan<const typename T::value_type>(value_ptr(v), type<TN>::cols);
186121
}
187122

188-
template<length_t L1, length_t L2, typename T, qualifier Q>
189-
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR auto mdspan(mat<L1, L2, T, Q> & m)
123+
template<typename T>
124+
requires (type<std::remove_cvref_t<T>>::rows > 1)
125+
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR auto mdspan(T & m)
190126
{
191-
return std::mdspan<T>(value_ptr(m), L1, L2);
127+
using TN = std::remove_cvref_t<T>;
128+
static_assert(type<TN>::cols >= 1);
129+
return std::mdspan<typename T::value_type>(value_ptr(m), type<TN>::cols, type<TN>::rows);
192130
}
193131

194-
template<length_t L1, length_t L2, typename T, qualifier Q>
195-
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR auto mdspan(mat<L1, L2, T, Q> const& m)
132+
template<typename T>
133+
requires (type<std::remove_cvref_t<T>>::rows > 1)
134+
GLM_NODISCARD GLM_FUNC_QUALIFIER GLM_CONSTEXPR auto mdspan(T const& m)
196135
{
197-
return std::mdspan<const T>(value_ptr(m), L1, L2);
136+
using TN = std::remove_cvref_t<T>;
137+
static_assert(type<TN>::cols >= 1);
138+
return std::mdspan<const typename T::value_type>(value_ptr(m), type<TN>::cols, type<TN>::rows);
198139
}
199140

200141
#endif

glm/gtx/type_trait.hpp

+18
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,20 @@ namespace glm
3131
template<typename T>
3232
struct type
3333
{
34+
#if GLM_LANG & GLM_LANG_CXX11_FLAG
35+
// with C++20, you can use std::remove_cvref for all of those
36+
// with C++11, you can use std::remove_cv for the first two
37+
static_assert(!std::is_const<T>::value); // use std::remove_const
38+
static_assert(!std::is_volatile<T>::value); // use std::remove_volatile
39+
static_assert(!std::is_reference<T>::value); // use std::remove_reference
40+
#endif
3441
static bool const is_vec = false;
3542
static bool const is_mat = false;
3643
static bool const is_quat = false;
3744
static length_t const components = 0;
3845
static length_t const cols = 0;
3946
static length_t const rows = 0;
47+
static length_t const elements = cols * rows;
4048
};
4149

4250
template<length_t L, typename T, qualifier Q>
@@ -46,6 +54,9 @@ namespace glm
4654
static bool const is_mat = false;
4755
static bool const is_quat = false;
4856
static length_t const components = L;
57+
static length_t const cols = L;
58+
static length_t const rows = 1;
59+
static length_t const elements = cols * rows;
4960
};
5061

5162
template<length_t C, length_t R, typename T, qualifier Q>
@@ -57,6 +68,7 @@ namespace glm
5768
static length_t const components = C;
5869
static length_t const cols = C;
5970
static length_t const rows = R;
71+
static length_t const elements = cols * rows;
6072
};
6173

6274
template<typename T, qualifier Q>
@@ -66,6 +78,9 @@ namespace glm
6678
static bool const is_mat = false;
6779
static bool const is_quat = true;
6880
static length_t const components = 4;
81+
static length_t const cols = components;
82+
static length_t const rows = 1;
83+
static length_t const elements = cols * rows;
6984
};
7085

7186
template<typename T, qualifier Q>
@@ -75,6 +90,9 @@ namespace glm
7590
static bool const is_mat = false;
7691
static bool const is_quat = true;
7792
static length_t const components = 8;
93+
static length_t const cols = components;
94+
static length_t const rows = 1;
95+
static length_t const elements = cols * rows;
7896
};
7997

8098
/// @}

0 commit comments

Comments
 (0)