Tomographer
v3.0
Tomographer C++ Framework Documentation
|
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... | |
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 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
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.
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).
|
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.
|
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.