mat_traits_array.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef BOOST_QVM_MAT_TRAITS_ARRAY_HPP_INCLUDED
  2. #define BOOST_QVM_MAT_TRAITS_ARRAY_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/inline.hpp>
  7. #include <boost/qvm/deduce_mat.hpp>
  8. #include <boost/qvm/detail/remove_const.hpp>
  9. #include <boost/qvm/assert.hpp>
  10. namespace boost { namespace qvm {
  11. template <class T,int R,int Q,int C>
  12. struct
  13. mat_traits<T[R][Q][C]>
  14. {
  15. static int const rows=0;
  16. static int const cols=0;
  17. typedef void scalar_type;
  18. };
  19. template <class T,int Rows,int Cols>
  20. struct
  21. mat_traits<T[Rows][Cols]>
  22. {
  23. typedef T this_matrix[Rows][Cols];
  24. typedef typename qvm_detail::remove_const<T>::type scalar_type;
  25. static int const rows=Rows;
  26. static int const cols=Cols;
  27. template <int Row,int Col>
  28. static
  29. BOOST_QVM_INLINE_CRITICAL
  30. scalar_type
  31. read_element( this_matrix const & x )
  32. {
  33. BOOST_QVM_STATIC_ASSERT(Row>=0);
  34. BOOST_QVM_STATIC_ASSERT(Row<Rows);
  35. BOOST_QVM_STATIC_ASSERT(Col>=0);
  36. BOOST_QVM_STATIC_ASSERT(Col<Cols);
  37. return x[Row][Col];
  38. }
  39. template <int Row,int Col>
  40. static
  41. BOOST_QVM_INLINE_CRITICAL
  42. scalar_type &
  43. write_element( this_matrix & x )
  44. {
  45. BOOST_QVM_STATIC_ASSERT(Row>=0);
  46. BOOST_QVM_STATIC_ASSERT(Row<Rows);
  47. BOOST_QVM_STATIC_ASSERT(Col>=0);
  48. BOOST_QVM_STATIC_ASSERT(Col<Cols);
  49. return x[Row][Col];
  50. }
  51. static
  52. BOOST_QVM_INLINE_CRITICAL
  53. scalar_type
  54. read_element_idx( int row, int col, this_matrix const & x )
  55. {
  56. BOOST_QVM_ASSERT(row>=0);
  57. BOOST_QVM_ASSERT(row<Rows);
  58. BOOST_QVM_ASSERT(col>=0);
  59. BOOST_QVM_ASSERT(col<Cols);
  60. return x[row][col];
  61. }
  62. static
  63. BOOST_QVM_INLINE_CRITICAL
  64. scalar_type &
  65. write_element_idx( int row, int col, this_matrix & x )
  66. {
  67. BOOST_QVM_ASSERT(row>=0);
  68. BOOST_QVM_ASSERT(row<Rows);
  69. BOOST_QVM_ASSERT(col>=0);
  70. BOOST_QVM_ASSERT(col<Cols);
  71. return x[row][col];
  72. }
  73. };
  74. template <class T,int Rows,int Cols,int R,int C>
  75. struct
  76. deduce_mat<T[Rows][Cols],R,C>
  77. {
  78. typedef mat<T,R,C> type;
  79. };
  80. template <class T,int Rows,int Cols,int R,int C>
  81. struct
  82. deduce_mat<T const[Rows][Cols],R,C>
  83. {
  84. typedef mat<T,R,C> type;
  85. };
  86. template <class T1,class T2,int Rows,int Cols,int R,int C>
  87. struct
  88. deduce_mat2<T1[Rows][Cols],T2[Rows][Cols],R,C>
  89. {
  90. typedef mat<typename deduce_scalar<T1,T2>::type,R,C> type;
  91. };
  92. template <int Rows,int Cols,class T>
  93. T (&ptr_mref( T * ptr ))[Rows][Cols]
  94. {
  95. return *reinterpret_cast<T (*)[Rows][Cols]>(ptr);
  96. }
  97. } }
  98. #endif