mat_traits_defaults.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef BOOST_QVM_MAT_TRAITS_DEFAULTS_HPP_INCLUDED
  2. #define BOOST_QVM_MAT_TRAITS_DEFAULTS_HPP_INCLUDED
  3. // Copyright 2008-2022 Emil Dotchevski and Reverge Studios, Inc.
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/qvm/config.hpp>
  7. #include <boost/qvm/assert.hpp>
  8. namespace boost { namespace qvm {
  9. template <class>
  10. struct mat_traits;
  11. namespace
  12. qvm_detail
  13. {
  14. template <int I,int N>
  15. struct
  16. matrix_w
  17. {
  18. template <class A>
  19. static
  20. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  21. typename mat_traits<A>::scalar_type &
  22. write_element_idx( int r, int c, A & a )
  23. {
  24. return (I/mat_traits<A>::cols)==r && (I%mat_traits<A>::cols)==c?
  25. mat_traits<A>::template write_element<I/mat_traits<A>::cols,I%mat_traits<A>::cols>(a) :
  26. matrix_w<I+1,N>::write_element_idx(r,c,a);
  27. }
  28. };
  29. template <int N>
  30. struct
  31. matrix_w<N,N>
  32. {
  33. template <class A>
  34. static
  35. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  36. typename mat_traits<A>::scalar_type &
  37. write_element_idx( int, int, A & a )
  38. {
  39. BOOST_QVM_ASSERT(0);
  40. return mat_traits<A>::template write_element<0,0>(a);
  41. }
  42. };
  43. }
  44. template <class MatType,class ScalarType,int Rows,int Cols>
  45. struct
  46. mat_traits_defaults
  47. {
  48. typedef MatType mat_type;
  49. typedef ScalarType scalar_type;
  50. static int const rows=Rows;
  51. static int const cols=Cols;
  52. template <int Row,int Col>
  53. static
  54. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  55. scalar_type
  56. read_element( mat_type const & x )
  57. {
  58. return mat_traits<mat_type>::template write_element<Row,Col>(const_cast<mat_type &>(x));
  59. }
  60. static
  61. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  62. scalar_type
  63. read_element_idx( int r, int c, mat_type const & x )
  64. {
  65. return mat_traits<mat_type>::write_element_idx(r,c,const_cast<mat_type &>(x));
  66. }
  67. protected:
  68. static
  69. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_TRIVIAL
  70. scalar_type &
  71. write_element_idx( int r, int c, mat_type & m )
  72. {
  73. return qvm_detail::matrix_w<0,mat_traits<mat_type>::rows*mat_traits<mat_type>::cols>::write_element_idx(r,c,m);
  74. }
  75. };
  76. } }
  77. #endif