matio-cpp  v0.2.5
A C++ wrapper of the matio library, with memory ownership handling, to read and write .mat files.
matioCpp::StructArray Class Reference

StructArray is a particular type of Variable specialized for array of structs. More...

#include <matioCpp/StructArray.h>

+ Inheritance diagram for matioCpp::StructArray:

Public Types

using index_type = size_t
 
using Element = StructArrayElement< false >
 The type used for indices. More...
 
using ConstElement = StructArrayElement< true >
 Non-const version of StructArrayElement. More...
 

Public Member Functions

 StructArray ()
 Const version of Element. More...
 
 StructArray (const std::string &name)
 Constructor. More...
 
 StructArray (const std::string &name, const std::vector< index_type > &dimensions)
 Constructor. More...
 
 StructArray (const std::string &name, const std::vector< index_type > &dimensions, const std::vector< matioCpp::Struct > &elements)
 Constructor. More...
 
 StructArray (const std::string &name, const std::vector< index_type > &dimensions, const std::vector< std::string > &fields)
 Constructor. More...
 
 StructArray (const StructArray &other)
 Copy constructor. More...
 
 StructArray (StructArray &&other)
 Move constructor. More...
 
 StructArray (const MatvarHandler &handler)
 Constructor to share the data ownership of another variable. More...
 
 ~StructArray ()
 Destructor. More...
 
StructArrayoperator= (const StructArray &other)
 Assignement operator (copy) from another StructArray. More...
 
StructArrayoperator= (StructArray &&other)
 Assignement operator (move) from another StructArray. More...
 
bool fromVectorOfStructs (const std::vector< index_type > &dimensions, const std::vector< matioCpp::Struct > &elements)
 Set from a vector of Variables. More...
 
index_type rawIndexFromIndices (const std::vector< index_type > &el) const
 Get the linear index corresponding to the provided indices. More...
 
bool indicesFromRawIndex (size_t rawIndex, std::vector< index_type > &el) const
 Get the indices given the raw index. More...
 
bool setName (const std::string &newName)
 Change the name of the Variable. More...
 
void resize (const std::vector< index_type > &newDimensions)
 Resize the array. More...
 
index_type numberOfElements () const
 Get the total number of elements in the array. More...
 
index_type numberOfFields () const
 Get the total number of fields in the struct. More...
 
std::vector< std::stringfields () const
 Get the list of fields. More...
 
void clear ()
 Clear the struct array. More...
 
bool isFieldExisting (const std::string &field) const
 Check if a field is existing It performs a linear search over the output of fields(). More...
 
size_t getFieldIndex (const std::string &field) const
 Get the index of the specified field in the struct by performing a linear search. More...
 
bool addField (const std::string &newField)
 Add a new field to all the structs. More...
 
bool addFields (const std::vector< std::string > &newFields)
 Add the fields to all the structs. More...
 
bool setElement (const std::vector< index_type > &el, const matioCpp::Struct &newValue)
 Set the element at the specified position. More...
 
bool setElement (index_type el, const matioCpp::Struct &newValue)
 Set the element at the specified position. More...
 
Element operator() (const std::vector< index_type > &el)
 Access specified element. More...
 
ConstElement operator() (const std::vector< index_type > &el) const
 Access specified element. More...
 
Element operator() (index_type el)
 Access specified element. More...
 
ConstElement operator() (index_type el) const
 Access specified element. More...
 
Element operator[] (const std::vector< index_type > &el)
 Access specified element. More...
 
ConstElement operator[] (const std::vector< index_type > &el) const
 Access specified element. More...
 
Element operator[] (index_type el)
 Access specified element. More...
 
ConstElement operator[] (index_type el) const
 Access specified element. More...
 
- Public Member Functions inherited from matioCpp::Variable
 Variable ()
 Default constructor. More...
 
 Variable (const matvar_t *inputVar)
 Constructor from an existing matvar_t pointer. More...
 
 Variable (const Variable &other)
 Copy constructor. More...
 
 Variable (Variable &&other)
 Move constructor. More...
 
 Variable (const MatvarHandler &handler)
 Constructor to share the data ownership of another variable. More...
 
 ~Variable ()
 Destructor. More...
 
Variableoperator= (const Variable &other)
 Copy assignement. More...
 
Variableoperator= (Variable &&other)
 Move assignement. More...
 
bool fromMatio (const matvar_t *inputVar)
 Set this variable from an existing matio variable. More...
 
bool fromOther (const Variable &other)
 Set this variable from another variable. More...
 
bool fromOther (Variable &&other)
 Set this variable from another variable. More...
 
const matvar_t * toMatio () const
 Convert this Variable to a matio variable. More...
 
matvar_t * toMatio ()
 Convert this Variable to a matio variable. More...
 
std::string name () const
 Get the name of the Variable. More...
 
matioCpp::VariableType variableType () const
 Get the VariableType. More...
 
matioCpp::ValueType valueType () const
 Get the ValueType. More...
 
bool isComplex () const
 Get if the variable is complex. More...
 
matioCpp::Span< const size_t > dimensions () const
 Get the dimensions of this object. More...
 
bool isValid () const
 Check if the variable is valid. More...
 
template<typename T >
matioCpp::Element< T > asElement ()
 Cast the variable as a Element. More...
 
template<typename T >
const matioCpp::Element< T > asElement () const
 Cast the variable as a const Element. More...
 
template<typename T >
matioCpp::Vector< T > asVector ()
 Cast the variable as a Vector. More...
 
template<typename T >
const matioCpp::Vector< T > asVector () const
 Cast the variable as a const Vector. More...
 
matioCpp::String asString ()
 Cast the variable as a String. More...
 
const matioCpp::String asString () const
 Cast the variable as a const String. More...
 
matioCpp::String16 asString16 ()
 Cast the variable as a String16. More...
 
const matioCpp::String16 asString16 () const
 Cast the variable as a const String16. More...
 
matioCpp::String32 asString32 ()
 Cast the variable as a String32. More...
 
const matioCpp::String32 asString32 () const
 Cast the variable as a const String32. More...
 
template<typename T >
matioCpp::MultiDimensionalArray< T > asMultiDimensionalArray ()
 Cast the variable as a MultiDimensionalArray. More...
 
template<typename T >
const matioCpp::MultiDimensionalArray< T > asMultiDimensionalArray () const
 Cast the variable as a const MultiDimensionalArray. More...
 
matioCpp::CellArray asCellArray ()
 Cast the variable as a CellArray. More...
 
const matioCpp::CellArray asCellArray () const
 Cast the variable as a const CellArray. More...
 
matioCpp::Struct asStruct ()
 Cast the variable as a Struct. More...
 
const matioCpp::Struct asStruct () const
 Cast the variable as a const Struct. More...
 
matioCpp::StructArray asStructArray ()
 Cast the variable as a StructArray. More...
 
const matioCpp::StructArray asStructArray () const
 Cast the variable as a const StructArray. More...
 

Friends

template<bool >
class StructArrayElement
 

Additional Inherited Members

- Protected Member Functions inherited from matioCpp::Variable
bool initializeVariable (const std::string &name, const VariableType &variableType, const ValueType &valueType, matioCpp::Span< const size_t > dimensions, void *data)
 Initialize the variable. More...
 
bool initializeComplexVariable (const std::string &name, const VariableType &variableType, const ValueType &valueType, matioCpp::Span< const size_t > dimensions, void *realData, void *imaginaryData)
 Initialize a complex variable. More...
 
template<typename T >
bool initializeComplexVector (const std::string &name, const Span< T > realInputVector, const Span< T > imaginaryInputVector)
 Initialize a complex vector. More...
 
bool changeName (const std::string &newName)
 Change the name of the variable. More...
 
size_t getArrayNumberOfElements () const
 Get the total number of elements in the array. More...
 
bool setCellElement (size_t linearIndex, const Variable &newValue)
 Set a cell element at a specified linear position. More...
 
Variable getCellElement (size_t linearIndex)
 Get a cell element at a specified linear position. More...
 
const Variable getCellElement (size_t linearIndex) const
 Get a cell element at a specified linear position. More...
 
size_t getStructNumberOfFields () const
 Get the total number of fields in the variable, considered as a struct. More...
 
char *const * getStructFields () const
 Get the list of fields in the variable, considered as a struct. More...
 
size_t getStructFieldIndex (const std::string &field) const
 Get the index of the specified field in the variable, considered as a struct. More...
 
bool setStructField (size_t index, const Variable &newValue, size_t structPositionInArray=0)
 Set the field of the struct at the specified position. More...
 
bool addStructField (const std::string &newField)
 Add a new field to the variable, considered as a struct. More...
 
bool setStructField (const std::string &field, const Variable &newValue, size_t structPositionInArray=0)
 Set the field of the struct given the newValue name. More...
 
Variable getStructField (size_t index, size_t structPositionInArray=0)
 Get the specified field in the variable, considered as a struct. More...
 
const Variable getStructField (size_t index, size_t structPositionInArray=0) const
 Get the specified field in the variable, considered as a struct. More...
 
Struct getStructArrayElement (size_t linearIndex)
 Get an element of the variable, considered as a StructArray. More...
 
const Struct getStructArrayElement (size_t linearIndex) const
 Get an element of the variable, considered as a StructArray. More...
 

Detailed Description

StructArray is a particular type of Variable specialized for array of structs.

Each element of the array is a Struct and they all share the same fields.

Definition at line 20 of file StructArray.h.

Member Typedef Documentation

◆ ConstElement

Non-const version of StructArrayElement.

Definition at line 41 of file StructArray.h.

◆ Element

The type used for indices.

Definition at line 39 of file StructArray.h.

◆ index_type

Definition at line 37 of file StructArray.h.

Constructor & Destructor Documentation

◆ StructArray() [1/8]

matioCpp::StructArray::StructArray ( )

Const version of Element.

Default Constructor

Note
The name is set to "unnamed_struct_array".

Definition at line 29 of file StructArray.cpp.

References matioCpp::StructArray, and matioCpp::VARIABLE.

◆ StructArray() [2/8]

matioCpp::StructArray::StructArray ( const std::string name)

Constructor.

Parameters
nameThe name of the StructArray

Definition at line 38 of file StructArray.cpp.

References matioCpp::StructArray, and matioCpp::VARIABLE.

◆ StructArray() [3/8]

matioCpp::StructArray::StructArray ( const std::string name,
const std::vector< index_type > &  dimensions 
)

Constructor.

Parameters
nameThe name of the StructArray
dimensionsThe dimensions of the StructArray

Definition at line 47 of file StructArray.cpp.

References std::endl(), matioCpp::StructArray, and matioCpp::VARIABLE.

◆ StructArray() [4/8]

matioCpp::StructArray::StructArray ( const std::string name,
const std::vector< index_type > &  dimensions,
const std::vector< matioCpp::Struct > &  elements 
)

Constructor.

Parameters
nameThe name of the StructArray
dimensionsThe dimensions of the StructArray
elementsThe elements to be added to the StructArray (they are copied)
Note
The name of the variables is not used
The vector is supposed to contain the variables in column-major format.
Each element is supposed to have the same set of fields.

Definition at line 64 of file StructArray.cpp.

References std::vector< T >::data(), std::endl(), matioCpp::MatvarHandler::GetMatvarDuplicate(), std::vector< T >::size(), matioCpp::StructArray, and matioCpp::VARIABLE.

◆ StructArray() [5/8]

matioCpp::StructArray::StructArray ( const std::string name,
const std::vector< index_type > &  dimensions,
const std::vector< std::string > &  fields 
)

Constructor.

Parameters
nameThe name of the StructArray
dimensionsThe dimensions of the StructArray
fieldsThe fields to be added to the StructArray

Definition at line 147 of file StructArray.cpp.

References std::endl(), matioCpp::StructArray, and matioCpp::VARIABLE.

◆ StructArray() [6/8]

matioCpp::StructArray::StructArray ( const StructArray other)

Copy constructor.

Definition at line 169 of file StructArray.cpp.

◆ StructArray() [7/8]

matioCpp::StructArray::StructArray ( matioCpp::StructArray &&  other)

Move constructor.

Definition at line 174 of file StructArray.cpp.

◆ StructArray() [8/8]

matioCpp::StructArray::StructArray ( const MatvarHandler handler)

Constructor to share the data ownership of another variable.

Parameters
handlerThe MatvarHandler handler to the matvar_t which has to be shared.

Definition at line 179 of file StructArray.cpp.

References matioCpp::MatvarHandler::get(), matioCpp::Variable::initializeVariable(), matioCpp::StructArray, matioCpp::MatvarHandler::valueType(), matioCpp::VARIABLE, and matioCpp::MatvarHandler::variableType().

◆ ~StructArray()

matioCpp::StructArray::~StructArray ( )

Destructor.

Definition at line 193 of file StructArray.cpp.

Member Function Documentation

◆ addField()

bool matioCpp::StructArray::addField ( const std::string newField)

Add a new field to all the structs.

Parameters
newFieldThe new field
Returns
True on success, false otherwise

Definition at line 382 of file StructArray.cpp.

References std::endl().

◆ addFields()

bool matioCpp::StructArray::addFields ( const std::vector< std::string > &  newFields)

Add the fields to all the structs.

Parameters
newFieldsThe new fields to add
Returns
True on success, false otherwise

Definition at line 391 of file StructArray.cpp.

◆ clear()

void matioCpp::StructArray::clear ( )

Clear the struct array.

Definition at line 367 of file StructArray.cpp.

References std::move().

◆ fields()

std::vector< std::string > matioCpp::StructArray::fields ( ) const

Get the list of fields.

Returns
The list of fields.

Definition at line 350 of file StructArray.cpp.

References std::vector< T >::emplace_back(), and std::vector< T >::reserve().

◆ fromVectorOfStructs()

bool matioCpp::StructArray::fromVectorOfStructs ( const std::vector< index_type > &  dimensions,
const std::vector< matioCpp::Struct > &  elements 
)

Set from a vector of Variables.

Parameters
dimensionsThe input dimensions
elementsThe elements to be set.
Note
Previous structs are lost
Each struct is supposed to have the same set of fields
Returns
True if successful. False otherwise, for example if one of the input variables is not valid.

Definition at line 210 of file StructArray.cpp.

References std::vector< T >::data(), std::endl(), matioCpp::MatvarHandler::GetMatvarDuplicate(), std::vector< T >::size(), matioCpp::StructArray, and matioCpp::VARIABLE.

◆ getFieldIndex()

size_t matioCpp::StructArray::getFieldIndex ( const std::string field) const

Get the index of the specified field in the struct by performing a linear search.

Parameters
fieldThe field to search
Returns
The index of the field, the output of numberOfFields() if not found.

Definition at line 377 of file StructArray.cpp.

◆ indicesFromRawIndex()

bool matioCpp::StructArray::indicesFromRawIndex ( size_t  rawIndex,
std::vector< index_type > &  el 
) const

Get the indices given the raw index.

Parameters
rawIndexThe input raw index from which to compute the indices
elThe output indices
Returns
True if successful, false otherwise (for example if rawIndex is out of bounds)

Definition at line 297 of file StructArray.cpp.

References std::endl(), std::vector< T >::resize(), and std::vector< T >::size().

◆ isFieldExisting()

bool matioCpp::StructArray::isFieldExisting ( const std::string field) const

Check if a field is existing It performs a linear search over the output of fields().

Returns
True if the field is existing, false otherwise.

Definition at line 372 of file StructArray.cpp.

◆ numberOfElements()

matioCpp::StructArray::index_type matioCpp::StructArray::numberOfElements ( ) const

Get the total number of elements in the array.

Returns
The total number of elements

Definition at line 340 of file StructArray.cpp.

◆ numberOfFields()

matioCpp::StructArray::index_type matioCpp::StructArray::numberOfFields ( ) const

Get the total number of fields in the struct.

Returns
The total number of fields

Definition at line 345 of file StructArray.cpp.

◆ operator()() [1/4]

matioCpp::StructArray::Element matioCpp::StructArray::operator() ( const std::vector< index_type > &  el)

Access specified element.

Parameters
elThe element to be accessed.
Warning
Each element of el has to be strictly smaller than the corresponding dimension.
Returns
A Struct with a weak ownership to the underlying mat variable. This means that the data can be changed, but the name cannot change, and no fields can be added.

Definition at line 441 of file StructArray.cpp.

◆ operator()() [2/4]

matioCpp::StructArray::ConstElement matioCpp::StructArray::operator() ( const std::vector< index_type > &  el) const

Access specified element.

Parameters
elThe element to be accessed.
Warning
Each element of el has to be strictly smaller than the corresponding dimension.
Returns
A const Struct with a weak ownership to the underlying mat variable.

Definition at line 447 of file StructArray.cpp.

◆ operator()() [3/4]

matioCpp::StructArray::Element matioCpp::StructArray::operator() ( index_type  el)

Access specified element.

Parameters
elThe element to be accessed (raw index).
Returns
A Struct with a weak ownership to the underlying mat variable. This means that the data can be changed, but the name cannot change, and no fields can be added.

Definition at line 453 of file StructArray.cpp.

◆ operator()() [4/4]

matioCpp::StructArray::ConstElement matioCpp::StructArray::operator() ( index_type  el) const

Access specified element.

Parameters
elThe element to be accessed (raw index).
Returns
A const Struct with a weak ownership to the underlying mat variable.

Definition at line 459 of file StructArray.cpp.

◆ operator=() [1/2]

matioCpp::StructArray & matioCpp::StructArray::operator= ( const StructArray other)

Assignement operator (copy) from another StructArray.

Parameters
otherThe other StructArray.
Note
Also the name is copied
Returns
A reference to this StructArray.

Definition at line 198 of file StructArray.cpp.

◆ operator=() [2/2]

matioCpp::StructArray & matioCpp::StructArray::operator= ( matioCpp::StructArray &&  other)

Assignement operator (move) from another StructArray.

Parameters
otherThe other StructArray.
Note
Also the name is copied
Returns
A reference to this StructArray.

Definition at line 204 of file StructArray.cpp.

◆ operator[]() [1/4]

matioCpp::StructArray::Element matioCpp::StructArray::operator[] ( const std::vector< index_type > &  el)

Access specified element.

Parameters
elThe element to be accessed.
Warning
Each element of el has to be strictly smaller than the corresponding dimension.
Returns
A Struct with a weak ownership to the underlying mat variable. This means that the data can be changed, but the name cannot change, and no fields can be added.

Definition at line 465 of file StructArray.cpp.

◆ operator[]() [2/4]

matioCpp::StructArray::ConstElement matioCpp::StructArray::operator[] ( const std::vector< index_type > &  el) const

Access specified element.

Parameters
elThe element to be accessed.
Warning
Each element of el has to be strictly smaller than the corresponding dimension.
Returns
A const Struct with a weak ownership to the underlying mat variable.

Definition at line 471 of file StructArray.cpp.

◆ operator[]() [3/4]

matioCpp::StructArray::Element matioCpp::StructArray::operator[] ( index_type  el)

Access specified element.

Parameters
elThe element to be accessed.
Returns
A Struct with a weak ownership to the underlying mat variable. This means that the data can be changed, but the name cannot change, and no fields can be added.

Definition at line 477 of file StructArray.cpp.

◆ operator[]() [4/4]

matioCpp::StructArray::ConstElement matioCpp::StructArray::operator[] ( index_type  el) const

Access specified element.

Parameters
elThe element to be accessed.
Returns
A const Struct with a weak ownership to the underlying mat variable.

Definition at line 483 of file StructArray.cpp.

◆ rawIndexFromIndices()

matioCpp::StructArray::index_type matioCpp::StructArray::rawIndexFromIndices ( const std::vector< index_type > &  el) const

Get the linear index corresponding to the provided indices.

Parameters
elThe desider element
Warning
It checks if the element is in the bounds only in debug mode.

Since the variables are stored internally in column-major, an element (i,j,k,l,..) of an array of dimensions (n,m,p,k,...) corresponds to a row index equal to i + j*n + k*n*m + l*n*m*p +...

Returns
the linear index corresponding to the provided indices

Definition at line 278 of file StructArray.cpp.

References std::vector< T >::size().

◆ resize()

void matioCpp::StructArray::resize ( const std::vector< index_type > &  newDimensions)

Resize the array.

Parameters
newDimensionsThe new dimensions.
Warning
Previous data is lost.

Definition at line 334 of file StructArray.cpp.

References std::move().

◆ setElement() [1/2]

bool matioCpp::StructArray::setElement ( const std::vector< index_type > &  el,
const matioCpp::Struct newValue 
)

Set the element at the specified position.

Parameters
elThe indices of the specified element
newValueThe Struct that will be copied in the specified location
Returns
True if successful, false otherwise (for example if the newValue is not valid)
Note
An assertion is thrown if el is out of bounds, but only in debug mode
The struct needs to have the same set of fields returned by fields()

Definition at line 403 of file StructArray.cpp.

◆ setElement() [2/2]

bool matioCpp::StructArray::setElement ( index_type  el,
const matioCpp::Struct newValue 
)

Set the element at the specified position.

Parameters
elThe raw index of the specified element
newValueThe Struct that will be copied in the specified location
Returns
True if successful, false otherwise (for example if the newValue is not valid)
Note
An assertion is thrown if el is out of bounds, but only in debug mode
The struct needs to have the same set of fields returned by fields()

Definition at line 408 of file StructArray.cpp.

References std::endl(), matioCpp::Variable::getStructFields(), and matioCpp::Struct::numberOfFields().

◆ setName()

bool matioCpp::StructArray::setName ( const std::string newName)

Change the name of the Variable.

Parameters
newNameThe new name
Returns
True if successful.

Definition at line 329 of file StructArray.cpp.

Friends And Related Function Documentation

◆ StructArrayElement

template<bool >
friend class StructArrayElement
friend

Definition at line 33 of file StructArray.h.


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