QVM: Quaternions, Vectors, Matrices

mat_traits_defaults

#include <boost/qvm/mat_traits_defaults.hpp>

namespace boost
{
    namespace qvm
    {
        template <class MatType,class ScalarType,int Rows,int Cols>
        struct mat_traits_defaults
        {
            typedef MatType mat_type;
            typedef ScalarType scalar_type;
            static int const rows=Rows;
            static int const cols=Cols;
        
            template <int Row,int Col>
            static BOOST_QVM_INLINE_CRITICAL
            scalar_type write_element( mat_type const & x )
            {
                return mat_traits<mat_type>::template write_element<Row,Col>(const_cast<mat_type &>(x));
            }
        
            static BOOST_QVM_INLINE_CRITICAL
            scalar_type read_element_idx( int r, int c, mat_type const & x )
            {
                return mat_traits<mat_type>::write_element_idx(r,c,const_cast<mat_type &>(x));
            }
        
            protected:
        
            static BOOST_QVM_INLINE_TRIVIAL
            scalar_type & write_element_idx( int r, int c, mat_type & m )
            {
                /* unspecified */
            }
        };
    }
}

The mat_traits_defaults template is designed to be used as a public base for user-defined specializations of the mat_traits template, to easily define the required members. If it is used, the only member that must be defined by the user in a mat_traits specialization is write_element; the mat_traits_defaults base will define read_element, as well as scalar_type, rows and cols automatically.

Optionally, the user may also define write_element_idx, in which case the mat_traits_defaults base will provide a suitable read_element_idx definition automatically. In addition, mat_traits_defaults defines a protected implementation of write_element_idx which may be made publicly available by the deriving mat_traits specialization in case the matrix type for which it is being specialized can not be indexed efficiently. This write_element_idx function is less efficient (using meta-programming), implemented in terms of the required user-defined write_element.