| 1 | #ifndef BOOST_QVM_TRAITS_HPP_INCLUDED |
| 2 | #define BOOST_QVM_TRAITS_HPP_INCLUDED |
| 3 | |
| 4 | // Copyright 2008-2022 Emil Dotchevski and Reverge Studios, Inc. |
| 5 | |
| 6 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 7 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 8 | |
| 9 | #include <boost/qvm/is_scalar.hpp> |
| 10 | #include <boost/qvm/enable_if.hpp> |
| 11 | #include <boost/qvm/config.hpp> |
| 12 | |
| 13 | namespace boost { namespace qvm { |
| 14 | |
| 15 | template <class M> |
| 16 | struct |
| 17 | mat_traits |
| 18 | { |
| 19 | static int const rows=0; |
| 20 | static int const cols=0; |
| 21 | typedef void scalar_type; |
| 22 | }; |
| 23 | |
| 24 | template <class T> |
| 25 | struct |
| 26 | is_mat |
| 27 | { |
| 28 | static bool const value = is_scalar<typename mat_traits<T>::scalar_type>::value && mat_traits<T>::rows>0 && mat_traits<T>::cols>0; |
| 29 | }; |
| 30 | |
| 31 | namespace |
| 32 | qvm_detail |
| 33 | { |
| 34 | template <class T, T> |
| 35 | struct |
| 36 | mtr_dispatch_yes |
| 37 | { |
| 38 | char x, y; |
| 39 | }; |
| 40 | } |
| 41 | |
| 42 | template <class T> |
| 43 | class |
| 44 | mat_write_element_ref |
| 45 | { |
| 46 | template <class U> |
| 47 | static qvm_detail::mtr_dispatch_yes<typename mat_traits<U>::scalar_type & (*)( U & ), &mat_traits<U>::template write_element<0,0> > check(int); |
| 48 | |
| 49 | template <class> |
| 50 | static char check(long); |
| 51 | |
| 52 | public: |
| 53 | |
| 54 | static bool const value = sizeof(check<T>(0)) > 1; |
| 55 | }; |
| 56 | |
| 57 | template <int R, int C, class M> |
| 58 | BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL |
| 59 | typename enable_if_c< |
| 60 | mat_write_element_ref<M>::value, |
| 61 | void>::type |
| 62 | write_mat_element( M & m, typename mat_traits<M>::scalar_type s ) |
| 63 | { |
| 64 | mat_traits<M>::template write_element<R,C>(m) = s; |
| 65 | } |
| 66 | |
| 67 | template <int R, int C, class M> |
| 68 | BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL |
| 69 | typename enable_if_c< |
| 70 | !mat_write_element_ref<M>::value, |
| 71 | void>::type |
| 72 | write_mat_element( M & m, typename mat_traits<M>::scalar_type s ) |
| 73 | { |
| 74 | mat_traits<M>::template write_element<R,C>(m, s); |
| 75 | } |
| 76 | |
| 77 | template <class M> |
| 78 | BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL |
| 79 | typename enable_if_c< |
| 80 | mat_write_element_ref<M>::value, |
| 81 | void>::type |
| 82 | write_mat_element_idx( int r, int c, M & m, typename mat_traits<M>::scalar_type s ) |
| 83 | { |
| 84 | mat_traits<M>::write_element_idx(r, c, m) = s; |
| 85 | } |
| 86 | |
| 87 | template <class M> |
| 88 | BOOST_QVM_CONSTEXPR BOOST_QVM_INLINE_CRITICAL |
| 89 | typename enable_if_c< |
| 90 | !mat_write_element_ref<M>::value, |
| 91 | void>::type |
| 92 | write_mat_element_idx( int r, int c, M & m, typename mat_traits<M>::scalar_type s ) |
| 93 | { |
| 94 | mat_traits<M>::write_element_idx(r, c, m, s); |
| 95 | } |
| 96 | |
| 97 | } } |
| 98 | |
| 99 | #endif |
| 100 | |