matio-cpp  v0.2.5
A C++ wrapper of the matio library, with memory ownership handling, to read and write .mat files.
StructArrayElement.tpp
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 #ifndef MATIOCPP_STRUCTARRAYELEMENT_TPP
9 #define MATIOCPP_STRUCTARRAYELEMENT_TPP
10 
11 template <bool isConst>
13  : m_innerIndex(index)
14  , m_array(array)
15 {
16  assert(m_array);
17 }
18 
19 template <bool isConst>
21  : m_innerIndex(other.m_innerIndex)
22  , m_array(other.m_array)
23 {
24  assert(m_array);
25 }
26 
27 template <bool isConst>
28 template<bool B, typename >
30  : m_innerIndex(other.m_innerIndex)
31  , m_array(other.m_array)
32 {
33  static_assert (!B && isConst, "This constructor is meant to be used to obtain a const element from a non const one.");
34  assert(m_array);
35 }
36 
37 template <bool isConst>
39  : m_innerIndex(other.m_innerIndex)
40  , m_array(other.m_array)
41 {
42  assert(m_array);
43 }
44 
45 template <bool isConst>
46 template<bool B, typename >
48  : m_innerIndex(other.m_innerIndex)
49  , m_array(other.m_array)
50 {
51  static_assert (!B && isConst, "This constructor is meant to be used to obtain a const element from a non const one.");
52  assert(m_array);
53 }
54 
55 template <bool isConst>
57 {
58  m_innerIndex = other.m_innerIndex;
59  m_array = other.m_array;
60  assert(m_array);
61  return *this;
62 }
63 
64 template <bool isConst>
65 template<bool B, typename >
67 {
68  static_assert (!B && isConst, "This operator is meant to be used to obtain a const element from a non const one.");
69 
70  m_innerIndex = other.m_innerIndex;
71  m_array = other.m_array;
72  assert(m_array);
73  return *this;
74 }
75 
76 template <bool isConst>
77 template<bool B, typename >
79 {
80  static_assert (!B && isConst, "This operator is meant to be used to obtain a const element from a non const one.");
81 
82  bool ok = m_array->setElement(m_innerIndex, other);
83  assert(ok && "Failed to set the specified element to the input struct.");
84  matioCpp::unused(ok);
85 
86  return *this;
87 }
88 
89 template <bool isConst>
90 template<bool B, typename >
92 {
93  static_assert ((B == isConst) && !B, "This method can be used only if the the element is not const.");
94 
95  char * const * arrayFields = m_array->getStructFields();
96 
97  if (m_array->numberOfFields() != elements.size())
98  {
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;
100  return false;
101  }
102 
103  for (size_t i = 0; i < m_array->numberOfFields(); ++i)
104  {
105  if (strcmp(arrayFields[i], elements[i].name().c_str()) != 0)
106  {
107  std::cerr << "[ERROR][matioCpp::StructArrayElement::operator=] The name " << elements[i].name().c_str() << " of the input vector of variables at position "
108  << std::to_string(i) << " is supposed to be " << arrayFields[i]
109  << ". Cannot insert in a struct array a new field in a single element." <<std::endl;
110  return false;
111  }
112 
113  bool ok = m_array->setStructField(i, elements[i], m_innerIndex);
114  if (!ok)
115  {
116  std::cerr << "[ERROR][matioCpp::StructArrayElement::operator=] Failed to set field " << arrayFields[i] << "." <<std::endl;
117  return false;
118  }
119  }
120 
121  return true;
122 }
123 
124 template <bool isConst>
126 {
127  return m_array->numberOfFields();
128 }
129 
130 template <bool isConst>
132 {
133  return m_array->fields();
134 }
135 
136 template <bool isConst>
138 {
139  return m_array->isFieldExisting(field);
140 }
141 
142 template <bool isConst>
144 {
145  return m_array->getFieldIndex(field);
146 }
147 
148 template <bool isConst>
149 template<bool B, typename >
151 {
152  static_assert ((B == isConst) && !B, "This method can be used only if the the element is not const.");
153 
154  assert(index < numberOfFields() && "The index is out of bounds.");
155  if (!newValue.isValid())
156  {
157  std::cerr << "[ERROR][matioCpp::StructArrayElement::setField] The input variable is not valid." << std::endl;
158  return false;
159  }
160 
161  return m_array->setStructField(index, newValue, m_innerIndex);
162 }
163 
164 template <bool isConst>
165 template<bool B, typename >
167 {
168  static_assert ((B == isConst) && !B, "This method can be used only if the the element is not const.");
169 
170  size_t index = getFieldIndex(field);
171  if (index == numberOfFields())
172  {
173  std::cerr << "[ERROR][matioCpp::StructArrayElement::setField] No field named " << field << "." << std::endl;
174  return false;
175  }
176 
177  return setField(index, newValue);
178 }
179 
180 template <bool isConst>
181 template<bool B, typename >
183 {
184  static_assert ((B == isConst) && !B, "This method can be used only if the the element is not const.");
185 
186  return setField(newValue.name(), newValue);
187 }
188 
189 template <bool isConst>
191 {
192  return m_array->getStructArrayElement(m_innerIndex);
193 }
194 
195 template <bool isConst>
197 {
198  assert(el < numberOfFields() && "The specified index is out of bounds");
199  return m_array->getStructField(el, m_innerIndex);
200 }
201 
202 template <bool isConst>
204 {
205  size_t index = getFieldIndex(el);
206  assert(index < numberOfFields() && "The specified field does not exist.");
207  return m_array->getStructField(index, m_innerIndex);
208 }
209 
210 template <bool isConst>
212 {
213  assert(el < numberOfFields() && "The specified index is out of bounds");
214  return m_array->getStructField(el, m_innerIndex);
215 }
216 
217 template <bool isConst>
219 {
220  size_t index = getFieldIndex(el);
221  assert(index < numberOfFields() && "The specified field does not exist.");
222  return m_array->getStructField(index, m_innerIndex);
223 }
224 
225 #endif // MATIOCPP_STRUCTARRAYELEMENT_TPP
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.
Definition: Struct.h:18
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
bool isValid() const
Check if the variable is valid.
Definition: Variable.cpp:542
T endl(T... args)
void unused(Args &&...)
Utility metafunction to avoid compiler warnings about unused variables.
T size(T... args)
T to_string(T... args)