matio-cpp  v0.2.6
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 
133  bool getStructFieldIndex(const std::string& field, size_t& index) const;
134 
142  bool setStructField(size_t index, const Variable& newValue, size_t structPositionInArray = 0);
143 
149  bool addStructField(const std::string& newField);
150 
160  bool setStructField(const std::string &field, const Variable& newValue, size_t structPositionInArray = 0);
161 
169  Variable getStructField(size_t index, size_t structPositionInArray = 0);
170 
177  const Variable getStructField(size_t index, size_t structPositionInArray = 0) const;
178 
186  Struct getStructArrayElement(size_t linearIndex);
187 
194  const Struct getStructArrayElement(size_t linearIndex) const;
195 
203  virtual bool checkCompatibility(const matvar_t* inputPtr, matioCpp::VariableType variableType, matioCpp::ValueType valueType) const;
204 
205 public:
206 
210  Variable();
211 
216  Variable(const matvar_t * inputVar);
217 
221  Variable(const Variable& other);
222 
226  Variable(Variable&& other);
227 
232  Variable(const MatvarHandler& handler);
233 
237  ~Variable();
238 
245  Variable& operator=(const Variable& other);
246 
253  Variable& operator=(Variable&& other);
254 
260  bool fromMatio(const matvar_t * inputVar);
261 
268  bool fromOther(const Variable& other);
269 
277  bool fromOther(Variable&& other);
278 
284  const matvar_t * toMatio() const;
285 
291  matvar_t * toMatio();
292 
297  std::string name() const;
298 
304 
310 
315  bool isComplex() const;
316 
322 
330  bool isValid() const;
331 
341 
350  const matioCpp::Variable operator[](const std::string& el) const;
351 
357  template<typename T>
359 
365  template<typename T>
366  const matioCpp::Element<T> asElement() const;
367 
373  template<typename T>
375 
381  template<typename T>
382  const matioCpp::Vector<T> asVector() const;
383 
388 
392  const matioCpp::String asString() const;
393 
398 
402  const matioCpp::String16 asString16() const;
403 
408 
412  const matioCpp::String32 asString32() const;
413 
419  template<typename T>
421 
427  template<typename T>
429 
434 
438  const matioCpp::CellArray asCellArray() const;
439 
444 
448  const matioCpp::Struct asStruct() const;
449 
454 
458  const matioCpp::StructArray asStructArray() const;
459 
460 };
461 
462 #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:257
matioCpp::String16 asString16()
Cast the variable as a String16.
Definition: Variable.cpp:627
Struct getStructArrayElement(size_t linearIndex)
Get an element of the variable, considered as a StructArray.
Definition: Variable.cpp:342
char *const * getStructFields() const
Get the list of fields in the variable, considered as a struct.
Definition: Variable.cpp:229
bool getStructFieldIndex(const std::string &field, size_t &index) const
Get the index of the specified field in the variable, considered as a struct.
Definition: Variable.cpp:235
matioCpp::String32 asString32()
Cast the variable as a String32.
Definition: Variable.cpp:637
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:421
Variable()
Default constructor.
Definition: Variable.cpp:379
matioCpp::StructArray asStructArray()
Cast the variable as a StructArray.
Definition: Variable.cpp:607
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:412
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:498
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:374
bool addStructField(const std::string &newField)
Add a new field to the variable, considered as a struct.
Definition: Variable.cpp:285
const matvar_t * toMatio() const
Convert this Variable to a matio variable.
Definition: Variable.cpp:484
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:437
matioCpp::Span< const size_t > dimensions() const
Get the dimensions of this object.
Definition: Variable.cpp:532
bool isValid() const
Check if the variable is valid.
Definition: Variable.cpp:546
matioCpp::VariableType variableType() const
Get the VariableType.
Definition: Variable.cpp:510
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:457
matioCpp::Variable operator[](const std::string &el)
Access field with specific name.
Definition: Variable.cpp:551
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:515
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:330
bool isComplex() const
Get if the variable is complex.
Definition: Variable.cpp:520
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:587
matioCpp::Struct asStruct()
Cast the variable as a Struct.
Definition: Variable.cpp:597
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:617
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.