matio-cpp  v0.2.5
A C++ wrapper of the matio library, with memory ownership handling, to read and write .mat files.
Element.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_ELEMENT_TPP
9 #define MATIOCPP_ELEMENT_TPP
10 
11 template<typename T>
12 bool matioCpp::Element<T>::checkCompatibility(const matvar_t* inputPtr, matioCpp::VariableType variableType, matioCpp::ValueType valueType) const
13 {
14 
15  if (variableType != matioCpp::VariableType::Element)
16  {
17  std::cerr << "[matioCpp::Element::checkCompatibility] The variable type is not compatible with an Element." << std::endl;
18  return false;
19  }
20 
21  if (inputPtr->isComplex)
22  {
23  std::cerr << "[matioCpp::Element::checkCompatibility] Cannot use a complex variable into a non-complex one." << std::endl;
24  return false;
25  }
26 
27  if (!matioCpp::is_convertible_to_primitive_type<T>(valueType))
28  {
29  std::string dataType = "";
30  std::string classType = "";
31 
32  get_types_names_from_matvart(inputPtr, classType, dataType);
33 
34  std::cerr << "[matioCpp::Element::checkCompatibility] The value type is not convertible to " <<
35  get_type<T>::toString() <<"." << std::endl <<
36  " Input class type: " << classType << std::endl <<
37  " Input data type: " << dataType << std::endl;
38  return false;
39  }
40  return true;
41 }
42 
43 template<typename T>
45 {
46  static_assert(!std::is_same<T, std::string>::value, "A string is not handled by matio. Use Vector<char> instead." );
48 
49  if (std::is_same<T, char>::value) //If the type is char, use \0 to make sure strlen works well, if used
50  {
51  empty = '\0';
52  }
53  size_t emptyDimensions[] = {1, 1};
54  initializeVariable("unnamed_element",
55  VariableType::Element,
56  matioCpp::get_type<T>::valueType(), emptyDimensions,
57  (void*)&empty);
58 }
59 
60 template<typename T>
62 {
63  static_assert(!std::is_same<T, std::string>::value, "A string is not handled by matio. Use Vector<char> instead." );
65  size_t emptyDimensions[] = {1, 1};
66  initializeVariable(name,
67  VariableType::Element,
68  matioCpp::get_type<T>::valueType(), emptyDimensions,
69  (void*)&empty);
70 }
71 
72 template<typename T>
74 {
75  static_assert(!std::is_same<T, std::string>::value, "A string is not handled by matio. Use Vector<char> instead." );
76  typename matioCpp::Element<T>::element_type empty = inputValue;
77  size_t emptyDimensions[] = {1, 1};
78  initializeVariable(name,
79  VariableType::Element,
80  matioCpp::get_type<T>::valueType(), emptyDimensions,
81  (void*)&empty);
82 }
83 
84 template<typename T>
86 {
87  fromOther(other);
88 }
89 
90 template<typename T>
92 {
93  fromOther(std::forward<matioCpp::Element<T>>(other));
94 }
95 
96 template<typename T>
98  : matioCpp::Variable(handler)
99 {
100  if (!handler.get() || !checkCompatibility(handler.get(), handler.variableType(), handler.valueType()))
101  {
102  assert(false);
103  typename matioCpp::Element<T>::element_type empty;
104  size_t emptyDimensions[] = {1, 1};
105  initializeVariable("unnamed_element",
107  matioCpp::get_type<T>::valueType(), emptyDimensions,
108  (void*)&empty);
109  }
110 }
111 
112 template<typename T>
114 {
115 
116 }
117 
118 template<typename T>
120 {
121  fromOther(other);
122  return *this;
123 }
124 
125 template<typename T>
127 {
128  fromOther(std::forward<matioCpp::Element<T>>(other));
129  return *this;
130 }
131 
132 template<typename T>
134 {
135  operator()() = value;
136  return *this;
137 }
138 
139 template<typename T>
141 {
142  return matioCpp::make_span(static_cast<typename matioCpp::Element<T>::pointer>(toMatio()->data), 1);
143 }
144 
145 template<typename T>
147 {
148  return matioCpp::make_span(static_cast<typename matioCpp::Element<T>::const_pointer>(toMatio()->data), 1);
149 }
150 
151 template<typename T>
153 {
154  return changeName(newName);
155 }
156 
157 template<typename T>
159 {
160  return (((typename matioCpp::Element<T>::pointer)(toMatio()->data))[0]);
161 }
162 
163 template<typename T>
165 {
166  return (((typename matioCpp::Element<T>::const_pointer)(toMatio()->data))[0]);
167 }
168 
169 template<typename T>
171 {
172  return (((typename matioCpp::Element<T>::const_pointer)(toMatio()->data))[0]);
173 }
174 
175 template<typename T>
177 {
178  return matioCpp::Element<T>(*m_handler);
179 }
180 
181 template<typename T>
183 {
184  return matioCpp::Element<T>(*m_handler);
185 }
186 
187 #endif // MATIOCPP_ELEMENT_TPP
Element allows having a 1x1 variable (like double, int,..) castable to a primitive type.
Definition: Element.h:20
matioCpp::Span< element_type > toSpan()
Get this Vector as a Span.
Definition: Element.tpp:140
typename std::allocator_traits< std::allocator< element_type > >::const_pointer const_pointer
The pointer type.
Definition: Element.h:43
reference operator()()
Access the Element.
Definition: Element.tpp:158
Element()
The const pointer type.
Definition: Element.tpp:44
element_type & reference
Defines the type of the Element.
Definition: Element.h:39
bool setName(const std::string &newName)
Change the name of the Variable.
Definition: Element.tpp:152
Element< T > & operator=(const Element< T > &other)
Assignement operator (copy) from another Element.
Definition: Element.tpp:119
typename std::allocator_traits< std::allocator< element_type > >::pointer pointer
The reference type.
Definition: Element.h:41
typename get_type< T >::type element_type
Defines the type specified in the template.
Definition: Element.h:35
std::remove_cv_t< element_type > value_type
Defines the type of the Element.
Definition: Element.h:37
~Element()
Destructor.
Definition: Element.tpp:113
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::Element< T > asElement()
Cast the variable as a Element.
Definition: Element.tpp:176
bool initializeVariable(const std::string &name, const VariableType &variableType, const ValueType &valueType, matioCpp::Span< const size_t > dimensions, void *data)
Initialize the variable.
Definition: Variable.cpp:16
T endl(T... args)
T forward(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.
Utility metafunction to get the ValueType from a given primitive type.