vec.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #ifndef BOOST_QVM_VEC_HPP_INCLUDED
  2. #define BOOST_QVM_VEC_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/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. operator R() const
  17. {
  18. R r;
  19. assign(r,*this);
  20. return r;
  21. }
  22. };
  23. template <class V>
  24. struct vec_traits;
  25. template <class T,int Dim>
  26. struct
  27. vec_traits< vec<T,Dim> >
  28. {
  29. typedef vec<T,Dim> this_vector;
  30. typedef T scalar_type;
  31. static int const dim=Dim;
  32. template <int I>
  33. static
  34. BOOST_QVM_INLINE_CRITICAL
  35. scalar_type
  36. read_element( this_vector const & x )
  37. {
  38. BOOST_QVM_STATIC_ASSERT(I>=0);
  39. BOOST_QVM_STATIC_ASSERT(I<dim);
  40. return x.a[I];
  41. }
  42. template <int I>
  43. static
  44. BOOST_QVM_INLINE_CRITICAL
  45. scalar_type &
  46. write_element( this_vector & x )
  47. {
  48. BOOST_QVM_STATIC_ASSERT(I>=0);
  49. BOOST_QVM_STATIC_ASSERT(I<dim);
  50. return x.a[I];
  51. }
  52. static
  53. BOOST_QVM_INLINE_CRITICAL
  54. scalar_type
  55. read_element_idx( int i, this_vector const & x )
  56. {
  57. BOOST_QVM_ASSERT(i>=0);
  58. BOOST_QVM_ASSERT(i<dim);
  59. return x.a[i];
  60. }
  61. static
  62. BOOST_QVM_INLINE_CRITICAL
  63. scalar_type &
  64. write_element_idx( int i, this_vector & x )
  65. {
  66. BOOST_QVM_ASSERT(i>=0);
  67. BOOST_QVM_ASSERT(i<dim);
  68. return x.a[i];
  69. }
  70. };
  71. } }
  72. #endif