mat.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #ifndef BOOST_QVM_MAT_HPP_INCLUDED
  2. #define BOOST_QVM_MAT_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/detail/mat_assign.hpp>
  7. #include <boost/qvm/assert.hpp>
  8. #include <boost/qvm/static_assert.hpp>
  9. namespace boost { namespace qvm {
  10. template <class T,int Rows,int Cols>
  11. struct
  12. mat
  13. {
  14. T a[Rows][Cols];
  15. template <class R
  16. #if __cplusplus >= 201103L
  17. , class = typename enable_if<is_mat<R> >::type
  18. #endif
  19. >
  20. operator R() const
  21. {
  22. R r;
  23. assign(r,*this);
  24. return r;
  25. }
  26. };
  27. template <class M>
  28. struct mat_traits;
  29. template <class T,int Rows,int Cols>
  30. struct
  31. mat_traits< mat<T,Rows,Cols> >
  32. {
  33. typedef mat<T,Rows,Cols> this_matrix;
  34. typedef T scalar_type;
  35. static int const rows=Rows;
  36. static int const cols=Cols;
  37. template <int Row,int Col>
  38. static
  39. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  40. scalar_type
  41. read_element( this_matrix const & x )
  42. {
  43. BOOST_QVM_STATIC_ASSERT(Row>=0);
  44. BOOST_QVM_STATIC_ASSERT(Row<rows);
  45. BOOST_QVM_STATIC_ASSERT(Col>=0);
  46. BOOST_QVM_STATIC_ASSERT(Col<cols);
  47. return x.a[Row][Col];
  48. }
  49. template <int Row,int Col>
  50. static
  51. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  52. scalar_type &
  53. write_element( this_matrix & x )
  54. {
  55. BOOST_QVM_STATIC_ASSERT(Row>=0);
  56. BOOST_QVM_STATIC_ASSERT(Row<rows);
  57. BOOST_QVM_STATIC_ASSERT(Col>=0);
  58. BOOST_QVM_STATIC_ASSERT(Col<cols);
  59. return x.a[Row][Col];
  60. }
  61. static
  62. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  63. scalar_type
  64. read_element_idx( int row, int col, this_matrix const & x )
  65. {
  66. BOOST_QVM_ASSERT(row>=0);
  67. BOOST_QVM_ASSERT(row<Rows);
  68. BOOST_QVM_ASSERT(col>=0);
  69. BOOST_QVM_ASSERT(col<Cols);
  70. return x.a[row][col];
  71. }
  72. static
  73. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  74. scalar_type &
  75. write_element_idx( int row, int col, this_matrix & x )
  76. {
  77. BOOST_QVM_ASSERT(row>=0);
  78. BOOST_QVM_ASSERT(row<Rows);
  79. BOOST_QVM_ASSERT(col>=0);
  80. BOOST_QVM_ASSERT(col<Cols);
  81. return x.a[row][col];
  82. }
  83. };
  84. } }
  85. #endif