mat.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #ifndef BOOST_QVM_MAT_HPP_INCLUDED
  2. #define BOOST_QVM_MAT_HPP_INCLUDED
  3. /// Copyright (c) 2008-2021 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. operator R() const
  17. {
  18. R r;
  19. assign(r,*this);
  20. return r;
  21. }
  22. };
  23. template <class M>
  24. struct mat_traits;
  25. template <class T,int Rows,int Cols>
  26. struct
  27. mat_traits< mat<T,Rows,Cols> >
  28. {
  29. typedef mat<T,Rows,Cols> this_matrix;
  30. typedef T scalar_type;
  31. static int const rows=Rows;
  32. static int const cols=Cols;
  33. template <int Row,int Col>
  34. static
  35. BOOST_QVM_INLINE_CRITICAL
  36. scalar_type
  37. read_element( this_matrix const & x )
  38. {
  39. BOOST_QVM_STATIC_ASSERT(Row>=0);
  40. BOOST_QVM_STATIC_ASSERT(Row<Rows);
  41. BOOST_QVM_STATIC_ASSERT(Col>=0);
  42. BOOST_QVM_STATIC_ASSERT(Col<Cols);
  43. return x.a[Row][Col];
  44. }
  45. template <int Row,int Col>
  46. static
  47. BOOST_QVM_INLINE_CRITICAL
  48. scalar_type &
  49. write_element( this_matrix & x )
  50. {
  51. BOOST_QVM_STATIC_ASSERT(Row>=0);
  52. BOOST_QVM_STATIC_ASSERT(Row<Rows);
  53. BOOST_QVM_STATIC_ASSERT(Col>=0);
  54. BOOST_QVM_STATIC_ASSERT(Col<Cols);
  55. return x.a[Row][Col];
  56. }
  57. static
  58. BOOST_QVM_INLINE_CRITICAL
  59. scalar_type
  60. read_element_idx( int row, int col, this_matrix const & x )
  61. {
  62. BOOST_QVM_ASSERT(row>=0);
  63. BOOST_QVM_ASSERT(row<Rows);
  64. BOOST_QVM_ASSERT(col>=0);
  65. BOOST_QVM_ASSERT(col<Cols);
  66. return x.a[row][col];
  67. }
  68. static
  69. BOOST_QVM_INLINE_CRITICAL
  70. scalar_type &
  71. write_element_idx( int row, int col, this_matrix & x )
  72. {
  73. BOOST_QVM_ASSERT(row>=0);
  74. BOOST_QVM_ASSERT(row<Rows);
  75. BOOST_QVM_ASSERT(col>=0);
  76. BOOST_QVM_ASSERT(col<Cols);
  77. return x.a[row][col];
  78. }
  79. };
  80. } }
  81. #endif