vec_traits_array.hpp 2.2 KB

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