vec.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #ifndef BOOST_QVM_VEC_HPP_INCLUDED
  2. #define BOOST_QVM_VEC_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/detail/vec_assign.hpp>
  7. #include <boost/qvm/assert.hpp>
  8. #include <boost/qvm/static_assert.hpp>
  9. namespace boost { namespace qvm {
  10. template <class T,int D>
  11. struct
  12. vec
  13. {
  14. T a[D];
  15. template <class R
  16. #if __cplusplus >= 201103L
  17. , class = typename enable_if<is_vec<R> >::type
  18. #endif
  19. >
  20. operator R() const
  21. {
  22. R r;
  23. assign(r,*this);
  24. return r;
  25. }
  26. };
  27. template <class V>
  28. struct vec_traits;
  29. template <class T,int Dim>
  30. struct
  31. vec_traits< vec<T,Dim> >
  32. {
  33. typedef vec<T,Dim> this_vector;
  34. typedef T scalar_type;
  35. static int const dim=Dim;
  36. template <int I>
  37. static
  38. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  39. scalar_type
  40. read_element( this_vector const & x )
  41. {
  42. BOOST_QVM_STATIC_ASSERT(I>=0);
  43. BOOST_QVM_STATIC_ASSERT(I<dim);
  44. return x.a[I];
  45. }
  46. template <int I>
  47. static
  48. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  49. scalar_type &
  50. write_element( this_vector & x )
  51. {
  52. BOOST_QVM_STATIC_ASSERT(I>=0);
  53. BOOST_QVM_STATIC_ASSERT(I<dim);
  54. return x.a[I];
  55. }
  56. static
  57. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  58. scalar_type
  59. read_element_idx( int i, this_vector const & x )
  60. {
  61. BOOST_QVM_ASSERT(i>=0);
  62. BOOST_QVM_ASSERT(i<dim);
  63. return x.a[i];
  64. }
  65. static
  66. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  67. scalar_type &
  68. write_element_idx( int i, this_vector & x )
  69. {
  70. BOOST_QVM_ASSERT(i>=0);
  71. BOOST_QVM_ASSERT(i<dim);
  72. return x.a[i];
  73. }
  74. };
  75. } }
  76. #endif