Add C++17 structureed binding support

Add C++17 Structured Bindings support for vec,mat,quat types
This commit is contained in:
ZXShady 2024-11-10 10:12:14 +00:00
parent 33b4a621a6
commit 24c1824286
4 changed files with 514 additions and 0 deletions

View File

@ -0,0 +1,92 @@
/// @ref gtx_structured_bindings
/// @file glm/gtx/structured_bindings.hpp
///
/// @defgroup gtx_structured_bindings GLM_GTX_structured_bindings
/// @ingroup gtx
///
/// Include <glm/gtx/structured_bindings.hpp> to use the features of this extension.
#pragma once
// Dependency:
#include "../glm.hpp"
#include "../gtx/quaternion.hpp"
#ifdef __cpp_structured_bindings
#if __cpp_structured_bindings >= 201606L
#include <utility>
#include <cstddef>
namespace std {
template<glm::length_t L,typename T,glm::qualifier Q>
struct tuple_size<glm::vec<L, T, Q>> {
static constexpr size_t value = L;
};
template<glm::length_t C,glm::length_t R, typename T, glm::qualifier Q>
struct tuple_size<glm::mat<C,R, T, Q>> {
static constexpr size_t value = C;
};
template<typename T, glm::qualifier Q>
struct tuple_size<glm::qua<T, Q>> {
static constexpr size_t value = 4;
};
template<std::size_t I,glm::length_t L,typename T,glm::qualifier Q>
struct tuple_element<I, glm::vec<L,T,Q>>
{
GLM_STATIC_ASSERT(I < L,"Index out of bounds");
typedef T type;
};
template<std::size_t I, glm::length_t C, glm::length_t R, typename T, glm::qualifier Q>
struct tuple_element<I, glm::mat<C,R, T, Q>>
{
GLM_STATIC_ASSERT(I < C, "Index out of bounds");
typedef glm::vec<R,T,Q> type;
};
template<std::size_t I, typename T, glm::qualifier Q>
struct tuple_element<I, glm::qua<T, Q>>
{
GLM_STATIC_ASSERT(I < 4, "Index out of bounds");
typedef T type;
};
}
#endif
#endif
#ifndef GLM_ENABLE_EXPERIMENTAL
# error "GLM: GLM_GTX_iteration is an experimental extension and may change in the future. Use #define GLM_ENABLE_EXPERIMENTAL before including it, if you really want to use it."
#elif GLM_MESSAGES == GLM_ENABLE && !defined(GLM_EXT_INCLUDED)
# pragma message("GLM: GLM_GTX_io extension included")
#endif
namespace glm
{
/// @addtogroup gtx_structured_bindings
/// @{
template<length_t I, length_t L, typename T, qualifier Q>
GLM_FUNC_DECL GLM_CONSTEXPR T& get(vec<L, T, Q>& v);
template<length_t I, length_t L, typename T, qualifier Q>
GLM_FUNC_DECL GLM_CONSTEXPR T const& get(vec<L, T, Q> const& v);
template<length_t I, length_t C, length_t R, typename T, qualifier Q>
GLM_FUNC_DECL GLM_CONSTEXPR vec<R, T, Q>& get(mat<C, R, T, Q>& m);
template<length_t I, length_t C, length_t R, typename T, qualifier Q>
GLM_FUNC_DECL GLM_CONSTEXPR vec<R, T, Q> const& get(mat<C, R, T, Q> const& m);
template<length_t I, typename T, qualifier Q>
GLM_FUNC_DECL GLM_CONSTEXPR T& get(qua<T, Q>& q);
template<length_t I, typename T, qualifier Q>
GLM_FUNC_DECL GLM_CONSTEXPR T const& get(qua<T, Q> const& q);
#if GLM_HAS_RVALUE_REFERENCES
template<length_t I, length_t L,typename T, qualifier Q>
GLM_FUNC_DECL GLM_CONSTEXPR T get(vec<L,T, Q> const&& v);
template<length_t I,length_t C,length_t R, typename T, qualifier Q>
GLM_FUNC_DECL GLM_CONSTEXPR vec<R,T,Q> get(mat<C,R,T, Q> const&& m);
template<length_t I, typename T, qualifier Q>
GLM_FUNC_DECL GLM_CONSTEXPR T get(qua<T, Q> const&& q);
#endif
/// @}
}//namespace glm
#include "structured_bindings.inl"

View File

@ -0,0 +1,55 @@
namespace glm
{
template<length_t I, length_t L, typename T, qualifier Q>
GLM_CONSTEXPR T& get(vec<L, T, Q>& v) {
GLM_STATIC_ASSERT(I < L, "Index out of bounds");
return v[I];
}
template<length_t I, length_t L, typename T, qualifier Q>
GLM_CONSTEXPR T const& get(vec<L, T, Q> const& v) {
GLM_STATIC_ASSERT(I < L, "Index out of bounds");
return v[I];
}
template<length_t I, length_t C, length_t R, typename T, qualifier Q>
GLM_CONSTEXPR vec<R, T, Q>& get(mat<C, R, T, Q>& m) {
GLM_STATIC_ASSERT(I < C, "Index out of bounds");
return m[I];
}
template<length_t I, length_t C, length_t R, typename T, qualifier Q>
GLM_CONSTEXPR vec<R, T, Q> const& get(mat<C, R, T, Q> const& m) {
GLM_STATIC_ASSERT(I < C, "Index out of bounds");
return m[I];
}
template<length_t I, typename T, qualifier Q>
GLM_CONSTEXPR T& get(qua<T, Q>& q) {
GLM_STATIC_ASSERT(I < 4, "Index out of bounds");
return q[I];
}
template<length_t I, typename T, qualifier Q>
GLM_CONSTEXPR T const& get(qua<T, Q> const& q) {
GLM_STATIC_ASSERT(I < 4, "Index out of bounds");
return q[I];
}
#if GLM_HAS_RVALUE_REFERENCES
template<length_t I, length_t L, typename T, qualifier Q>
GLM_CONSTEXPR T get(vec<L, T, Q> const&& v)
{
GLM_STATIC_ASSERT(I < L, "Index out of bounds");
return v[I];
}
template<length_t I, length_t C, length_t R, typename T, qualifier Q>
GLM_CONSTEXPR vec<R, T, Q> get(mat<C, R, T, Q> const&& m) {
GLM_STATIC_ASSERT(I < C, "Index out of bounds");
return m[I];
}
template<length_t I, typename T, qualifier Q>
GLM_CONSTEXPR T get(qua<T, Q> const&& q) {
GLM_STATIC_ASSERT(I < 4, "Index out of bounds");
return q[I];
}
#endif
}//namespace glm

View File

@ -50,6 +50,7 @@ glmCreateTestGTC(gtx_scalar_multiplication)
glmCreateTestGTC(gtx_scalar_relational)
glmCreateTestGTC(gtx_spline)
glmCreateTestGTC(gtx_string_cast)
glmCreateTestGTC(gtx_structured_bindings)
glmCreateTestGTC(gtx_texture)
glmCreateTestGTC(gtx_type_aligned)
glmCreateTestGTC(gtx_type_trait)

View File

@ -0,0 +1,366 @@
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/structured_bindings.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/vec1.hpp>
static int test_vec1() {
glm::vec1 v(0);
float& x = glm::get<0>(v);
return (&x != &v.x);
}
static int test_vec2() {
glm::vec2 v(0);
float& x = glm::get<0>(v);
float& y = glm::get<1>(v);
return (&x != &v.x) + (&y != &v.y);
}
static int test_vec3() {
glm::vec3 v(0);
float& x = glm::get<0>(v);
float& y = glm::get<1>(v);
float& z = glm::get<2>(v);
return (&x != &v.x) + (&y != &v.y) + (&z != &v.z);
}
static int test_vec4() {
glm::vec4 v(0);
float& x = glm::get<0>(v);
float& y = glm::get<1>(v);
float& z = glm::get<2>(v);
float& w = glm::get<3>(v);
return (&x != &v.x) + (&y != &v.y) + (&z != &v.z) + (&w != &v.w);
}
static int test_const_vec1() {
glm::vec1 const v(0);
float const& x = glm::get<0>(v);
return (&x != &v.x);
}
static int test_const_vec2() {
glm::vec2 const v(0);
float const& x = glm::get<0>(v);
float const& y = glm::get<1>(v);
return (&x != &v.x) + (&y != &v.y);
}
static int test_const_vec3() {
glm::vec3 const v(0);
float const& x = glm::get<0>(v);
float const& y = glm::get<1>(v);
float const& z = glm::get<2>(v);
return (&x != &v.x) + (&y != &v.y) + (&z != &v.z);
}
static int test_const_vec4() {
glm::vec4 const v(0);
float const& x = glm::get<0>(v);
float const& y = glm::get<1>(v);
float const& z = glm::get<2>(v);
float const& w = glm::get<3>(v);
return (&x != &v.x) + (&y != &v.y) + (&z != &v.z) + (&w != &v.w);
}
static int test_quat() {
glm::quat q(0.0f, 0.0f, 0.0f, 0.0f);
#ifdef GLM_FORCE_QUAT_DATA_WXYZ
float& w = glm::get<0>(q);
float& x = glm::get<1>(q);
float& y = glm::get<2>(q);
float& z = glm::get<3>(q);
#else
float& x = glm::get<0>(q);
float& y = glm::get<1>(q);
float& z = glm::get<2>(q);
float& w = glm::get<3>(q);
#endif
return (&x != &q.x) + (&y != &q.y) + (&z != &q.z) + (&w != &q.w);
}
static int test_const_quat() {
glm::quat const q(0.0f, 0.0f, 0.0f, 0.0f);
#ifdef GLM_FORCE_QUAT_DATA_WXYZ
float const& w = glm::get<0>(q);
float const& x = glm::get<1>(q);
float const& y = glm::get<2>(q);
float const& z = glm::get<3>(q);
#else
float const& x = glm::get<0>(q);
float const& y = glm::get<1>(q);
float const& z = glm::get<2>(q);
float const& w = glm::get<3>(q);
#endif
return (&x != &q.x) + (&y != &q.y) + (&z != &q.z) + (&w != &q.w);
}
template<glm::length_t R>
static int test_mat2xR() {
typedef glm::mat<2, R, float> Mat;
Mat m(0);
typename Mat::col_type& c1 = glm::get<0>(m);
typename Mat::col_type& c2 = glm::get<1>(m);
return (&c1 != &m[0]) + (&c2 != &m[1]);
}
template<glm::length_t R>
static int test_const_mat2xR() {
typedef glm::mat<2,R,float> Mat;
Mat const m(0);
typename Mat::col_type const& c1 = glm::get<0>(m);
typename Mat::col_type const& c2 = glm::get<1>(m);
return (&c1 != &m[0]) + (&c2 != &m[1]);
}
template<glm::length_t R>
static int test_mat3xR() {
typedef glm::mat<3, R, float> Mat;
Mat m(0);
typename Mat::col_type& c1 = glm::get<0>(m);
typename Mat::col_type& c2 = glm::get<1>(m);
typename Mat::col_type& c3 = glm::get<2>(m);
return (&c1 != &m[0]) + (&c2 != &m[1]) + (&c3 != &m[2]);
}
template<glm::length_t R>
static int test_const_mat3xR() {
typedef glm::mat< 3, R, float> Mat;
Mat const m(0);
typename Mat::col_type const& c1 = glm::get<0>(m);
typename Mat::col_type const& c2 = glm::get<1>(m);
typename Mat::col_type const& c3 = glm::get<2>(m);
return (&c1 != &m[0]) + (&c2 != &m[1]) + (&c3 != &m[2]);
}
template<glm::length_t R>
static int test_mat4xR() {
typedef glm::mat<4,R,float> Mat;
Mat m(0);
typename Mat::col_type& c1 = glm::get<0>(m);
typename Mat::col_type& c2 = glm::get<1>(m);
typename Mat::col_type& c3 = glm::get<2>(m);
typename Mat::col_type& c4 = glm::get<3>(m);
return (&c1 != &m[0]) + (&c2 != &m[1]) + (&c3 != &m[2]) + (&c4 != &m[3]);
}
template<glm::length_t R>
static int test_const_mat4xR() {
typedef glm::mat<4,R,float> Mat;
Mat const m(0);
typename Mat::col_type const& c1 = glm::get<0>(m);
typename Mat::col_type const& c2 = glm::get<1>(m);
typename Mat::col_type const& c3 = glm::get<2>(m);
typename Mat::col_type const& c4 = glm::get<3>(m);
return (&c1 != &m[0]) + (&c2 != &m[1]) + (&c3 != &m[2]) + (&c4 != &m[3]);
}
#if defined(__cpp_structured_bindings)
#if __cpp_structured_bindings >= 201606L
static int test_structured_vec1() {
glm::vec1 v(0);
auto& [x] = v;
return (&x != &v.x);
}
static int test_structured_vec2() {
glm::vec2 v(0);
auto& [x, y] = v;
return (&x != &v.x) + (&y != &v.y);
}
static int test_structured_vec3() {
glm::vec3 v(0);
auto& [x, y, z] = v;
return (&x != &v.x) + (&y != &v.y) + (&z != &v.z);
}
static int test_structured_vec4() {
glm::vec4 v(0);
auto& [x, y, z, w] = v;
return (&x != &v.x) + (&y != &v.y) + (&z != &v.z) + (&w != &v.w);
}
static int test_const_structured_vec1() {
glm::vec1 const v(0);
auto const& [x] = v;
return (&x != &v.x);
}
static int test_const_structured_vec2() {
glm::vec2 const v(0);
auto const& [x, y] = v;
return (&x != &v.x) + (&y != &v.y);
}
static int test_const_structured_vec3() {
glm::vec3 const v(0);
auto const& [x, y, z] = v;
return (&x != &v.x) + (&y != &v.y) + (&z != &v.z);
}
static int test_const_structured_vec4() {
glm::vec4 const v(0);
auto const& [x, y, z, w] = v;
return (&x != &v.x) + (&y != &v.y) + (&z != &v.z) + (&w != &v.w);
}
template<glm::length_t R>
static int test_structured_mat2xR() {
glm::mat<2,R,float,glm::defaultp> m(0);
auto& [c1, c2] = m;
return (&c1 != &m[0]) + (&c2 != &m[1]);
}
template<glm::length_t R>
static int test_const_structured_mat2xR() {
glm::mat<2, R, float, glm::defaultp> const m(0);
auto const& [c1, c2] = m;
return (&c1 != &m[0]) + (&c2 != &m[1]);
}
template<glm::length_t R>
static int test_structured_mat3xR() {
glm::mat<3, R, float, glm::defaultp> m(0);
auto& [c1, c2,c3] = m;
return (&c1 != &m[0]) + (&c2 != &m[1]) + (&c3 != &m[2]);
}
template<glm::length_t R>
static int test_const_structured_mat3xR() {
glm::mat<3, R, float, glm::defaultp> const m(0);
auto const& [c1, c2, c3] = m;
return (&c1 != &m[0]) + (&c2 != &m[1]) + (&c3 != &m[2]);
}
template<glm::length_t R>
static int test_structured_mat4xR() {
glm::mat<4, R, float, glm::defaultp> m(0);
auto& [c1, c2, c3,c4] = m;
return (&c1 != &m[0]) + (&c2 != &m[1]) + (&c3 != &m[2]) + (&c4 != &m[3]);
}
template<glm::length_t R>
static int test_const_structured_mat4xR() {
glm::mat<4, R, float, glm::defaultp> const m(0);
auto const& [c1, c2, c3, c4] = m;
return (&c1 != &m[0]) + (&c2 != &m[1]) + (&c3 != &m[2]) + (&c4 != &m[3]);
}
static int test_structured_quat() {
glm::quat q(0.0f, 0.0f, 0.0f, 0.0f);
#ifdef GLM_FORCE_QUAT_DATA_WXYZ
auto& [w, x, y, z] = q;
#else
auto& [x, y, z, w] = q;
#endif
return (&x != &q.x) + (&y != &q.y) + (&z != &q.z) + (&w != &q.w);
}
static int test_const_structured_quat() {
glm::quat const q(0.0f, 0.0f, 0.0f, 0.0f);
#ifdef GLM_FORCE_QUAT_DATA_WXYZ
auto const& [w, x, y, z] = q;
#else
auto const& [x, y, z, w] = q;
#endif
return (&x != &q.x) + (&y != &q.y) + (&z != &q.z) + (&w != &q.w);
}
#endif
#endif
int main()
{
int Error = 0;
Error += test_vec1();
Error += test_vec2();
Error += test_vec3();
Error += test_vec4();
Error += test_const_vec1();
Error += test_const_vec2();
Error += test_const_vec3();
Error += test_const_vec4();
Error += test_quat();
Error += test_const_quat();
Error += test_mat2xR<2>();
Error += test_const_mat2xR<2>();
Error += test_mat2xR<3>();
Error += test_const_mat2xR<3>();
Error += test_mat2xR<4>();
Error += test_const_mat2xR<4>();
Error += test_mat3xR<2>();
Error += test_const_mat3xR<2>();
Error += test_mat3xR<3>();
Error += test_const_mat3xR<3>();
Error += test_mat3xR<4>();
Error += test_const_mat3xR<4>();
Error += test_mat4xR<2>();
Error += test_const_mat4xR<2>();
Error += test_mat4xR<3>();
Error += test_const_mat4xR<3>();
Error += test_mat4xR<4>();
Error += test_const_mat4xR<4>();
#ifdef __cpp_structured_bindings
#if __cpp_structured_bindings >= 201606L
Error += test_structured_vec1();
Error += test_structured_vec2();
Error += test_structured_vec3();
Error += test_structured_vec4();
Error += test_const_structured_vec1();
Error += test_const_structured_vec2();
Error += test_const_structured_vec3();
Error += test_const_structured_vec4();
Error += test_structured_quat();
Error += test_const_structured_quat();
Error += test_structured_mat2xR<2>();
Error += test_const_structured_mat2xR<2>();
Error += test_structured_mat2xR<3>();
Error += test_const_structured_mat2xR<3>();
Error += test_structured_mat2xR<4>();
Error += test_const_structured_mat2xR<4>();
Error += test_structured_mat3xR<2>();
Error += test_const_structured_mat3xR<2>();
Error += test_structured_mat3xR<3>();
Error += test_const_structured_mat3xR<3>();
Error += test_structured_mat3xR<4>();
Error += test_const_structured_mat3xR<4>();
Error += test_structured_mat4xR<2>();
Error += test_const_structured_mat4xR<2>();
Error += test_structured_mat4xR<3>();
Error += test_const_structured_mat4xR<3>();
Error += test_structured_mat4xR<4>();
Error += test_const_structured_mat4xR<4>();
#endif
#endif
return Error;
}