vec_traits_array.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #ifndef BOOST_QVM_VEC_TRAITS_ARRAY_HPP_INCLUDED
  2. #define BOOST_QVM_VEC_TRAITS_ARRAY_HPP_INCLUDED
  3. // Copyright 2008-2022 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/config.hpp>
  7. #include <boost/qvm/deduce_vec.hpp>
  8. #include <boost/qvm/assert.hpp>
  9. #if __cplusplus > 199711L
  10. #include <array>
  11. namespace boost { namespace qvm {
  12. template <class T,std::size_t M,std::size_t N>
  13. struct
  14. vec_traits<std::array<std::array<T,M>,N> >
  15. {
  16. static int const dim=0;
  17. typedef void scalar_type;
  18. };
  19. template <class T,std::size_t Dim>
  20. struct
  21. vec_traits<std::array<T, Dim> >
  22. {
  23. typedef std::array<T, Dim> this_vector;
  24. typedef T scalar_type;
  25. static int const dim=int(Dim);
  26. template <int I>
  27. static
  28. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  29. scalar_type
  30. read_element( this_vector const & x )
  31. {
  32. BOOST_QVM_STATIC_ASSERT(I>=0);
  33. BOOST_QVM_STATIC_ASSERT(I<int(Dim));
  34. return x[I];
  35. }
  36. template <int I>
  37. static
  38. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  39. scalar_type &
  40. write_element( this_vector & x )
  41. {
  42. BOOST_QVM_STATIC_ASSERT(I>=0);
  43. BOOST_QVM_STATIC_ASSERT(I<int(Dim));
  44. return x[I];
  45. }
  46. static
  47. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  48. scalar_type
  49. read_element_idx( int i, this_vector const & x )
  50. {
  51. BOOST_QVM_ASSERT(i>=0);
  52. BOOST_QVM_ASSERT(i<int(Dim));
  53. return x[i];
  54. }
  55. static
  56. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  57. scalar_type &
  58. write_element_idx( int i, this_vector & x )
  59. {
  60. BOOST_QVM_ASSERT(i>=0);
  61. BOOST_QVM_ASSERT(i<int(Dim));
  62. return x[i];
  63. }
  64. };
  65. template <class T,std::size_t Dim>
  66. struct
  67. vec_traits<std::array<T, Dim> const>
  68. {
  69. typedef std::array<T, Dim> const this_vector;
  70. typedef T scalar_type;
  71. static int const dim=int(Dim);
  72. template <int I>
  73. static
  74. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  75. scalar_type
  76. read_element( this_vector & x )
  77. {
  78. BOOST_QVM_STATIC_ASSERT(I>=0);
  79. BOOST_QVM_STATIC_ASSERT(I<int(Dim));
  80. return x[I];
  81. }
  82. static
  83. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  84. scalar_type
  85. read_element_idx( int i, this_vector & x )
  86. {
  87. BOOST_QVM_ASSERT(i>=0);
  88. BOOST_QVM_ASSERT(i<int(Dim));
  89. return x[i];
  90. }
  91. };
  92. template <class T,std::size_t Dim,int D>
  93. struct
  94. deduce_vec<std::array<T,Dim>,D>
  95. {
  96. typedef vec<T,D> type;
  97. };
  98. template <class T,std::size_t Dim,int D>
  99. struct
  100. deduce_vec<std::array<T,Dim> const,D>
  101. {
  102. typedef vec<T,D> type;
  103. };
  104. template <class T1,class T2,std::size_t Dim,int D>
  105. struct
  106. deduce_vec2<std::array<T1,Dim>,std::array<T2,Dim>,D>
  107. {
  108. typedef vec<typename deduce_scalar<T1,T2>::type,D> type;
  109. };
  110. template <class T1,class T2,std::size_t Dim,int D>
  111. struct
  112. deduce_vec2<std::array<T1,Dim> const,std::array<T2,Dim>,D>
  113. {
  114. typedef vec<typename deduce_scalar<T1,T2>::type,D> type;
  115. };
  116. template <class T1,class T2,std::size_t Dim,int D>
  117. struct
  118. deduce_vec2<std::array<T1,Dim>,std::array<T2,Dim> const,D>
  119. {
  120. typedef vec<typename deduce_scalar<T1,T2>::type,D> type;
  121. };
  122. template <class T1,class T2,std::size_t Dim,int D>
  123. struct
  124. deduce_vec2<std::array<T1,Dim> const,std::array<T2,Dim> const,D>
  125. {
  126. typedef vec<typename deduce_scalar<T1,T2>::type,D> type;
  127. };
  128. } }
  129. #endif
  130. namespace boost { namespace qvm {
  131. template <class T,int M,int N>
  132. struct
  133. vec_traits<T[M][N]>
  134. {
  135. static int const dim=0;
  136. typedef void scalar_type;
  137. };
  138. template <class T,int Dim>
  139. struct
  140. vec_traits<T[Dim]>
  141. {
  142. typedef T this_vector[Dim];
  143. typedef T scalar_type;
  144. static int const dim=Dim;
  145. template <int I>
  146. static
  147. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  148. scalar_type
  149. read_element( this_vector const & x )
  150. {
  151. BOOST_QVM_STATIC_ASSERT(I>=0);
  152. BOOST_QVM_STATIC_ASSERT(I<Dim);
  153. return x[I];
  154. }
  155. template <int I>
  156. static
  157. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  158. scalar_type &
  159. write_element( this_vector & x )
  160. {
  161. BOOST_QVM_STATIC_ASSERT(I>=0);
  162. BOOST_QVM_STATIC_ASSERT(I<Dim);
  163. return x[I];
  164. }
  165. static
  166. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  167. scalar_type
  168. read_element_idx( int i, this_vector const & x )
  169. {
  170. BOOST_QVM_ASSERT(i>=0);
  171. BOOST_QVM_ASSERT(i<Dim);
  172. return x[i];
  173. }
  174. static
  175. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  176. scalar_type &
  177. write_element_idx( int i, this_vector & x )
  178. {
  179. BOOST_QVM_ASSERT(i>=0);
  180. BOOST_QVM_ASSERT(i<Dim);
  181. return x[i];
  182. }
  183. };
  184. template <class T,int Dim,int D>
  185. struct
  186. deduce_vec<T[Dim],D>
  187. {
  188. typedef vec<T,D> type;
  189. };
  190. template <class T,int Dim,int D>
  191. struct
  192. deduce_vec<T const[Dim],D>
  193. {
  194. typedef vec<T,D> type;
  195. };
  196. template <class T1,class T2,int Dim,int D>
  197. struct
  198. deduce_vec2<T1[Dim],T2[Dim],D>
  199. {
  200. typedef vec<typename deduce_scalar<T1,T2>::type,D> type;
  201. };
  202. template <int Dim,class T>
  203. T (&ptr_vref( T * ptr ))[Dim]
  204. {
  205. return *reinterpret_cast<T (*)[Dim]>(ptr);
  206. }
  207. } }
  208. #endif