matio-cpp  v0.2.5
A C++ wrapper of the matio library, with memory ownership handling, to read and write .mat files.
ExogenousConversions.tpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 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 #ifndef MATIOCPP_EXOGENOUSCONVERSIONS_TPP
8 #define MATIOCPP_EXOGENOUSCONVERSIONS_TPP
9 
10 template <class Vector, typename>
12 {
14  return matioCpp::Vector<typename std::remove_cv_t<type>>(name, matioCpp::make_span(input)); //data is copied
15 }
16 
17 template<typename type, typename>
18 inline matioCpp::Element<type> matioCpp::make_variable(const std::string& name, const type& input)
19 {
20  return matioCpp::Element<type>(name, input);
21 }
22 
23 template<typename Struct, typename>
25 {
26  matioCpp::Struct matioStruct(name);
27 
28  visit_struct::for_each(input,
29  [& matioStruct](const char * name, const auto & value) {
30  static_assert (is_make_variable_callable<decltype(value)>::value, "The input struct contains non-compatible fields.");
31  bool ok = matioStruct.setField(make_variable(name, value));
32  matioCpp::unused(ok);
33  assert(ok);
34  });
35  return matioStruct;
36 }
37 
38 template<class iterator,
39  typename>
40 inline matioCpp::Struct matioCpp::make_struct(const std::string& name, iterator begin, iterator end)
41 {
42  matioCpp::Struct matioStruct(name);
43  for (iterator it = begin; it != end; it++)
44  {
45  bool ok = matioStruct.setField(make_variable(it->first, it->second));
46  matioCpp::unused(ok);
47  assert(ok);
48  }
49 
50  return matioStruct;
51 }
52 
53 template<class iterator,
54  typename std::enable_if_t<matioCpp::is_pair_iterator_string<iterator>::value>*>
55 inline matioCpp::CellArray matioCpp::make_cell_array(const std::string& name, const iterator& begin, const iterator& end)
56 {
57  matioCpp::CellArray matioCellArray(name, {static_cast<size_t>(std::distance(begin, end)), 1});
58 
59  size_t index = 0;
60  iterator it = begin;
61  while (it != end)
62  {
63  bool ok = matioCellArray.setElement(index, make_variable(it->first, it->second));
64  matioCpp::unused(ok);
65  assert(ok);
66  index++;
67  it++;
68  }
69 
70  return matioCellArray;
71 }
72 
73 template<class iterator,
74  typename std::enable_if_t<!matioCpp::is_pair<decltype(*std::declval<iterator>())>::value>*>
75 inline matioCpp::CellArray matioCpp::make_cell_array(const std::string& name, const iterator& begin, const iterator& end)
76 {
77  matioCpp::CellArray matioCellArray(name, {static_cast<size_t>(std::distance(begin, end)), 1});
78 
79  size_t index = 0;
80  iterator it = begin;
81  while (it != end)
82  {
83  bool ok = matioCellArray.setElement(index, make_variable("imported_element_" + std::to_string(index), *it));
84  matioCpp::unused(ok);
85  assert(ok);
86  index++;
87  it++;
88  }
89 
90  return matioCellArray;
91 }
92 
93 #endif // MATIOCPP_EXOGENOUSCONVERSIONS_TPP
CellArray is a particular type of Variable specialized for cell arrays.
Definition: CellArray.h:18
Element allows having a 1x1 variable (like double, int,..) castable to a primitive type.
Definition: Element.h:20
Struct is a particular type of Variable specialized for structs.
Definition: Struct.h:18
bool setField(index_type index, const Variable &newValue)
Set the field at the specified position.
Definition: Struct.cpp:173
Vector is a particular type of Variable specialized for 1-D arrays of a generic type T.
Definition: Vector.h:23
T distance(T... args)
matioCpp::Struct make_struct(const std::string &name, iterator begin, iterator end)
Create a matioCpp::Struct starting from the begin and end iterators of a map-like container The deref...
matioCpp::CellArray make_cell_array(const std::string &name, const iterator &begin, const iterator &end)
Create a matioCpp::CellArray starting from the begin and end iterators of a container.
MATIOCPP_CONSTEXPR Span< ElementType > make_span(ElementType *ptr, typename Span< ElementType >::index_type count)
Definition: Span.h:714
void unused(Args &&...)
Utility metafunction to avoid compiler warnings about unused variables.
matioCpp::Vector< typename std::remove_cv_t< typename matioCpp::SpanUtils::container_data< Vector >::type > > make_variable(const std::string &name, const Vector &input)
Conversion from a generic vector to a matioCpp::Vector.
is_make_variable_callable is a template utility to check if the make_variable works for a give type
T to_string(T... args)