MocoExtendProblem: Interface Between OpenSim and MATLAB for Rapidly Developing Direct Collocation Goals in Moco 1.1.0
add custom Moco goals to existing matlab scripts
mxtypes.h
Go to the documentation of this file.
1
6#ifndef INCLUDE_MEXPLUS_MXTYPES_H_
7#define INCLUDE_MEXPLUS_MXTYPES_H_
8
9#include <mex.h>
10#include <complex>
11#include <type_traits>
12
13namespace mexplus {
14
15/************************************************************/
16/* Traits for fundamental datatypes.
17 Don't use with function templates due to type promotion!
18 (Ignore it and get PITA!) */
19/************************************************************/
20
23template <typename T, typename U = T>
24struct MxLogicalTy : std::false_type {};
25
26template <typename T>
27struct MxLogicalTy<T, typename std::enable_if<
28 std::is_same<typename std::remove_cv<T>::type, bool>::value ||
29 std::is_same<typename std::remove_cv<T>::type, mxLogical>::value,
30 T>::type> : std::true_type {};
31
38template <typename T, typename U = T>
39struct MxCharTy : std::false_type {};
40
41template <typename T>
42struct MxCharTy<T, typename std::enable_if<
43 std::is_same<typename std::remove_cv<T>::type, char>::value ||
44 // Visual Studio cannot distinguish these from uint.
45 // std::is_same<typename std::remove_cv<T>::type, char16_t>::value ||
46 // std::is_same<typename std::remove_cv<T>::type, char32_t>::value ||
47 std::is_same<typename std::remove_cv<T>::type, mxChar>::value ||
48 std::is_same<typename std::remove_cv<T>::type, wchar_t>::value,
49 T>::type> : std::true_type {};
50
53template <typename T, typename U = T>
54struct MxIntTy : std::false_type {};
55template <typename T>
56struct MxIntTy<T, typename std::enable_if<
57 std::is_integral<T>::value &&
58 !MxLogicalTy<T>::value &&
59 !MxCharTy<T>::value,
60 T>::type> : std::true_type {};
63template <typename T, typename U = T>
64struct MxArithmeticTy : std::false_type {};
65template <typename T>
66struct MxArithmeticTy<T, typename std::enable_if<
67 (std::is_floating_point<T>::value) || (MxIntTy<T>::value),
68 T>::type> : std::true_type {};
69
70/**********************************************/
71/* Introducing traits for MATLAB array types. */
72/**********************************************/
73
74typedef struct mxNumeric_tag {} mxNumeric;
75typedef struct mxCell_tag {} mxCell;
76typedef struct mxComplex_tag {} mxComplex;
77// mxLogical already defined in MATLAB (matrix.h).
78
81template <typename T, typename U = T>
82struct MxTypes {
83 typedef T type;
85 static const mxClassID class_id = mxUNKNOWN_CLASS;
86 static const mxComplexity complexity = mxREAL;
87};
88
89template <typename T>
90struct MxTypes<T, typename std::enable_if<MxCharTy<T>::value, T>::type> {
91 typedef T type;
92 typedef mxChar array_type;
93 static const mxClassID class_id = mxCHAR_CLASS;
94 static const mxComplexity complexity = mxREAL;
95};
96
97template <typename T>
98struct MxTypes<T, typename std::enable_if<MxLogicalTy<T>::value, T>::type> {
99 typedef T type;
100 typedef mxLogical array_type;
101 static const mxClassID class_id = mxLOGICAL_CLASS;
102 static const mxComplexity complexity = mxREAL;
103};
104
105template <typename T>
106struct MxTypes<T, typename std::enable_if<std::is_signed<T>::value &&
107 MxIntTy<T>::value &&
108 sizeof(T) == 1, T>::type> {
109 typedef T type;
111 static const mxClassID class_id = mxINT8_CLASS;
112 static const mxComplexity complexity = mxREAL;
113};
114
115template <typename T>
116struct MxTypes<T, typename std::enable_if<std::is_unsigned<T>::value &&
117 MxIntTy<T>::value &&
118 sizeof(T) == 1, T>::type> {
119 typedef T type;
121 static const mxClassID class_id = mxUINT8_CLASS;
122 static const mxComplexity complexity = mxREAL;
123};
124
125template <typename T>
126struct MxTypes<T, typename std::enable_if<std::is_signed<T>::value &&
127 MxIntTy<T>::value &&
128 sizeof(T) == 2, T>::type> {
129 typedef T type;
131 static const mxClassID class_id = mxINT16_CLASS;
132 static const mxComplexity complexity = mxREAL;
133};
134
135template <typename T>
136struct MxTypes<T, typename std::enable_if<std::is_unsigned<T>::value &&
137 MxIntTy<T>::value &&
138 sizeof(T) == 2, T>::type> {
139 typedef T type;
141 static const mxClassID class_id = mxUINT16_CLASS;
142 static const mxComplexity complexity = mxREAL;
143};
144
145template <typename T>
146struct MxTypes<T, typename std::enable_if<std::is_signed<T>::value &&
147 MxIntTy<T>::value &&
148 sizeof(T) == 4, T>::type> {
149 typedef T type;
151 static const mxClassID class_id = mxINT32_CLASS;
152 static const mxComplexity complexity = mxREAL;
153};
154
155template <typename T>
156struct MxTypes<T, typename std::enable_if<std::is_unsigned<T>::value &&
157 MxIntTy<T>::value &&
158 sizeof(T) == 4, T>::type> {
159 typedef T type;
161 static const mxClassID class_id = mxUINT32_CLASS;
162 static const mxComplexity complexity = mxREAL;
163};
164
165template <typename T>
166struct MxTypes<T, typename std::enable_if<std::is_signed<T>::value &&
167 MxIntTy<T>::value &&
168 sizeof(T) == 8, T>::type> {
169 typedef T type;
171 static const mxClassID class_id = mxINT64_CLASS;
172 static const mxComplexity complexity = mxREAL;
173};
174
175template <typename T>
176struct MxTypes<T, typename std::enable_if<std::is_unsigned<T>::value &&
177 MxIntTy<T>::value &&
178 sizeof(T) == 8, T>::type> {
179 typedef T type;
181 static const mxClassID class_id = mxUINT64_CLASS;
182 static const mxComplexity complexity = mxREAL;
183};
184
185template <typename T>
186struct MxTypes<T, typename std::enable_if<std::is_floating_point<T>::value &&
187 sizeof(T) == 4, T>::type> {
188 typedef T type;
190 static const mxClassID class_id = mxSINGLE_CLASS;
191 static const mxComplexity complexity = mxREAL;
192};
193
194template <typename T>
195struct MxTypes<T, typename std::enable_if<std::is_floating_point<T>::value &&
196 sizeof(T) == 8, T>::type> {
197 typedef T type;
199 static const mxClassID class_id = mxDOUBLE_CLASS;
200 static const mxComplexity complexity = mxREAL;
201};
202
203template <typename T>
204struct MxTypes<T, typename std::enable_if<
205 std::is_same<typename std::remove_cv<T>::type,
206 std::complex<float>>::value,
207 T>::type> {
208 typedef T type;
210 static const mxClassID class_id = mxSINGLE_CLASS;
211 static const mxComplexity complexity = mxCOMPLEX;
212};
213
214template <typename T>
215struct MxTypes<T, typename std::enable_if<
216 std::is_same<typename std::remove_cv<T>::type,
217 std::complex<double>>::value,
218 T>::type> {
219 typedef T type;
221 static const mxClassID class_id = mxDOUBLE_CLASS;
222 static const mxComplexity complexity = mxCOMPLEX;
223};
224
225/********************************************/
226/* Type traits for function template usage. */
227/********************************************/
228
229/* Traits for logical types.
230 */
231template <typename T, typename U = T>
232struct MxLogicalType : std::false_type {};
233template<typename T>
234struct MxLogicalType<T, typename std::enable_if<
235 std::is_same<typename MxTypes<T>::array_type, mxLogical>::value,
236 T>::type> : std::true_type {};
237
238/* Traits for char types.
239 */
240template <typename T, typename U = T>
241struct MxCharType : std::false_type {};
242template<typename T>
243struct MxCharType<T, typename std::enable_if<
244 std::is_same<typename MxTypes<T>::array_type, mxChar>::value,
245 T>::type> : std::true_type {};
246
247/* Traits for arithmetic types.
248 */
249template <typename T, typename U = T>
250struct MxArithmeticType : std::false_type {};
251template<typename T>
252struct MxArithmeticType<T, typename std::enable_if<
253 std::is_same<typename MxTypes<T>::array_type, mxNumeric>::value,
254 T>::type> : std::true_type {};
255
256/* Traits for complex types.
257 */
258template <typename T, typename U = T>
259struct MxComplexType : std::false_type {};
260template<typename T>
261struct MxComplexType<T, typename std::enable_if<
262 std::is_same<typename MxTypes<T>::array_type, mxComplex>::value,
263 T>::type> : std::true_type {};
264
265/* Traits for complex or arithmetic types.
266 */
267template <typename T, typename U = T>
268struct MxComplexOrArithmeticType : std::false_type {};
269template <typename T>
270struct MxComplexOrArithmeticType<T, typename std::enable_if<
271 MxComplexType<T>::value,
272 T>::type> : std::true_type {};
273template <typename T>
274struct MxComplexOrArithmeticType<T, typename std::enable_if<
275 MxArithmeticTy<T>::value,
276 T>::type> : std::true_type {};
277
278/* Traits for cell types.
279 */
280template <typename T, typename U = T>
281struct MxCellType : std::false_type {};
282template <typename T>
283struct MxCellType<T, typename std::enable_if<
284 std::is_same<typename MxTypes<T>::array_type, mxCell>::value,
285 T>::type> : std::true_type {};
286
287/* Traits for logical type compounds.
288 */
289template <typename T, typename U = T>
290struct MxLogicalCompound : std::false_type {};
291template <typename T>
292struct MxLogicalCompound<T, typename std::enable_if<
293 MxLogicalType<typename T::value_type>::value,
294 T>::type> : std::true_type {};
295
296/* Traits for char type compounds.
297 */
298template <typename T, typename U = T>
299struct MxCharCompound : std::false_type {};
300template <typename T>
301struct MxCharCompound<T, typename std::enable_if<
302 MxCharType<typename T::value_type>::value,
303 T>::type> : std::true_type {};
304
305/* Traits for arithmetic type compounds.
306 */
307template <typename T, typename U = T>
308struct MxArithmeticCompound : std::false_type {};
309template <typename T>
310struct MxArithmeticCompound<T, typename std::enable_if<
311 (MxArithmeticType<typename T::value_type>::value) &&
312 !(MxComplexType<T>::value),
313 T>::type> : std::true_type {};
314
315/* Traits for complex type compounds.
316 */
317template <typename T, typename U = T>
318struct MxComplexCompound : std::false_type {};
319template <typename T>
320struct MxComplexCompound<T, typename std::enable_if<
321 MxComplexType<typename T::value_type>::value,
322 T>::type> : std::true_type {};
323
324/* Traits for complex or arithmetic type compounds.
325 */
326template <typename T, typename U = T>
327struct MxComplexOrArithmeticCompound : std::false_type {};
328template <typename T>
329struct MxComplexOrArithmeticCompound<T, typename std::enable_if<
330 MxComplexCompound<T>::value ||
331 MxArithmeticCompound<T>::value,
332 T>::type> : std::true_type {};
333
334/* Traits for cell compounds.
335 */
336template <typename T, typename U = T>
337struct MxCellCompound : std::false_type {};
338template <typename T>
339struct MxCellCompound<T, typename std::enable_if<
340 MxCellType<typename T::value_type>::value,
341 T>::type> : std::true_type {};
342
343} // namespace mexplus
344
345#endif // INCLUDE_MEXPLUS_MXTYPES_H_
MEX function arguments helper library.
Definition arguments.h:40
Definition mxtypes.h:308
Traits for arithmetic types.
Definition mxtypes.h:64
Definition mxtypes.h:250
Definition mxtypes.h:337
Definition mxtypes.h:281
Definition mxtypes.h:299
Traits for mxChar-convertibles.
Definition mxtypes.h:39
Definition mxtypes.h:241
Definition mxtypes.h:318
Definition mxtypes.h:268
Definition mxtypes.h:259
Traits for integer numerics.
Definition mxtypes.h:54
Definition mxtypes.h:290
Traits for mxLogical-convertibles.
Definition mxtypes.h:24
Definition mxtypes.h:232
Traits for mxArray.
Definition mxtypes.h:82
T type
Definition mxtypes.h:83
static const mxComplexity complexity
Definition mxtypes.h:86
mxCell array_type
Definition mxtypes.h:84
static const mxClassID class_id
Definition mxtypes.h:85
Definition mxtypes.h:75
Definition mxtypes.h:76
Definition mxtypes.h:74