8 #ifndef MATIOCPP_STRUCTARRAYELEMENT_TPP
9 #define MATIOCPP_STRUCTARRAYELEMENT_TPP
11 template <
bool isConst>
19 template <
bool isConst>
21 : m_innerIndex(other.m_innerIndex)
22 , m_array(other.m_array)
27 template <
bool isConst>
28 template<
bool B,
typename >
30 : m_innerIndex(other.m_innerIndex)
31 , m_array(other.m_array)
33 static_assert (!B && isConst,
"This constructor is meant to be used to obtain a const element from a non const one.");
37 template <
bool isConst>
39 : m_innerIndex(other.m_innerIndex)
40 , m_array(other.m_array)
45 template <
bool isConst>
46 template<
bool B,
typename >
48 : m_innerIndex(other.m_innerIndex)
49 , m_array(other.m_array)
51 static_assert (!B && isConst,
"This constructor is meant to be used to obtain a const element from a non const one.");
55 template <
bool isConst>
58 m_innerIndex = other.m_innerIndex;
59 m_array = other.m_array;
64 template <
bool isConst>
65 template<
bool B,
typename >
68 static_assert (!B && isConst,
"This operator is meant to be used to obtain a const element from a non const one.");
70 m_innerIndex = other.m_innerIndex;
71 m_array = other.m_array;
76 template <
bool isConst>
77 template<
bool B,
typename >
80 static_assert (!B && isConst,
"This operator is meant to be used to obtain a const element from a non const one.");
82 bool ok = m_array->setElement(m_innerIndex, other);
83 assert(ok &&
"Failed to set the specified element to the input struct.");
89 template <
bool isConst>
90 template<
bool B,
typename >
93 static_assert ((B == isConst) && !B,
"This method can be used only if the the element is not const.");
95 char *
const * arrayFields = m_array->getStructFields();
97 if (m_array->numberOfFields() != elements.
size())
99 std::cerr <<
"[ERROR][matioCpp::StructArrayElement::fromVectorOfVariables] The input vector is supposed to have size equal to the number of fields of the struct array." <<
std::endl;
103 for (
size_t i = 0; i < m_array->numberOfFields(); ++i)
105 if (strcmp(arrayFields[i], elements[i].name().c_str()) != 0)
107 std::cerr <<
"[ERROR][matioCpp::StructArrayElement::operator=] The name " << elements[i].name().c_str() <<
" of the input vector of variables at position "
109 <<
". Cannot insert in a struct array a new field in a single element." <<
std::endl;
113 bool ok = m_array->setStructField(i, elements[i], m_innerIndex);
116 std::cerr <<
"[ERROR][matioCpp::StructArrayElement::operator=] Failed to set field " << arrayFields[i] <<
"." <<
std::endl;
124 template <
bool isConst>
127 return m_array->numberOfFields();
130 template <
bool isConst>
133 return m_array->fields();
136 template <
bool isConst>
139 return m_array->isFieldExisting(field);
142 template <
bool isConst>
145 return m_array->getFieldIndex(field);
148 template <
bool isConst>
149 template<
bool B,
typename >
152 static_assert ((B == isConst) && !B,
"This method can be used only if the the element is not const.");
154 assert(index < numberOfFields() &&
"The index is out of bounds.");
157 std::cerr <<
"[ERROR][matioCpp::StructArrayElement::setField] The input variable is not valid." <<
std::endl;
161 return m_array->setStructField(index, newValue, m_innerIndex);
164 template <
bool isConst>
165 template<
bool B,
typename >
168 static_assert ((B == isConst) && !B,
"This method can be used only if the the element is not const.");
170 size_t index = getFieldIndex(field);
171 if (index == numberOfFields())
173 std::cerr <<
"[ERROR][matioCpp::StructArrayElement::setField] No field named " << field <<
"." <<
std::endl;
177 return setField(index, newValue);
180 template <
bool isConst>
181 template<
bool B,
typename >
184 static_assert ((B == isConst) && !B,
"This method can be used only if the the element is not const.");
186 return setField(newValue.
name(), newValue);
189 template <
bool isConst>
192 return m_array->getStructArrayElement(m_innerIndex);
195 template <
bool isConst>
198 assert(el < numberOfFields() &&
"The specified index is out of bounds");
199 return m_array->getStructField(el, m_innerIndex);
202 template <
bool isConst>
205 size_t index = getFieldIndex(el);
206 assert(index < numberOfFields() &&
"The specified field does not exist.");
207 return m_array->getStructField(index, m_innerIndex);
210 template <
bool isConst>
213 assert(el < numberOfFields() &&
"The specified index is out of bounds");
214 return m_array->getStructField(el, m_innerIndex);
217 template <
bool isConst>
220 size_t index = getFieldIndex(el);
221 assert(index < numberOfFields() &&
"The specified field does not exist.");
222 return m_array->getStructField(index, m_innerIndex);
output_variable_type operator()(index_type el) const
Access field at a specific index.
index_type numberOfFields() const
Get the total number of fields in the struct.
bool isFieldExisting(const std::string &field) const
Check if a field is existing It performs a linear search over the output of fields().
bool fromVectorOfVariables(const std::vector< Variable > &elements) const
Set from a vector of Variables.
output_struct_type asStruct() const
Get the element as a Struct.
std::conditional_t< isConst, const matioCpp::Struct, matioCpp::Struct > output_struct_type
bool setField(index_type index, const Variable &newValue) const
Set the field at the specified position.
output_variable_type operator[](index_type el) const
Access field at a specific index.
StructArrayElement & operator=(const StructArrayElement &other)=delete
Deleted copy assignement since it is not clear whether the index or the content have to be copied.
StructArrayElement()=delete
Deleted default constructor.
std::conditional_t< isConst, const matioCpp::StructArray, matioCpp::StructArray > input_vector_type
The type used for indices.
std::vector< std::string > fields() const
Get the list of fields.
std::conditional_t< isConst, const matioCpp::Variable, matioCpp::Variable > output_variable_type
size_t getFieldIndex(const std::string &field) const
Get the index of the specified field in the struct by performing a linear search.
Struct is a particular type of Variable specialized for structs.
The matioCpp::Variable class is the equivalent of matvar_t in matio.
std::string name() const
Get the name of the Variable.
bool isValid() const
Check if the variable is valid.
void unused(Args &&...)
Utility metafunction to avoid compiler warnings about unused variables.