Tomographer  v5.3
Tomographer C++ Framework Documentation
Tomographer::MAT::VarValueDecoder< T, Enabled > Class Template Reference

Specializable template which takes care of decoding values. More...

#include <tomographer/tools/ezmatio.h>

Public Types

typedef T RetType
 Type returned by decodeValue() More...
 

Static Public Member Functions

static void checkShape (const Var &var)
 Check that the type and shape of the var are compatible with T. More...
 
static RetType decodeValue (const Var &var)
 Decode the variable var into the C++ type T. More...
 

Detailed Description

template<typename T, typename Enabled = void>
class Tomographer::MAT::VarValueDecoder< T, Enabled >

Specializable template which takes care of decoding values.

Specialize this template to your favorite return type to define a decoder for that type.

This class does two things:

  • it ensures that the variable in the data file has a type and shape which is compatible with the C++ data type requested (T)
  • it decodes the MAT Matlab data via the C MatIO interface (see the matvar_t structure) to transform it into the requested native C++ object

It may be that the decoding routine might not want to have directly T as a return type, but rather return a type which is convertible to T or which can be used as an initializer for a T. (Think, for example, an Eigen mapped type.)

Another reason for not returning T directly is if T is a proxy type which describes which type is requested along with some parameters (e.g. storage order). See for example GetStdVector.

This default class template does nothing. Specialize it for your C++ type or use one of the available specializations.

The Enabled template parameter is provided for convenience, if you wish to employ SFINAE to conditionally enable specializations. See the VarValueDecoder<T> for simple numeric values for an example. The idea is to use a construct

template<...>
struct VarValueDecoder<..., typename std::enable_if<condition>::type >

where the second argument (Enabled) is an invalid C++ expression (SFINAE) if the required condition is not met, and resolves to void if the condition is met. Don't forget that because of the initial template declaration, when invoking VarValueDecoder<T>, the second paramter is always defaulted to void regardless of the specialization.

In addition, this class may have an additional typedef (or nested struct/class) member named Params (no example available yet). In that case, it is possible to pass a second argument to the Var::value() method (same for value()), precisely of the type Params. This argument is passed as second argument to both checkShape() and decodeValue(); these methods may use this additional argument however they like. For example, the Params may be flags or options specifying how to encode the value into the type T, or may be the requested shape of the return type, or whatever other additional info the decoding procedure may like to have.

Definition at line 802 of file ezmatio.h.

Member Typedef Documentation

§ RetType

template<typename T, typename Enabled = void>
typedef T Tomographer::MAT::VarValueDecoder< T, Enabled >::RetType

Type returned by decodeValue()

The specialization of VarValueDecoder<T> should explicitly specify which type is returned by the main decoding routine (see class doc).

Definition at line 810 of file ezmatio.h.

Member Function Documentation

§ checkShape()

template<typename T, typename Enabled = void>
static void Tomographer::MAT::VarValueDecoder< T, Enabled >::checkShape ( const Var var)
inlinestatic

Check that the type and shape of the var are compatible with T.

Specializations of VarValueDecoder<T> should implement this function and check whether the given variable can be decoded into the requested C++ type T. If there is any error, throw a VarTypeError exception.

Definition at line 818 of file ezmatio.h.

§ decodeValue()

template<typename T , typename Enabled >
VarValueDecoder< T, Enabled >::RetType Tomographer::MAT::VarValueDecoder< T, Enabled >::decodeValue ( const Var var)
inlinestatic

Decode the variable var into the C++ type T.

Specializations of VarValueDecoder<T> should implement this function to read data in the MATLAB variable var and return an instance of the requested C++ type T.

Note the return type of this function need not be T itself, but any type you like, presumably one that is convertible to T, assignable to T, or which can be used as initializer for T. You may find this useful if you want to return an initializer type such as, e.g., an Eigen generator expression.

Do note, though, that with modern compilers performing return value optimization (RVO), in most cases there shouldn't be any real overhead in returning a T directly.

Definition at line 1191 of file ezmatio.h.


The documentation for this class was generated from the following file: