vec_traits.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2008-2022 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 BOOST_QVM_VEC_TRAITS_HPP_INCLUDED
  5. #define BOOST_QVM_VEC_TRAITS_HPP_INCLUDED
  6. #include <boost/qvm/is_scalar.hpp>
  7. #include <boost/qvm/enable_if.hpp>
  8. #include <boost/qvm/config.hpp>
  9. namespace boost { namespace qvm {
  10. template <class V>
  11. struct
  12. vec_traits
  13. {
  14. static int const dim=0;
  15. typedef void scalar_type;
  16. };
  17. template <class T>
  18. struct
  19. is_vec
  20. {
  21. static bool const value = is_scalar<typename vec_traits<T>::scalar_type>::value && vec_traits<T>::dim>0;
  22. };
  23. namespace
  24. qvm_detail
  25. {
  26. template <class T, T>
  27. struct
  28. vtr_dispatch_yes
  29. {
  30. char x, y;
  31. };
  32. }
  33. template <class T>
  34. class
  35. vec_write_element_ref
  36. {
  37. template <class U>
  38. static qvm_detail::vtr_dispatch_yes<typename vec_traits<U>::scalar_type & (*)( U & ), &vec_traits<U>::template write_element<0> > check(int);
  39. template <class>
  40. static char check(long);
  41. public:
  42. static bool const value = sizeof(check<T>(0)) > 1;
  43. };
  44. template <int I, class V>
  45. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  46. typename enable_if_c<
  47. vec_write_element_ref<V>::value,
  48. void>::type
  49. write_vec_element( V & v, typename vec_traits<V>::scalar_type s )
  50. {
  51. vec_traits<V>::template write_element<I>(v) = s;
  52. }
  53. template <int I, class V>
  54. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  55. typename enable_if_c<
  56. !vec_write_element_ref<V>::value,
  57. void>::type
  58. write_vec_element( V & v, typename vec_traits<V>::scalar_type s )
  59. {
  60. vec_traits<V>::template write_element<I>(v, s);
  61. }
  62. template <class V>
  63. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  64. typename enable_if_c<
  65. vec_write_element_ref<V>::value,
  66. void>::type
  67. write_vec_element_idx( int i, V & v, typename vec_traits<V>::scalar_type s )
  68. {
  69. vec_traits<V>::write_element_idx(i, v) = s;
  70. }
  71. template <class V>
  72. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  73. typename enable_if_c<
  74. !vec_write_element_ref<V>::value,
  75. void>::type
  76. write_vec_element_idx( int i, V & v, typename vec_traits<V>::scalar_type s )
  77. {
  78. vec_traits<V>::write_element_idx(i, v, s);
  79. }
  80. } }
  81. #endif