Boost QVM

v_traits

#include <boost/qvm/v_traits.hpp>

namespace
boost
{
    namespace
    qvm
    {
        template <class V>
        struct v_traits
        {
            /*main template members unspecified*/
        };
        
        /*
        User-defined (possibly partial) specializations:
        
        template <>
        struct v_traits<V>
        {
            static int const dim = /*user-defined*/;        
            typedef /*user-defined*/ scalar_type;        
        
            template <int I> static inline scalar_type r( Vector const & v );        
            template <int I> static inline scalar_type & w( Vector & v );        
        
            static inline scalar_type ir( int i, Vector const & v );        
            static inline scalar_type & iw( int i, Vector & v );        
        };
        */
    }
}

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

Note: vector types are not required to be copyable.

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

  • dim: the expression v_traits<Vector>::dim must evaluate to a compile-time integer constant greater than 0 that specifies the vector size.
  • scalar_type: the expression v_traits<Vector>::scalar_type must be a value type which satisfies the scalar requirements.

In addition, valid specializations of the v_traits template may define the following access functions as static members, where v is an object of type Vector, I is a compile-time integer constant, and i is a variable of type int:

  • r: the expression v_traits<Vector>::r<I>(v) returns either a copy of or a const reference to the I-th element of v.
  • w: the expression v_traits<Vector>::w<I>(v) returns mutable reference to the I-th element of v.
  • ir: the expression v_traits<Vector>::ir(i,v) returns either a copy of or a const reference to the i-th element of v.
  • iw: the expression v_traits<Vector>::iw(i,v) returns mutable reference to the i-th element of v.

It is illegal to call any of the above functions unless is_v<Vector>::value is true. Even then, vector 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 3D vector type, and its corresponding specialization of the v_traits template:

#include <boost/qvm/v_traits.hpp>

struct float3 { float a[3]; };

namespace boost
{
    namespace qvm
    {
        template <>
        struct v_traits<float3>
        {
            static int const dim=3;
            typedef float scalar_type;

            template <int I> static inline scalar_type & w( float3 & v ) { return v.a[I]; }
            template <int I> static inline scalar_type r( float3 const & v ) { return v.a[I]; }

            static inline scalar_type & iw( int i, float3 & v ) { return v.a[i]; }
            static inline scalar_type ir( int i, float3 const & v ) { return v.a[i]; }
        };
    }
}