Boost QVM

m_traits

#include <boost/qvm/m_traits.hpp>

namespace boost
{
    namespace qvm
    {
        template <class M>
        struct m_traits
        {
            /*main template members unspecified*/
        };
        
        /*
        User-defined (possibly partial) specializations:
        
        template <>
        struct m_traits<M>
        {
            static int const rows = /*user-defined*/;        
            static int const cols = /*user-defined*/;        
            typedef /*user-defined*/ scalar_type;        
        
            template <int R,int C> static inline scalar_type r( Matrix const & m );        
            template <int R,int C> static inline scalar_type & w( Matrix & m );        
        
            static inline scalar_type ir( int r, int c, Matrix const & m );        
            static inline scalar_type & iw( int r, int c, Matrix & m );        
        };
        */
    }
}

The m_traits template must be specialized for (user-defined) matrix types in order to enable vector and matrix operations defined in Boost QVM headers for objects of those types.

Note: matrix types are not required to be copyable.

The main m_traits template members are not specified. Valid specializations are required to define the following members:

  • rows: the expression m_traits<Matrix>::rows must evaluate to a compile-time integer constant greater than 0 that specifies the number of rows in a matrix.
  • cols must evaluate to a compile-time integer constant greater than 0 that specifies the number of columns in a matrix.
  • scalar_type: the expression m_traits<Matrix>::scalar_type must be a value type which satisfies the scalar requirements.

In addition, valid specializations of the m_traits template may define the following access functions as static members, where m is an object of type Matrix, R and C are compile-time integer constants, and r and c are variables of type int:

  • r: the expression m_traits<Matrix>::r<R,C>(m) returns either a copy of or a const reference to the element at row R and column C of m.
  • w: the expression m_traits<Matrix>::w<R,C>(m) returns mutable reference to the element at row R and column C of m.
  • ir: the expression m_traits<Matrix>::ir(r,c,m) returns either a copy of or a const reference to the element at row r and column c of m.
  • iw: the expression m_traits<Matrix>::iw(r,c,m) returns mutable reference to the element at row r and column c of m.

It is illegal to call any of the above functions unless is_m<Matrix>::value is true. Even then, matrix types are allowed to define only a subset of the access functions. The general requirements are:

  • if ir is defined, r must also be defined;
  • if iw is defined, w must also be defined.

Below is an example of a user-defined 3x3 matrix type, and its corresponding specialization of the m_traits template:

#include <boost/qvm/m_traits.hpp>

struct float33 { float a[3][3]; };

namespace boost
{
    namespace qvm
    {
        template <>
        struct m_traits<float33>
        {
            static int const rows=3;
            static int const cols=3;
            typedef float scalar_type;

            template <int R,int C> static inline scalar_type & w( float33 & m ) { return m.a[R][C]; }
            template <int R,int C> static inline scalar_type r( float33 const & m ) { return m.a[R][C]; }

            static inline scalar_type & iw( int r, int c, float33 & m ) { return m.a[r][c]; }
            static inline scalar_type ir( int r, int c, float33 const & m ) { return m.a[r][c]; }
        };
    }
}