vec_traits_array.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_DEC6035EA1C211DEA5E8ECB856D89593
  5. #define UUID_DEC6035EA1C211DEA5E8ECB856D89593
  6. #include <boost/qvm/inline.hpp>
  7. #include <boost/qvm/deduce_vec.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 M,int N>
  17. struct
  18. vec_traits<T[M][N]>
  19. {
  20. static int const dim=0;
  21. typedef void scalar_type;
  22. };
  23. template <class T,int Dim>
  24. struct
  25. vec_traits<T[Dim]>
  26. {
  27. typedef T this_vector[Dim];
  28. typedef typename qvm_detail::remove_const<T>::type scalar_type;
  29. static int const dim=Dim;
  30. template <int I>
  31. static
  32. BOOST_QVM_INLINE_CRITICAL
  33. scalar_type
  34. read_element( this_vector const & x )
  35. {
  36. BOOST_QVM_STATIC_ASSERT(I>=0);
  37. BOOST_QVM_STATIC_ASSERT(I<Dim);
  38. return x[I];
  39. }
  40. template <int I>
  41. static
  42. BOOST_QVM_INLINE_CRITICAL
  43. scalar_type &
  44. write_element( this_vector & x )
  45. {
  46. BOOST_QVM_STATIC_ASSERT(I>=0);
  47. BOOST_QVM_STATIC_ASSERT(I<Dim);
  48. return x[I];
  49. }
  50. static
  51. BOOST_QVM_INLINE_CRITICAL
  52. scalar_type
  53. read_element_idx( int i, this_vector const & x )
  54. {
  55. BOOST_QVM_ASSERT(i>=0);
  56. BOOST_QVM_ASSERT(i<Dim);
  57. return x[i];
  58. }
  59. static
  60. BOOST_QVM_INLINE_CRITICAL
  61. scalar_type &
  62. write_element_idx( int i, this_vector & x )
  63. {
  64. BOOST_QVM_ASSERT(i>=0);
  65. BOOST_QVM_ASSERT(i<Dim);
  66. return x[i];
  67. }
  68. };
  69. template <class T,int Dim,int D>
  70. struct
  71. deduce_vec<T[Dim],D>
  72. {
  73. typedef vec<T,D> type;
  74. };
  75. template <class T,int Dim,int D>
  76. struct
  77. deduce_vec<T const[Dim],D>
  78. {
  79. typedef vec<T,D> type;
  80. };
  81. template <class T1,class T2,int Dim,int D>
  82. struct
  83. deduce_vec2<T1[Dim],T2[Dim],D>
  84. {
  85. typedef vec<typename deduce_scalar<T1,T2>::type,D> type;
  86. };
  87. template <int Dim,class T>
  88. T (&ptr_vref( T * ptr ))[Dim]
  89. {
  90. return *reinterpret_cast<T (*)[Dim]>(ptr);
  91. }
  92. }
  93. }
  94. #endif