8 #define _CRT_NONSTDC_NO_DEPRECATE
18 std::string errorPrefix =
"[ERROR][matioCpp::Variable::createVar] ";
31 matio_types matioType;
32 matio_classes matioClass;
36 std::cerr << errorPrefix <<
"Either the variableType or the valueType are not supported." <<
std::endl;
47 flags = flags | matio_flags::MAT_F_LOGICAL;
50 matvar_t* newPtr = Mat_VarCreate(
name.
c_str(), matioClass, matioType,
static_cast<int>(dimensionsCopy.
size()), dimensionsCopy.
data(), data, flags);
66 if (!m_handler || !m_handler->
get())
77 std::string errorPrefix =
"[ERROR][matioCpp::Variable::createComplexVar] ";
84 if (dimensions.
size() < 2)
102 matio_types matioType;
103 matio_classes matioClass;
107 std::cerr << errorPrefix <<
"Either the variableType or the valueType are not supported." <<
std::endl;
111 mat_complex_split_t matioComplexSplit;
112 matioComplexSplit.Re = realData;
113 matioComplexSplit.Im = imaginaryData;
118 int flags = MAT_F_COMPLEX;
122 flags = flags | matio_flags::MAT_F_LOGICAL;
125 matvar_t* newPtr = Mat_VarCreate(name.
c_str(), matioClass, matioType,
static_cast<int>(dimensionsCopy.
size()), dimensionsCopy.
data(), &matioComplexSplit, flags);
129 if (!m_handler->importMatvar(newPtr))
141 if (!m_handler || !m_handler->get())
157 char* previousName = m_handler->get()->name;
164 m_handler->get()->name = strdup(newName.
c_str());
166 return (name() == newName);
171 size_t totalElements = 1;
172 for (
size_t dim : dimensions())
174 totalElements *= dim;
177 return totalElements;
184 std::cerr <<
"[ERROR][matioCpp::Variable::setCellElement] The variable is not valid." <<
std::endl;
189 if (!copiedNonOwning.
isValid())
191 std::cerr <<
"[ERROR][matioCpp::Variable::setCellElement] Could not copy the new value. ";
203 matvar_t* previousCell = Mat_VarSetCell(m_handler->get(),
static_cast<int>(linearIndex), copiedNonOwning.
toMatio());
205 m_handler->dropOwnedPointer(previousCell);
208 return Mat_VarGetCell(m_handler->get(),
static_cast<int>(linearIndex));
226 return Mat_VarGetNumberOfFields(m_handler->get());
232 return Mat_VarGetStructFieldnames(m_handler->get());
238 size_t numberOfFields = getStructNumberOfFields();
239 char *
const * fields = getStructFields();
243 return numberOfFields;
246 while (i < numberOfFields && (strcmp(fields[i], field.
c_str()) != 0))
258 std::cerr <<
"[ERROR][matioCpp::Variable::setStructField] The variable is not valid." <<
std::endl;
262 if (!m_handler->isShared())
264 std::cerr <<
"[ERROR][matioCpp::Variable::setStructField] Cannot set the field if the variable is not owning the memory." <<
std::endl;
269 if (!copiedNonOwning.
isValid())
274 matvar_t* previousField = Mat_VarSetStructFieldByIndex(m_handler->get(), index, structPositionInArray, copiedNonOwning.
toMatio());
276 m_handler->dropOwnedPointer(previousField);
279 return Mat_VarGetStructFieldByIndex(m_handler->get(), index, structPositionInArray);
286 std::cerr <<
"[ERROR][matioCpp::Variable::addStructField] The variable is not valid." <<
std::endl;
290 if (m_handler->isShared())
292 int err = Mat_VarAddStructField(m_handler->get(), newField.
c_str());
311 std::cerr <<
"[ERROR][matioCpp::Variable::setStructField] The variable is not valid." <<
std::endl;
315 size_t fieldindex = getStructFieldIndex(field);
317 if ((fieldindex == getStructNumberOfFields()) && !((getArrayNumberOfElements() == 1) && addStructField(field)))
323 return setStructField(fieldindex, newValue, structPositionInArray);
342 size_t numberOfFields = getStructNumberOfFields();
344 for (
size_t field = 0; field < numberOfFields; ++field)
346 fields[field] = Mat_VarGetStructFieldByIndex(m_handler->get(), field, linearIndex);
349 size_t dimensions[] = {1,1};
350 matvar_t* rawStruct = Mat_VarCreate(newName.
c_str(), matio_classes::MAT_C_STRUCT, matio_types::MAT_T_STRUCT, 2, dimensions, fields.
data(), MAT_F_DONT_COPY_DATA);
358 size_t numberOfFields = getStructNumberOfFields();
360 for (
size_t field = 0; field < numberOfFields; ++field)
362 fields[field] = Mat_VarGetStructFieldByIndex(m_handler->get(), field, linearIndex);
365 size_t dimensions[] = {1,1};
366 matvar_t* rawStruct = Mat_VarCreate(newName.
c_str(), matio_classes::MAT_C_STRUCT, matio_types::MAT_T_STRUCT, 2, dimensions, fields.
data(), MAT_F_DONT_COPY_DATA);
398 m_handler = other.m_handler;
399 other.m_handler =
nullptr;
403 : m_handler(handler.pointerToDuplicate())
419 bool ok = fromOther(other);
427 bool ok = fromOther(std::forward<matioCpp::Variable>(other));
437 std::cerr <<
"[matioCpp::Variable::fromMatio] The input pointer is null." <<
std::endl;
445 if (!checkCompatibility(inputVar, outputVariableType, outputValueType))
450 return m_handler->duplicateMatvar(inputVar);
455 return fromMatio(other.
toMatio());
460 if (!other.isValid())
462 std::cerr <<
"[matioCpp::Variable::fromOther] The input variable is not valid." <<
std::endl;
466 if (!checkCompatibility(other.toMatio(), other.variableType(), other.valueType()))
475 m_handler = other.m_handler;
476 other.m_handler =
nullptr;
484 return m_handler->get();
491 return m_handler->get();
498 return m_handler->get()->name;
508 return m_handler->variableType();
513 return m_handler->valueType();
520 return m_handler->get()->isComplex;
544 return m_handler->get() && checkCompatibility(m_handler->get(), m_handler->variableType(), m_handler->valueType());
CellArray is a particular type of Variable specialized for cell arrays.
virtual bool duplicateMatvar(const matvar_t *inputPtr)=0
Perform a deep copy of the input pointer.
static matvar_t * GetMatvarDuplicate(const matvar_t *inputPtr)
Get a duplicate of the input matvar pointer/.
virtual bool importMatvar(matvar_t *inputPtr)=0
Import the input pointer.
static void DeleteMatvar(matvar_t *pointerToDelete, DeleteMode mode=DeleteMode::Delete)
Delete the specified Matvar.
virtual matvar_t * get() const =0
Get the shared matvar_t pointer.
MATIOCPP_CONSTEXPR iterator begin() const noexcept
MATIOCPP_CONSTEXPR index_type size() const noexcept
MATIOCPP_CONSTEXPR iterator end() const noexcept
StructArray is a particular type of Variable specialized for array of structs.
Struct is a particular type of Variable specialized for structs.
The matioCpp::Variable class is the equivalent of matvar_t in matio.
bool setStructField(size_t index, const Variable &newValue, size_t structPositionInArray=0)
Set the field of the struct at the specified position.
matioCpp::String16 asString16()
Cast the variable as a String16.
Struct getStructArrayElement(size_t linearIndex)
Get an element of the variable, considered as a StructArray.
char *const * getStructFields() const
Get the list of fields in the variable, considered as a struct.
matioCpp::String32 asString32()
Cast the variable as a String32.
size_t getArrayNumberOfElements() const
Get the total number of elements in the array.
Variable & operator=(const Variable &other)
Copy assignement.
Variable()
Default constructor.
matioCpp::StructArray asStructArray()
Cast the variable as a StructArray.
bool setCellElement(size_t linearIndex, const Variable &newValue)
Set a cell element at a specified linear position.
size_t getStructNumberOfFields() const
Get the total number of fields in the variable, considered as a struct.
std::string name() const
Get the name of the Variable.
virtual bool checkCompatibility(const matvar_t *inputPtr, matioCpp::VariableType variableType, matioCpp::ValueType valueType) const
Check if an input matio pointer is compatible with the specified variable.
bool addStructField(const std::string &newField)
Add a new field to the variable, considered as a struct.
const matvar_t * toMatio() const
Convert this Variable to a matio variable.
size_t getStructFieldIndex(const std::string &field) const
Get the index of the specified field in the variable, considered as a struct.
bool changeName(const std::string &newName)
Change the name of the variable.
bool fromMatio(const matvar_t *inputVar)
Set this variable from an existing matio variable.
matioCpp::Span< const size_t > dimensions() const
Get the dimensions of this object.
bool isValid() const
Check if the variable is valid.
matioCpp::VariableType variableType() const
Get the VariableType.
bool fromOther(const Variable &other)
Set this variable from another variable.
bool initializeVariable(const std::string &name, const VariableType &variableType, const ValueType &valueType, matioCpp::Span< const size_t > dimensions, void *data)
Initialize the variable.
matioCpp::ValueType valueType() const
Get the ValueType.
Variable getCellElement(size_t linearIndex)
Get a cell element at a specified linear position.
Variable getStructField(size_t index, size_t structPositionInArray=0)
Get the specified field in the variable, considered as a struct.
bool isComplex() const
Get if the variable is complex.
matioCpp::CellArray asCellArray()
Cast the variable as a CellArray.
matioCpp::Struct asStruct()
Cast the variable as a Struct.
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.
matioCpp::String asString()
Cast the variable as a String.
Vector is a particular type of Variable specialized for 1-D arrays of a generic type T.
MATIOCPP_CONSTEXPR Span< ElementType > make_span(ElementType *ptr, typename Span< ElementType >::index_type count)
@ Delete
The handler deletes the pointer but not the data.
bool get_matio_types(const VariableType &inputVariableType, const ValueType &inputValueType, matio_classes &outputMatioClasses, matio_types &outputMatioType)
Get both the matio type and class from the input VariableType and ValueType.
void unused(Args &&...)
Utility metafunction to avoid compiler warnings about unused variables.
VariableType
Define the type of variable.
bool get_types_from_matvart(const matvar_t *input, VariableType &outputVariableType, ValueType &outputValueType)
Get the VariableType and the ValueType from a matvar_t pointer.
ValueType
The list of types for an element of a certain variable type.