matio-cpp  v0.2.5
A C++ wrapper of the matio library, with memory ownership handling, to read and write .mat files.
Variable.h
Go to the documentation of this file.
1 #ifndef MATIOCPP_VARIABLE_H
2 #define MATIOCPP_VARIABLE_H
3 /*
4  * Copyright (C) 2020 Fondazione Istituto Italiano di Tecnologia
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-2-Clause license (https://opensource.org/licenses/BSD-2-Clause).
8  */
9 
10 
13 #include <matioCpp/Span.h>
14 #include <matioCpp/MatvarHandler.h>
15 #include <matioCpp/SharedMatvar.h>
16 #include <matioCpp/WeakMatvar.h>
17 
18 
23 {
24 
25  matioCpp::MatvarHandler* m_handler;
26 
27 protected:
28 
43 
54  bool initializeComplexVariable(const std::string& name, const VariableType& variableType, const ValueType& valueType, matioCpp::Span<const size_t> dimensions, void *realData, void *imaginaryData);
55 
56 
64  template<typename T>
65  bool initializeComplexVector(const std::string& name, const Span<T> realInputVector, const Span<T> imaginaryInputVector)
66  {
67  if (realInputVector.size() != imaginaryInputVector.size())
68  {
69  std::string errorPrefix = "[ERROR][matioCpp::Variable::createComplexVector] ";
70  if (name.empty())
71  {
72  std::cerr << errorPrefix << "The real and imaginary part have different size." << std::endl;
73  return false;
74  }
75  }
76  return initializeComplexVariable(name, VariableType::Vector, get_type<T>::valuetype, {realInputVector.size(), 1}, (void*)realInputVector.data(), (void*)imaginaryInputVector.data());
77  }
78 
84  bool changeName(const std::string& newName);
85 
90  size_t getArrayNumberOfElements() const;
91 
98  bool setCellElement(size_t linearIndex, const Variable& newValue);
99 
106  Variable getCellElement(size_t linearIndex);
107 
113  const Variable getCellElement(size_t linearIndex) const;
114 
119  size_t getStructNumberOfFields() const;
120 
125  char * const *getStructFields() const;
126 
132  size_t getStructFieldIndex(const std::string& field) const;
133 
141  bool setStructField(size_t index, const Variable& newValue, size_t structPositionInArray = 0);
142 
148  bool addStructField(const std::string& newField);
149 
159  bool setStructField(const std::string &field, const Variable& newValue, size_t structPositionInArray = 0);
160 
168  Variable getStructField(size_t index, size_t structPositionInArray = 0);
169 
176  const Variable getStructField(size_t index, size_t structPositionInArray = 0) const;
177 
185  Struct getStructArrayElement(size_t linearIndex);
186 
193  const Struct getStructArrayElement(size_t linearIndex) const;
194 
202  virtual bool checkCompatibility(const matvar_t* inputPtr, matioCpp::VariableType variableType, matioCpp::ValueType valueType) const;
203 
204 public:
205 
209  Variable();
210 
215  Variable(const matvar_t * inputVar);
216 
220  Variable(const Variable& other);
221 
225  Variable(Variable&& other);
226 
231  Variable(const MatvarHandler& handler);
232 
236  ~Variable();
237 
244  Variable& operator=(const Variable& other);
245 
252  Variable& operator=(Variable&& other);
253 
259  bool fromMatio(const matvar_t * inputVar);
260 
267  bool fromOther(const Variable& other);
268 
276  bool fromOther(Variable&& other);
277 
283  const matvar_t * toMatio() const;
284 
290  matvar_t * toMatio();
291 
296  std::string name() const;
297 
303 
309 
314  bool isComplex() const;
315 
321 
329  bool isValid() const;
330 
336  template<typename T>
338 
344  template<typename T>
345  const matioCpp::Element<T> asElement() const;
346 
352  template<typename T>
354 
360  template<typename T>
361  const matioCpp::Vector<T> asVector() const;
362 
367 
371  const matioCpp::String asString() const;
372 
377 
381  const matioCpp::String16 asString16() const;
382 
387 
391  const matioCpp::String32 asString32() const;
392 
398  template<typename T>
400 
406  template<typename T>
408 
413 
417  const matioCpp::CellArray asCellArray() const;
418 
423 
427  const matioCpp::Struct asStruct() const;
428 
433 
437  const matioCpp::StructArray asStructArray() const;
438 
439 };
440 
441 #endif
CellArray is a particular type of Variable specialized for cell arrays.
Definition: CellArray.h:18
Element allows having a 1x1 variable (like double, int,..) castable to a primitive type.
Definition: Element.h:20
MultiDimensionalArray is a particular type of Variable specialized for multidimensional arrays of a g...
MATIOCPP_CONSTEXPR index_type size() const noexcept
Definition: Span.h:526
MATIOCPP_CONSTEXPR pointer data() const noexcept
Definition: Span.h:550
StructArray is a particular type of Variable specialized for array of structs.
Definition: StructArray.h:21
Struct is a particular type of Variable specialized for structs.
Definition: Struct.h:18
The matioCpp::Variable class is the equivalent of matvar_t in matio.
Definition: Variable.h:23
bool setStructField(size_t index, const Variable &newValue, size_t structPositionInArray=0)
Set the field of the struct at the specified position.
Definition: Variable.cpp:254
matioCpp::String16 asString16()
Cast the variable as a String16.
Definition: Variable.cpp:587
Struct getStructArrayElement(size_t linearIndex)
Get an element of the variable, considered as a StructArray.
Definition: Variable.cpp:338
char *const * getStructFields() const
Get the list of fields in the variable, considered as a struct.
Definition: Variable.cpp:229
matioCpp::String32 asString32()
Cast the variable as a String32.
Definition: Variable.cpp:597
size_t getArrayNumberOfElements() const
Get the total number of elements in the array.
Definition: Variable.cpp:169
Variable & operator=(const Variable &other)
Copy assignement.
Definition: Variable.cpp:417
Variable()
Default constructor.
Definition: Variable.cpp:375
matioCpp::StructArray asStructArray()
Cast the variable as a StructArray.
Definition: Variable.cpp:567
bool setCellElement(size_t linearIndex, const Variable &newValue)
Set a cell element at a specified linear position.
Definition: Variable.cpp:180
size_t getStructNumberOfFields() const
Get the total number of fields in the variable, considered as a struct.
Definition: Variable.cpp:223
~Variable()
Destructor.
Definition: Variable.cpp:408
bool initializeComplexVector(const std::string &name, const Span< T > realInputVector, const Span< T > imaginaryInputVector)
Initialize a complex vector.
Definition: Variable.h:65
std::string name() const
Get the name of the Variable.
Definition: Variable.cpp:494
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.
Definition: Variable.cpp:370
bool addStructField(const std::string &newField)
Add a new field to the variable, considered as a struct.
Definition: Variable.cpp:282
const matvar_t * toMatio() const
Convert this Variable to a matio variable.
Definition: Variable.cpp:480
size_t getStructFieldIndex(const std::string &field) const
Get the index of the specified field in the variable, considered as a struct.
Definition: Variable.cpp:235
matioCpp::MultiDimensionalArray< T > asMultiDimensionalArray()
Cast the variable as a MultiDimensionalArray.
bool changeName(const std::string &newName)
Change the name of the variable.
Definition: Variable.cpp:150
bool fromMatio(const matvar_t *inputVar)
Set this variable from an existing matio variable.
Definition: Variable.cpp:433
matioCpp::Span< const size_t > dimensions() const
Get the dimensions of this object.
Definition: Variable.cpp:528
bool isValid() const
Check if the variable is valid.
Definition: Variable.cpp:542
matioCpp::VariableType variableType() const
Get the VariableType.
Definition: Variable.cpp:506
matioCpp::Element< T > asElement()
Cast the variable as a Element.
Definition: Element.tpp:176
bool fromOther(const Variable &other)
Set this variable from another variable.
Definition: Variable.cpp:453
bool initializeVariable(const std::string &name, const VariableType &variableType, const ValueType &valueType, matioCpp::Span< const size_t > dimensions, void *data)
Initialize the variable.
Definition: Variable.cpp:16
matioCpp::ValueType valueType() const
Get the ValueType.
Definition: Variable.cpp:511
Variable getCellElement(size_t linearIndex)
Get a cell element at a specified linear position.
Definition: Variable.cpp:211
Variable getStructField(size_t index, size_t structPositionInArray=0)
Get the specified field in the variable, considered as a struct.
Definition: Variable.cpp:326
bool isComplex() const
Get if the variable is complex.
Definition: Variable.cpp:516
matioCpp::Vector< T > asVector()
Cast the variable as a Vector.
Definition: Vector.tpp:404
matioCpp::CellArray asCellArray()
Cast the variable as a CellArray.
Definition: Variable.cpp:547
matioCpp::Struct asStruct()
Cast the variable as a Struct.
Definition: Variable.cpp:557
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.
Definition: Variable.cpp:75
matioCpp::String asString()
Cast the variable as a String.
Definition: Variable.cpp:577
Vector is a particular type of Variable specialized for 1-D arrays of a generic type T.
Definition: Vector.h:23
T empty(T... args)
T endl(T... args)
VariableType
Define the type of variable.
ValueType
The list of types for an element of a certain variable type.
Utility metafunction to get the ValueType from a given primitive type.