matio-cpp  v0.2.5
A C++ wrapper of the matio library, with memory ownership handling, to read and write .mat files.
Vector.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_VECTOR_TPP
9 #define MATIOCPP_VECTOR_TPP
10 
11 template<typename T>
12 bool matioCpp::Vector<T>::initializeVector(const std::string& name, Span<const typename matioCpp::Vector<T>::element_type> inputVector)
13 {
14  size_t dimensions[] = {1, static_cast<size_t>(inputVector.size())};
15  return initializeVariable(name, VariableType::Vector, matioCpp::get_type<T>::valueType(), dimensions, (void*)inputVector.data());
16 }
17 
18 template<typename T>
19 bool matioCpp::Vector<T>::checkCompatibility(const matvar_t* inputPtr, matioCpp::VariableType variableType, matioCpp::ValueType valueType) const
20 {
21 
22  if ((variableType != matioCpp::VariableType::Vector) &&
23  (variableType != matioCpp::VariableType::Element))
24  {
25  std::cerr << "[matioCpp::Vector::checkCompatibility] The variable type is not compatible with a vector." << std::endl;
26  return false;
27  }
28 
29  if (inputPtr->isComplex)
30  {
31  std::cerr << "[matioCpp::Vector::checkCompatibility] Cannot use a complex variable into a non-complex one." << std::endl;
32  return false;
33  }
34 
35  if (!matioCpp::is_convertible_to_primitive_type<T>(valueType))
36  {
37  std::string dataType = "";
38  std::string classType = "";
39 
40  get_types_names_from_matvart(inputPtr, classType, dataType);
41 
42  std::cerr << "[matioCpp::Vector::checkCompatibility] The value type is not convertible to " <<
43  get_type<T>::toString() <<"." << std::endl <<
44  " Input class type: " << classType << std::endl <<
45  " Input data type: " << dataType << std::endl;
46  return false;
47  }
48  return true;
49 }
50 
51 template<typename T>
53 {
55 
56  if (std::is_same<T, char>::value) //If the type is char, matio may use strlen
57  {
58  empty.push_back('\0');
59 
60  size_t dimensions[] = {1, 0};
61  initializeVariable("unnamed_vector", VariableType::Vector, matioCpp::get_type<T>::valueType(), dimensions, (void*)empty.data());
62  }
63  else
64  {
65  initializeVector("unnamed_vector", matioCpp::make_span(empty));
66  }
67 }
68 
69 template<typename T>
71 {
73 
74  if (std::is_same<T, char>::value) //If the type is char, the name corresponds to the content
75  {
76  for (size_t i = 0; i < name.size(); ++i)
77  {
78  empty.push_back(name[i]);
79  }
80  empty.push_back('\0');
81 
82  size_t dimensions[] = {1, static_cast<size_t>(name.size())};
83  initializeVariable(name, VariableType::Vector, matioCpp::get_type<T>::valueType(), dimensions, (void*)empty.data());
84  }
85  else
86  {
87  initializeVector(name, matioCpp::make_span(empty));
88  }
89 }
90 
91 template<typename T>
93 {
95 
96  if (std::is_same<T, char>::value) //If the type is char, matio may use strlen
97  {
98  empty.push_back('\0');
99 
100  size_t dimensionsVec[] = {1, dimensions};
101  initializeVariable(name, VariableType::Vector, matioCpp::get_type<T>::valueType(), dimensionsVec, (void*)empty.data());
102  }
103  else
104  {
105  initializeVector(name, matioCpp::make_span(empty));
106  }
107 }
108 
109 template<typename T>
110 template<typename, typename>
112 {
113  initializeVector(name, inputVector);
114 }
115 
116 template <typename T>
118 {
122  "The assignement operator from a string is available only if the type of the vector is char, char16_t, char32_t, uint8_t, uint16_t or uint32_t.");
123  size_t dimensions[] = {1, static_cast<size_t>(inputString.size())};
124  initializeVariable(name, VariableType::Vector, matioCpp::get_type<T>::valueType(), dimensions, (void*)inputString.c_str());
125 }
126 
127 template <typename T>
129 {
130  static_assert (std::is_same<T, matioCpp::Logical>::value,"The assignement operator from a vector of bool is available only if the type of the vector is Logical");
132  initializeVector(name, matioCpp::make_span(empty));
133  this->operator=(inputVector);
134 }
135 
136 template<typename T>
138 {
139  fromOther(other);
140 }
141 
142 template<typename T>
144 {
145  fromOther(std::forward<matioCpp::Vector<T>>(other));
146 }
147 
148 template<typename T>
150  : matioCpp::Variable(handler)
151 {
152  if (!handler.get() || !checkCompatibility(handler.get(), handler.variableType(), handler.valueType()))
153  {
154  assert(false);
156  initializeVector("unnamed_vector", matioCpp::make_span(empty));
157  }
158 }
159 
160 template<typename T>
162 {
163 
164 }
165 
166 template<typename T>
168 {
169  fromOther(other);
170  return *this;
171 }
172 
173 template<typename T>
175 {
176  fromOther(std::forward<matioCpp::Vector<T>>(other));
177  return *this;
178 }
179 
180 template<typename T>
182 {
183  if (size() == other.size())
184  {
185  memcpy(toMatio()->data, other.data(), size() * sizeof(typename matioCpp::Vector<T>::element_type));
186  }
187  else
188  {
189  bool ok = initializeVector(name(), other);
190  assert(ok && "Failed to resize.");
191  }
192 
193  return *this;
194 }
195 
196 template<typename T>
198 {
202  "The assignement operator from a string is available only if the type of the vector is char, char16_t, char32_t, uint8_t, uint16_t or uint32_t.");
203  if (size() == other.size())
204  {
205  memcpy(toMatio()->data, other.data(), size() * sizeof(T));
206  }
207  else
208  {
209  size_t dimensions[] = {1, static_cast<size_t>(other.size())};
210  initializeVariable(name(), VariableType::Vector, matioCpp::get_type<T>::valueType(), dimensions, (void*)other.c_str());
211  }
212 
213  return *this;
214 
215 }
216 
217 template<typename T>
219 {
220  static_assert (std::is_same<T, matioCpp::Logical>::value,"The assignement operator from a vector of bool is available only if the type of the vector is Logical");
221  if (size() != other.size())
222  {
224  bool ok = initializeVector(name(), matioCpp::make_span(empty));
225  if (!ok)
226  {
227  assert(false && "Failed to resize.");
228  return *this;
229  }
230  }
231 
232  for (size_t i = 0; i < size(); ++i)
233  {
234  this->operator()(i) = other[i];
235  }
236 
237  return *this;
238 
239 }
240 
241 template<typename T>
243 {
244  return matioCpp::make_span(*this);
245 }
246 
247 template<typename T>
249 {
250  return matioCpp::make_span(*this);
251 }
252 
253 template<typename T>
255 {
256  return changeName(newName);
257 }
258 
259 template<typename T>
261 {
262  //A vector should have the size of dimensions equal to 2
263  assert(this->dimensions().size() == 2);
264 
265  return std::min(this->dimensions()[0], this->dimensions()[1]) > 0 ? std::max(this->dimensions()[0], this->dimensions()[1]) : 0;
266 }
267 
268 template<typename T>
270 {
271  if (newSize != size())
272  {
274  memcpy(newVector.data(), data(), std::min(newSize, size()) * sizeof(typename matioCpp::Vector<T>::element_type));
275  this->operator=(newVector);
276  }
277 }
278 
279 template<typename T>
281 {
282  fromOther(std::move(Vector<T>(name())));
283 }
284 
285 template<typename T>
287 {
288  return static_cast<typename matioCpp::Vector<T>::pointer>(toMatio()->data);
289 }
290 
291 template<typename T>
293 {
294  return static_cast<typename matioCpp::Vector<T>::const_pointer>(toMatio()->data);
295 }
296 
297 template<typename T>
299 {
300  return toSpan()(el);
301 }
302 
303 template<typename T>
305 {
306  return toSpan()(el);
307 }
308 
309 template<typename T>
311 {
315  "The operator () to convert to a string is available only if the type of the vector is a char type or uint type.");
317 }
318 
319 template<typename T>
321 {
322  return this->operator()(el);
323 }
324 
325 template<typename T>
327 {
328  return this->operator()(el);
329 }
330 
331 template<typename T>
333 {
334  return iterator(this, 0);
335 }
336 
337 template<typename T>
339 {
340  return iterator(this, size());
341 }
342 
343 template<typename T>
345 {
346  return const_iterator(this, 0);
347 }
348 
349 template<typename T>
351 {
352  return const_iterator(this, size());
353 }
354 
355 template<typename T>
357 {
358  return const_iterator(this, 0);
359 }
360 
361 template<typename T>
363 {
364  return const_iterator(this, size());
365 }
366 
367 template<typename T>
369 {
370  return reverse_iterator{end()};
371 }
372 
373 template<typename T>
375 {
376  return reverse_iterator{begin()};
377 }
378 
379 template<typename T>
381 {
382  return const_reverse_iterator{cend()};
383 }
384 
385 template<typename T>
387 {
388  return const_reverse_iterator{cbegin()};
389 }
390 
391 template<typename T>
393 {
394  return const_reverse_iterator{cend()};
395 }
396 
397 template<typename T>
399 {
400  return const_reverse_iterator{cbegin()};
401 }
402 
403 template<typename T>
405 {
406  return matioCpp::Vector<T>(*m_handler);
407 }
408 
409 template<typename T>
411 {
412  return matioCpp::Vector<T>(*m_handler);
413 }
414 
415 #endif // MATIOCPP_VECTOR_TPP
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.
The matioCpp::Variable class is the equivalent of matvar_t in matio.
Definition: Variable.h:23
matioCpp::Vector< T > asVector()
Cast the variable as a Vector.
Definition: Vector.tpp:404
Vector is a particular type of Variable specialized for 1-D arrays of a generic type T.
Definition: Vector.h:23
matioCpp::Span< element_type > toSpan()
Get this Vector as a Span.
Definition: Vector.tpp:242
size_t index_type
Defines how to allocate T.
Definition: Vector.h:34
pointer data()
Direct access to the underlying array.
Definition: Vector.tpp:286
index_type size() const
Get the size of the vector.
Definition: Vector.tpp:260
Vector< T > & operator=(const Vector< T > &other)
Assignement operator (copy) from another Vector.
Definition: Vector.tpp:167
reverse_iterator rend()
rend Iterator to the element following the last element of the reversed vector.
Definition: Vector.tpp:374
const_reverse_iterator crend() const
crend Iterator to the element following the last element of the reversed vector.
Definition: Vector.tpp:398
std::remove_cv_t< element_type > value_type
Defines the type of an element of the Vector.
Definition: Vector.h:30
element_type & reference
The type used for indices.
Definition: Vector.h:36
typename get_type< T >::type element_type
Defines the type specified in the template.
Definition: Vector.h:28
typename std::allocator_traits< std::allocator< element_type > >::const_pointer const_pointer
The pointer type.
Definition: Vector.h:40
typename std::conditional< matioCpp::is_string16_compatible< T >::value, std::u16string, typename std::conditional< matioCpp::is_string32_compatible< T >::value, std::u32string, std::string >::type >::type string_input_type
The const reverse iterator type.
Definition: Vector.h:51
reverse_iterator rbegin()
rbegin Iterator to the first element of the reversed vector
Definition: Vector.tpp:368
Vector()
The output type of the conversion to string.
Definition: Vector.tpp:52
iterator begin()
begin Iterator
Definition: Vector.tpp:332
void clear()
Clear the vector.
Definition: Vector.tpp:280
string_output_type operator()() const
Get the Vector as a string.
Definition: Vector.tpp:310
const_iterator cbegin() const
cbegin Iterator
Definition: Vector.tpp:356
reference operator[](index_type el)
Access specified element.
typename std::conditional< matioCpp::is_string_compatible< T >::value, std::string, typename std::conditional< matioCpp::is_string16_compatible< T >::value, std::u16string, typename std::conditional< matioCpp::is_string32_compatible< T >::value, std::u32string, void >::type >::type >::type string_output_type
The type of string that can be used as initialization.
Definition: Vector.h:55
typename std::allocator_traits< std::allocator< element_type > >::pointer pointer
The reference type.
Definition: Vector.h:38
void resize(index_type newSize)
Resize the vector.
Definition: Vector.tpp:269
const_reverse_iterator crbegin() const
crbegin Iterator to the first element of the reversed vector
Definition: Vector.tpp:392
const_iterator cend() const
cend Iterator
Definition: Vector.tpp:362
iterator end()
end Iterator
Definition: Vector.tpp:338
~Vector()
Destructor.
Definition: Vector.tpp:161
bool setName(const std::string &newName)
Change the name of the Variable.
Definition: Vector.tpp:254
T data(T... args)
T endl(T... args)
T forward(T... args)
T max(T... args)
T min(T... args)
T move(T... args)
MATIOCPP_CONSTEXPR Span< ElementType > make_span(ElementType *ptr, typename Span< ElementType >::index_type count)
Definition: Span.h:714
VariableType
Define the type of variable.
ValueType
The list of types for an element of a certain variable type.
bool get_types_names_from_matvart(const matvar_t *input, std::string &classType, std::string &dataType)
Get the type names from the input pointer.
T push_back(T... args)
T size(T... args)
Utility metafunction to get the ValueType from a given primitive type.
Utility meta-function to check if a type is compatible with a std::u16string.
Utility meta-function to check if a type is compatible with a std::u32string.
Utility meta-function to check if a type is compatible with a std::string.