-
Notifications
You must be signed in to change notification settings - Fork 9
/
Struct.cpp
238 lines (199 loc) · 6.99 KB
/
Struct.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Copyright (C) 2020 Fondazione Istituto Italiano di Tecnologia
*
* This software may be modified and distributed under the terms of the
* BSD-2-Clause license (https://opensource.org/licenses/BSD-2-Clause).
*/
#include <matioCpp/Struct.h>
bool matioCpp::Struct::checkCompatibility(const matvar_t* inputPtr, matioCpp::VariableType variableType, matioCpp::ValueType) const
{
if (variableType != matioCpp::VariableType::Struct)
{
std::cerr << "[matioCpp::Struct::checkCompatibility] The variable type is not compatible with a struct." << std::endl;
return false;
}
if (inputPtr->isComplex)
{
std::cerr << "[matioCpp::Struct::checkCompatibility] Cannot use a complex variable into a non-complex one." << std::endl;
return false;
}
return true;
}
matioCpp::Struct::Struct()
{
size_t emptyDimensions[] = {1, 1};
initializeVariable("unnamed_struct",
VariableType::Struct,
matioCpp::ValueType::VARIABLE, emptyDimensions,
nullptr);
}
matioCpp::Struct::Struct(const std::string &name)
{
size_t emptyDimensions[] = {1, 1};
initializeVariable(name,
VariableType::Struct,
matioCpp::ValueType::VARIABLE, emptyDimensions,
nullptr);
}
matioCpp::Struct::Struct(const std::string &name, const std::vector<Variable> &elements)
{
size_t emptyDimensions[] = {1, 1};
std::vector<matvar_t*> vectorOfPointers(elements.size() + 1, nullptr); //The vector of pointers has to be null terminated
for (size_t i = 0; i < elements.size(); ++i)
{
if (!elements[i].isValid())
{
std::cerr << "[ERROR][matioCpp::Struct::Struct] The element at index "<< i << " (0-based) is not valid." << std::endl;
assert(false);
}
vectorOfPointers[i] = matioCpp::MatvarHandler::GetMatvarDuplicate(elements[i].toMatio());
}
initializeVariable(name,
VariableType::Struct,
matioCpp::ValueType::VARIABLE, emptyDimensions,
vectorOfPointers.data());
}
matioCpp::Struct::Struct(const matioCpp::Struct &other)
{
fromOther(other);
}
matioCpp::Struct::Struct(matioCpp::Struct &&other)
{
fromOther(std::forward<Struct>(other));
}
matioCpp::Struct::Struct(const MatvarHandler &handler)
: matioCpp::Variable(handler)
{
if (!handler.get() || !checkCompatibility(handler.get(), handler.variableType(), handler.valueType()))
{
assert(false);
size_t emptyDimensions[] = {1, 1};
initializeVariable("unnamed_struct",
VariableType::Struct,
matioCpp::ValueType::VARIABLE, emptyDimensions,
nullptr);
}
}
matioCpp::Struct::~Struct()
{
}
matioCpp::Struct &matioCpp::Struct::operator=(const matioCpp::Struct &other)
{
fromOther(other);
return *this;
}
matioCpp::Struct &matioCpp::Struct::operator=(matioCpp::Struct &&other)
{
fromOther(std::forward<Struct>(other));
return *this;
}
bool matioCpp::Struct::fromVectorOfVariables(const std::vector<Variable> &elements)
{
std::vector<matvar_t*> vectorOfPointers(elements.size() + 1, nullptr); //The vector of pointers has to be null terminated
for (size_t i = 0; i < elements.size(); ++i)
{
if (!elements[i].isValid())
{
std::cerr << "[ERROR][matioCpp::Struct::fromVectorOfVariables] The element at index "<< i << " (0-based) is not valid." << std::endl;
return false;
}
vectorOfPointers[i] = matioCpp::MatvarHandler::GetMatvarDuplicate(elements[i].toMatio());
}
initializeVariable(name(),
VariableType::Struct,
matioCpp::ValueType::VARIABLE, dimensions(),
vectorOfPointers.data());
return true;
}
bool matioCpp::Struct::setName(const std::string &newName)
{
return changeName(newName);
}
matioCpp::Struct::index_type matioCpp::Struct::numberOfFields() const
{
return getStructNumberOfFields();
}
std::vector<std::string> matioCpp::Struct::fields() const
{
std::vector<std::string> output;
char * const * matvarOutput = getStructFields();
if (matvarOutput)
{
size_t numberOfFields = getStructNumberOfFields();
output.reserve(numberOfFields);
for (size_t i = 0; i < numberOfFields; ++i)
{
output.emplace_back(matvarOutput[i]);
}
}
return output;
}
void matioCpp::Struct::clear()
{
fromOther(std::move(Struct(name())));
}
bool matioCpp::Struct::isFieldExisting(const std::string &field) const
{
return getStructFieldIndex(field) < numberOfFields();
}
size_t matioCpp::Struct::getFieldIndex(const std::string &field) const
{
return getStructFieldIndex(field);
}
bool matioCpp::Struct::setField(matioCpp::Struct::index_type index, const matioCpp::Variable &newValue)
{
assert(index < numberOfFields() && "The specified index is out of bounds");
return setStructField(index, newValue);
}
bool matioCpp::Struct::setField(const std::string &field, const matioCpp::Variable &newValue)
{
return setStructField(field, newValue);
}
bool matioCpp::Struct::setField(const matioCpp::Variable &newValue)
{
return setStructField(newValue.name(), newValue);
}
matioCpp::Variable matioCpp::Struct::operator()(matioCpp::Struct::index_type el)
{
assert(el < numberOfFields() && "The specified index is out of bounds");
return getStructField(el);
}
const matioCpp::Variable matioCpp::Struct::operator()(matioCpp::Struct::index_type el) const
{
assert(el < numberOfFields() && "The specified index is out of bounds");
return getStructField(el);
}
matioCpp::Variable matioCpp::Struct::operator()(const std::string &el)
{
size_t index = getFieldIndex(el);
assert(index < numberOfFields() && "The specified field does not exist.");
return getStructField(index);
}
const matioCpp::Variable matioCpp::Struct::operator()(const std::string &el) const
{
size_t index = getFieldIndex(el);
assert(index < numberOfFields() && "The specified field does not exist.");
return getStructField(index);
}
matioCpp::Variable matioCpp::Struct::operator[](matioCpp::Struct::index_type el)
{
assert(el < numberOfFields() && "The specified index is out of bounds");
return getStructField(el);
}
const matioCpp::Variable matioCpp::Struct::operator[](matioCpp::Struct::index_type el) const
{
assert(el < numberOfFields() && "The specified index is out of bounds");
return getStructField(el);
}
matioCpp::Variable matioCpp::Struct::operator[](const std::string &el)
{
size_t index = getFieldIndex(el);
assert(index < numberOfFields() && "The specified field does not exist.");
return getStructField(index);
}
const matioCpp::Variable matioCpp::Struct::operator[](const std::string &el) const
{
size_t index = getFieldIndex(el);
assert(index < numberOfFields() && "The specified field does not exist.");
return getStructField(index);
}