mat_traits.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #ifndef BOOST_QVM_TRAITS_HPP_INCLUDED
  2. #define BOOST_QVM_TRAITS_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/is_scalar.hpp>
  7. #include <boost/qvm/enable_if.hpp>
  8. #include <boost/qvm/config.hpp>
  9. namespace boost { namespace qvm {
  10. template <class M>
  11. struct
  12. mat_traits
  13. {
  14. static int const rows=0;
  15. static int const cols=0;
  16. typedef void scalar_type;
  17. };
  18. template <class T>
  19. struct
  20. is_mat
  21. {
  22. static bool const value = is_scalar<typename mat_traits<T>::scalar_type>::value && mat_traits<T>::rows>0 && mat_traits<T>::cols>0;
  23. };
  24. namespace
  25. qvm_detail
  26. {
  27. template <class T, T>
  28. struct
  29. mtr_dispatch_yes
  30. {
  31. char x, y;
  32. };
  33. }
  34. template <class T>
  35. class
  36. mat_write_element_ref
  37. {
  38. template <class U>
  39. static qvm_detail::mtr_dispatch_yes<typename mat_traits<U>::scalar_type & (*)( U & ), &mat_traits<U>::template write_element<0,0> > check(int);
  40. template <class>
  41. static char check(long);
  42. public:
  43. static bool const value = sizeof(check<T>(0)) > 1;
  44. };
  45. template <int R, int C, class M>
  46. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  47. typename enable_if_c<
  48. mat_write_element_ref<M>::value,
  49. void>::type
  50. write_mat_element( M & m, typename mat_traits<M>::scalar_type s )
  51. {
  52. mat_traits<M>::template write_element<R,C>(m) = s;
  53. }
  54. template <int R, int C, class M>
  55. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  56. typename enable_if_c<
  57. !mat_write_element_ref<M>::value,
  58. void>::type
  59. write_mat_element( M & m, typename mat_traits<M>::scalar_type s )
  60. {
  61. mat_traits<M>::template write_element<R,C>(m, s);
  62. }
  63. template <class M>
  64. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  65. typename enable_if_c<
  66. mat_write_element_ref<M>::value,
  67. void>::type
  68. write_mat_element_idx( int r, int c, M & m, typename mat_traits<M>::scalar_type s )
  69. {
  70. mat_traits<M>::write_element_idx(r, c, m) = s;
  71. }
  72. template <class M>
  73. BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL
  74. typename enable_if_c<
  75. !mat_write_element_ref<M>::value,
  76. void>::type
  77. write_mat_element_idx( int r, int c, M & m, typename mat_traits<M>::scalar_type s )
  78. {
  79. mat_traits<M>::write_element_idx(r, c, m, s);
  80. }
  81. } }
  82. #endif