matio-cpp  v0.2.5
A C++ wrapper of the matio library, with memory ownership handling, to read and write .mat files.
ConversionUtilities.h
Go to the documentation of this file.
1 #ifndef MATIOCPP_UTILITIES_H
2 #define MATIOCPP_UTILITIES_H
3 /*
4  * Copyright (C) 2020 Fondazione Istituto Italiano di Tecnologia
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-2-Clause license (https://opensource.org/licenses/BSD-2-Clause).
8  */
9 
11 
12 namespace matioCpp {
13 
22 bool get_matio_types(const VariableType& inputVariableType, const ValueType& inputValueType, matio_classes& outputMatioClasses, matio_types& outputMatioType);
23 
31 bool get_types_from_matvart(const matvar_t* input, VariableType& outputVariableType, ValueType &outputValueType);
32 
39 bool get_types_names_from_matvart(const matvar_t* input, std::string &classType, std::string &dataType);
40 
44 template <typename Tp> struct get_type
45 {
46  static_assert (matioCpp::dependent_false<Tp>::value, "Unsupported type.");
47  static_assert (!std::is_same<Tp, bool>::value, "Use matioCpp::Logical instead of bool.");
48 };
49 
50 #ifdef __APPLE__
51 using size_t_type = unsigned long;
52 #else
53 using size_t_type = uint64_t;
54 #endif
55 
56 // specializations
57 template <> struct get_type<int8_t> { using type = int8_t; static inline ValueType valueType(){return ValueType::INT8;}; static inline std::string toString(){return "int8_t" ;};};
58 template <> struct get_type<uint8_t> { using type = uint8_t; static inline ValueType valueType(){return ValueType::UINT8;}; static inline std::string toString(){return "uint8_t" ;};};
59 template <> struct get_type<int16_t> { using type = int16_t; static inline ValueType valueType(){return ValueType::INT16;}; static inline std::string toString(){return "int16_t" ;};};
60 template <> struct get_type<uint16_t> { using type = uint16_t; static inline ValueType valueType(){return ValueType::UINT16;}; static inline std::string toString(){return "uint16_t" ;};};
61 template <> struct get_type<int32_t> { using type = int32_t; static inline ValueType valueType(){return ValueType::INT32;}; static inline std::string toString(){return "int32_t" ;};};
62 template <> struct get_type<uint32_t> { using type = uint32_t; static inline ValueType valueType(){return ValueType::UINT32;}; static inline std::string toString(){return "uint32_t" ;};};
63 template <> struct get_type<float> { using type = float; static inline ValueType valueType(){return ValueType::SINGLE;}; static inline std::string toString(){return "float" ;};};
64 template <> struct get_type<double> { using type = double; static inline ValueType valueType(){return ValueType::DOUBLE;}; static inline std::string toString(){return "double" ;};};
65 template <> struct get_type<int64_t> { using type = int64_t; static inline ValueType valueType(){return ValueType::INT64;}; static inline std::string toString(){return "int64_t" ;};};
66 template <> struct get_type<size_t_type> { using type = size_t_type; static inline ValueType valueType(){return ValueType::UINT64;}; static inline std::string toString(){return "size_t" ;};};
67 template <> struct get_type<char> { using type = char; static inline ValueType valueType(){return ValueType::UTF8;}; static inline std::string toString(){return "char" ;};};
68 template <> struct get_type<char16_t> { using type = char16_t; static inline ValueType valueType(){return ValueType::UTF16;}; static inline std::string toString(){return "char16_t" ;};};
69 template <> struct get_type<char32_t> { using type = char32_t; static inline ValueType valueType(){return ValueType::UTF32;}; static inline std::string toString(){return "char32_t" ;};};
70 template <> struct get_type<Logical> { using type = uint8_t; static inline ValueType valueType(){return ValueType::LOGICAL;}; static inline std::string toString(){return "matioCpp::Logical" ;};};
71 
75 template <typename T>
77 template<> struct is_string_compatible<char> : std::true_type { };
78 template<> struct is_string_compatible<uint8_t> : std::true_type { };
79 
83 template <typename T>
85 template<> struct is_string16_compatible<char16_t> : std::true_type { };
86 template<> struct is_string16_compatible<uint16_t> : std::true_type { };
87 
91 template <typename T>
93 template<> struct is_string32_compatible<char32_t> : std::true_type { };
94 template<> struct is_string32_compatible<uint32_t> : std::true_type { };
95 
101 template <typename T>
103 {
104  switch (type)
105  {
108  break;
111  break;
114  break;
117  break;
120  break;
123  break;
126  break;
129  break;
132  break;
135  break;
138  break;
141  break;
144  break;
147  break;
151  return false;
152  }
153  return false;
154 }
155 
156 
157 }
158 
159 
160 #endif //MATIOCPP_UTILITIES_H
Utility class to consider logical values.
bool is_convertible_to_primitive_type(matioCpp::ValueType type)
Utility function to check if certain ValueType can be converted to a primitive type T.
bool get_matio_types(const VariableType &inputVariableType, const ValueType &inputValueType, matio_classes &outputMatioClasses, matio_types &outputMatioType)
Get both the matio type and class from the input VariableType and ValueType.
VariableType
Define the type of variable.
bool get_types_from_matvart(const matvar_t *input, VariableType &outputVariableType, ValueType &outputValueType)
Get the VariableType and the ValueType from a matvar_t pointer.
ValueType
The list of types for an element of a certain variable type.
uint64_t size_t_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.
dependent_false is a type-dependent expression that is always false.
static std::string toString()
static std::string toString()
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.