mat_traits_array.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //Copyright (c) 2008-2016 Emil Dotchevski and Reverge Studios, Inc.
  2. //Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #ifndef UUID_FA3ED0DCA17911DEA6BBA09955D89593
  5. #define UUID_FA3ED0DCA17911DEA6BBA09955D89593
  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
  11. boost
  12. {
  13. namespace
  14. qvm
  15. {
  16. template <class T,int R,int CR,int C>
  17. struct
  18. mat_traits<T[R][CR][C]>
  19. {
  20. static int const rows=0;
  21. static int const cols=0;
  22. typedef void scalar_type;
  23. };
  24. template <class T,int Rows,int Cols>
  25. struct
  26. mat_traits<T[Rows][Cols]>
  27. {
  28. typedef T this_matrix[Rows][Cols];
  29. typedef typename qvm_detail::remove_const<T>::type scalar_type;
  30. static int const rows=Rows;
  31. static int const cols=Cols;
  32. template <int Row,int Col>
  33. static
  34. BOOST_QVM_INLINE_CRITICAL
  35. scalar_type
  36. read_element( this_matrix const & x )
  37. {
  38. BOOST_QVM_STATIC_ASSERT(Row>=0);
  39. BOOST_QVM_STATIC_ASSERT(Row<Rows);
  40. BOOST_QVM_STATIC_ASSERT(Col>=0);
  41. BOOST_QVM_STATIC_ASSERT(Col<Cols);
  42. return x[Row][Col];
  43. }
  44. template <int Row,int Col>
  45. static
  46. BOOST_QVM_INLINE_CRITICAL
  47. scalar_type &
  48. write_element( this_matrix & x )
  49. {
  50. BOOST_QVM_STATIC_ASSERT(Row>=0);
  51. BOOST_QVM_STATIC_ASSERT(Row<Rows);
  52. BOOST_QVM_STATIC_ASSERT(Col>=0);
  53. BOOST_QVM_STATIC_ASSERT(Col<Cols);
  54. return x[Row][Col];
  55. }
  56. static
  57. BOOST_QVM_INLINE_CRITICAL
  58. scalar_type
  59. read_element_idx( int row, int col, this_matrix const & x )
  60. {
  61. BOOST_QVM_ASSERT(row>=0);
  62. BOOST_QVM_ASSERT(row<Rows);
  63. BOOST_QVM_ASSERT(col>=0);
  64. BOOST_QVM_ASSERT(col<Cols);
  65. return x[row][col];
  66. }
  67. static
  68. BOOST_QVM_INLINE_CRITICAL
  69. scalar_type &
  70. write_element_idx( int row, int col, this_matrix & x )
  71. {
  72. BOOST_QVM_ASSERT(row>=0);
  73. BOOST_QVM_ASSERT(row<Rows);
  74. BOOST_QVM_ASSERT(col>=0);
  75. BOOST_QVM_ASSERT(col<Cols);
  76. return x[row][col];
  77. }
  78. };
  79. template <class T,int Rows,int Cols,int R,int C>
  80. struct
  81. deduce_mat<T[Rows][Cols],R,C>
  82. {
  83. typedef mat<T,R,C> type;
  84. };
  85. template <class T,int Rows,int Cols,int R,int C>
  86. struct
  87. deduce_mat<T const[Rows][Cols],R,C>
  88. {
  89. typedef mat<T,R,C> type;
  90. };
  91. template <class T1,class T2,int Rows,int Cols,int R,int C>
  92. struct
  93. deduce_mat2<T1[Rows][Cols],T2[Rows][Cols],R,C>
  94. {
  95. typedef mat<typename deduce_scalar<T1,T2>::type,R,C> type;
  96. };
  97. template <int Rows,int Cols,class T>
  98. T (&ptr_mref( T * ptr ))[Rows][Cols]
  99. {
  100. return *reinterpret_cast<T (*)[Rows][Cols]>(ptr);
  101. }
  102. }
  103. }
  104. #endif