matio-cpp  v0.2.5
A C++ wrapper of the matio library, with memory ownership handling, to read and write .mat files.
Struct.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Fondazione Istituto Italiano di Tecnologia
3  *
4  * This software may be modified and distributed under the terms of the
5  * BSD-2-Clause license (https://opensource.org/licenses/BSD-2-Clause).
6  */
7 
8 
9 #include <matioCpp/Struct.h>
10 
11 bool matioCpp::Struct::checkCompatibility(const matvar_t* inputPtr, matioCpp::VariableType variableType, matioCpp::ValueType) const
12 {
13 
15  {
16  std::cerr << "[matioCpp::Struct::checkCompatibility] The variable type is not compatible with a struct." << std::endl;
17  return false;
18  }
19 
20  if (inputPtr->isComplex)
21  {
22  std::cerr << "[matioCpp::Struct::checkCompatibility] Cannot use a complex variable into a non-complex one." << std::endl;
23  return false;
24  }
25 
26  return true;
27 }
28 
30 {
31  size_t emptyDimensions[] = {1, 1};
32  initializeVariable("unnamed_struct",
34  matioCpp::ValueType::VARIABLE, emptyDimensions,
35  nullptr);
36 }
37 
39 {
40  size_t emptyDimensions[] = {1, 1};
41  initializeVariable(name,
43  matioCpp::ValueType::VARIABLE, emptyDimensions,
44  nullptr);
45 }
46 
48 {
49  size_t emptyDimensions[] = {1, 1};
50  std::vector<matvar_t*> vectorOfPointers;
51  for (size_t i = 0; i < elements.size(); ++i)
52  {
53  if (elements[i].isValid())
54  {
55  vectorOfPointers.push_back(matioCpp::MatvarHandler::GetMatvarDuplicate(elements[i].toMatio()));
56  }
57  else
58  {
59  std::cerr << "[ERROR][matioCpp::Struct::Struct] The element of " << name << " at index " << i << " (0-based) is not valid. It will be skipped." << std::endl;
60  }
61  }
62  vectorOfPointers.push_back(nullptr); //The vector of pointers has to be null terminated
63 
64  initializeVariable(name,
66  matioCpp::ValueType::VARIABLE, emptyDimensions,
67  vectorOfPointers.data());
68 }
69 
71 {
72  fromOther(other);
73 }
74 
76 {
77  fromOther(std::forward<Struct>(other));
78 }
79 
81  : matioCpp::Variable(handler)
82 {
83  if (!handler.get() || !checkCompatibility(handler.get(), handler.variableType(), handler.valueType()))
84  {
85  assert(false);
86  size_t emptyDimensions[] = {1, 1};
87  initializeVariable("unnamed_struct",
89  matioCpp::ValueType::VARIABLE, emptyDimensions,
90  nullptr);
91  }
92 }
93 
95 {
96 
97 }
98 
100 {
101  fromOther(other);
102  return *this;
103 }
104 
106 {
107  fromOther(std::forward<Struct>(other));
108  return *this;
109 }
110 
112 {
113  std::vector<matvar_t*> vectorOfPointers(elements.size() + 1, nullptr); //The vector of pointers has to be null terminated
114  for (size_t i = 0; i < elements.size(); ++i)
115  {
116  if (!elements[i].isValid())
117  {
118  std::cerr << "[ERROR][matioCpp::Struct::fromVectorOfVariables] The element at index "<< i << " (0-based) is not valid." << std::endl;
119  return false;
120  }
121  vectorOfPointers[i] = matioCpp::MatvarHandler::GetMatvarDuplicate(elements[i].toMatio());
122  }
123 
124  initializeVariable(name(),
126  matioCpp::ValueType::VARIABLE, dimensions(),
127  vectorOfPointers.data());
128  return true;
129 }
130 
132 {
133  return changeName(newName);
134 }
135 
137 {
138  return getStructNumberOfFields();
139 }
140 
142 {
144  char * const * matvarOutput = getStructFields();
145  if (matvarOutput)
146  {
147  size_t numberOfFields = getStructNumberOfFields();
148  output.reserve(numberOfFields);
149  for (size_t i = 0; i < numberOfFields; ++i)
150  {
151  output.emplace_back(matvarOutput[i]);
152  }
153  }
154 
155  return output;
156 }
157 
159 {
160  fromOther(std::move(Struct(name())));
161 }
162 
164 {
165  return getStructFieldIndex(field) < numberOfFields();
166 }
167 
169 {
170  return getStructFieldIndex(field);
171 }
172 
174 {
175  assert(index < numberOfFields() && "The specified index is out of bounds");
176  return setStructField(index, newValue);
177 }
178 
179 bool matioCpp::Struct::setField(const std::string &field, const matioCpp::Variable &newValue)
180 {
181  return setStructField(field, newValue);
182 }
183 
185 {
186  return setStructField(newValue.name(), newValue);
187 }
188 
190 {
191  assert(el < numberOfFields() && "The specified index is out of bounds");
192  return getStructField(el);
193 }
194 
196 {
197  assert(el < numberOfFields() && "The specified index is out of bounds");
198  return getStructField(el);
199 }
200 
202 {
203  size_t index = getFieldIndex(el);
204  assert(index < numberOfFields() && "The specified field does not exist.");
205  return getStructField(index);
206 }
207 
209 {
210  size_t index = getFieldIndex(el);
211  assert(index < numberOfFields() && "The specified field does not exist.");
212  return getStructField(index);
213 }
214 
216 {
217  assert(el < numberOfFields() && "The specified index is out of bounds");
218  return getStructField(el);
219 }
220 
222 {
223  assert(el < numberOfFields() && "The specified index is out of bounds");
224  return getStructField(el);
225 }
226 
228 {
229  size_t index = getFieldIndex(el);
230  assert(index < numberOfFields() && "The specified field does not exist.");
231  return getStructField(index);
232 }
233 
235 {
236  size_t index = getFieldIndex(el);
237  assert(index < numberOfFields() && "The specified field does not exist.");
238  return getStructField(index);
239 }
240 
241 
static matvar_t * GetMatvarDuplicate(const matvar_t *inputPtr)
Get a duplicate of the input matvar pointer/.
ValueType valueType() const
Get the value type of the pointer.
virtual matvar_t * get() const =0
Get the shared matvar_t pointer.
VariableType variableType() const
Get the variable type of the pointer.
Struct is a particular type of Variable specialized for structs.
Definition: Struct.h:18
size_t getFieldIndex(const std::string &field) const
Get the index of the specified field in the struct by performing a linear search.
Definition: Struct.cpp:168
bool setName(const std::string &newName)
Change the name of the Struct.
Definition: Struct.cpp:131
~Struct()
Destructor.
Definition: Struct.cpp:94
std::vector< std::string > fields() const
Get the list of fields.
Definition: Struct.cpp:141
bool fromVectorOfVariables(const std::vector< Variable > &elements)
Set from a vector of Variables.
Definition: Struct.cpp:111
matioCpp::Variable operator[](index_type el)
Access field at a specific index.
Definition: Struct.cpp:215
size_t index_type
Definition: Struct.h:33
Struct & operator=(const Struct &other)
Assignement operator (copy) from another Struct.
Definition: Struct.cpp:99
bool setField(index_type index, const Variable &newValue)
Set the field at the specified position.
Definition: Struct.cpp:173
void clear()
Clear the struct.
Definition: Struct.cpp:158
Struct()
The type used for indices.
Definition: Struct.cpp:29
bool isFieldExisting(const std::string &field) const
Check if a field is existing It performs a linear search over the output of fields().
Definition: Struct.cpp:163
index_type numberOfFields() const
Get the total number of fields in the struct.
Definition: Struct.cpp:136
matioCpp::Variable operator()(index_type el)
Access field at a specific index.
Definition: Struct.cpp:189
The matioCpp::Variable class is the equivalent of matvar_t in matio.
Definition: Variable.h:23
std::string name() const
Get the name of the Variable.
Definition: Variable.cpp:494
matioCpp::VariableType variableType() const
Get the VariableType.
Definition: Variable.cpp:506
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
T data(T... args)
T emplace_back(T... args)
T endl(T... args)
T move(T... args)
VariableType
Define the type of variable.
ValueType
The list of types for an element of a certain variable type.
T push_back(T... args)
T reserve(T... args)
T size(T... args)