forked from openPMD/openPMD-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatatype.tpp
More file actions
309 lines (285 loc) · 9.75 KB
/
Copy pathDatatype.tpp
File metadata and controls
309 lines (285 loc) · 9.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* Copyright 2017-2023 Fabian Koller, Franz Poeschel, Axel Huebl
*
* This file is part of openPMD-api.
*
* openPMD-api is free software: you can redistribute it and/or modify
* it under the terms of of either the GNU General Public License or
* the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* openPMD-api is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License and the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* and the GNU Lesser General Public License along with openPMD-api.
* If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "openPMD/Datatype.hpp"
// comment to prevent clang-format from moving this #include up
// datatype macros may be included and un-included in other headers
#include "openPMD/DatatypeMacros.hpp"
#include "openPMD/auxiliary/TypeTraits.hpp"
#include <string>
#include <type_traits> // std::void_t
#include <utility> // std::forward
namespace openPMD
{
namespace detail
{
// std::void_t is C++17
template <typename>
using void_t = void;
/*
* Check whether class T has a member "errorMsg" convertible
* to type std::string.
* Used to give helpful compile-time error messages with static_assert
* down in CallUndefinedDatatype.
*/
template <typename T, typename = void>
struct HasErrorMessageMember
{
static constexpr bool value = false;
};
template <typename T>
struct HasErrorMessageMember<T, void_t<decltype(std::string(T::errorMsg))>>
{
static constexpr bool value = true;
};
/**
* Purpose of this struct is to detect at compile time whether
* Action::template operator()\<0\>() exists. If yes, call
* Action::template operator()\<n\>() with the passed arguments.
* If not, throw an error.
*
* @tparam n As in switchType().
* @tparam ReturnType As in switchType().
* @tparam Action As in switchType().
* @tparam Args As in switchType().
*/
template <int n, typename ReturnType, typename Action, typename... Args>
struct CallUndefinedDatatype
{
static ReturnType call(Args &&...args)
{
if constexpr (HasErrorMessageMember<Action>::value)
{
throw std::runtime_error(
"[" + std::string(Action::errorMsg) +
"] Unknown Datatype.");
}
else
{
return Action::template call<n>(std::forward<Args>(args)...);
}
throw std::runtime_error("Unreachable!");
}
};
} // namespace detail
#define OPENPMD_SWITCHTYPE_IMPL(type) \
case determineDatatype<type>(): \
return Action::template call<type>(std::forward<Args>(args)...);
/**
* Generalizes switching over an openPMD datatype.
*
* Will call the function template found at Action::call< T >(), instantiating T
* with the C++ internal datatype corresponding to the openPMD datatype.
*
* @tparam ReturnType The function template's return type.
* @tparam Action The struct containing the function template.
* @tparam Args The function template's argument types.
* @param dt The openPMD datatype.
* @param args The function template's arguments.
* @return Passes on the result of invoking the function template with the given
* arguments and with the template parameter specified by dt.
*/
template <typename Action, typename... Args>
constexpr auto switchType(Datatype dt, Args &&...args)
-> decltype(Action::template call<char>(std::forward<Args>(args)...))
{
using ReturnType =
decltype(Action::template call<char>(std::forward<Args>(args)...));
switch (dt)
{
OPENPMD_FOREACH_DATATYPE(OPENPMD_SWITCHTYPE_IMPL)
case Datatype::UNDEFINED:
return detail::
CallUndefinedDatatype<0, ReturnType, Action, Args &&...>::call(
std::forward<Args>(args)...);
default:
throw std::runtime_error(
"Internal error: Encountered unknown datatype (switchType) ->" +
std::to_string(static_cast<int>(dt)));
}
}
/**
* Generalizes switching over an openPMD datatype.
*
* Will call the function template found at Action::call< T >(), instantiating T
* with the C++ internal datatype corresponding to the openPMD datatype.
* Ignores vector and array types.
*
* @tparam ReturnType The function template's return type.
* @tparam Action The struct containing the function template.
* @tparam Args The function template's argument types.
* @param dt The openPMD datatype.
* @param args The function template's arguments.
* @return Passes on the result of invoking the function template with the given
* arguments and with the template parameter specified by dt.
*/
template <typename Action, typename... Args>
constexpr auto switchNonVectorType(Datatype dt, Args &&...args)
-> decltype(Action::template call<char>(std::forward<Args>(args)...))
{
using ReturnType =
decltype(Action::template call<char>(std::forward<Args>(args)...));
switch (dt)
{
OPENPMD_FOREACH_NONVECTOR_DATATYPE(OPENPMD_SWITCHTYPE_IMPL)
case Datatype::UNDEFINED:
return detail::
CallUndefinedDatatype<0, ReturnType, Action, Args &&...>::call(
std::forward<Args>(args)...);
default:
throw std::runtime_error(
"Internal error: Encountered unknown datatype (switchType) ->" +
std::to_string(static_cast<int>(dt)));
}
}
/**
* Generalizes switching over an openPMD datatype.
*
* Will call the function template found at Action::call< T >(), instantiating T
* with the C++ internal datatype corresponding to the openPMD datatype.
* Specializes only on those types that can occur in a dataset.
*
* @tparam ReturnType The function template's return type.
* @tparam Action The struct containing the function template.
* @tparam Args The function template's argument types.
* @param dt The openPMD datatype.
* @param args The function template's arguments.
* @return Passes on the result of invoking the function template with the given
* arguments and with the template parameter specified by dt.
*/
template <typename Action, typename... Args>
constexpr auto switchDatasetType(Datatype dt, Args &&...args)
-> decltype(Action::template call<char>(std::forward<Args>(args)...))
{
using ReturnType =
decltype(Action::template call<char>(std::forward<Args>(args)...));
switch (dt)
{
OPENPMD_FOREACH_DATASET_DATATYPE(OPENPMD_SWITCHTYPE_IMPL)
case Datatype::UNDEFINED:
return detail::
CallUndefinedDatatype<0, ReturnType, Action, Args &&...>::call(
std::forward<Args>(args)...);
default:
throw std::runtime_error(
"Internal error: Encountered unknown datatype (switchType) ->" +
std::to_string(static_cast<int>(dt)));
}
}
#undef OPENPMD_SWITCHTYPE_IMPL
namespace detail
{
template <typename T>
struct is_char
{
static constexpr bool value = false;
};
template <>
struct is_char<char>
{
static constexpr bool value = true;
};
template <>
struct is_char<signed char>
{
static constexpr bool value = true;
};
template <>
struct is_char<unsigned char>
{
static constexpr bool value = true;
};
template <typename T>
constexpr bool is_char_v = is_char<T>::value;
template <typename T_Char1, typename T_Char2>
inline bool isSameChar()
{
return
// both must be char types
is_char_v<T_Char1> && is_char_v<T_Char2> &&
// both must have equivalent sign
std::is_signed_v<T_Char1> == std::is_signed_v<T_Char2> &&
// both must have equivalent size
sizeof(T_Char1) == sizeof(T_Char2);
}
template <typename T1>
struct IsSameChar
{
template <typename T2>
static bool call()
{
return isSameChar<T1, T2>();
}
static constexpr char const *errorMsg = "IsSameChar";
};
} // namespace detail
template <typename T_Char>
constexpr inline bool isSameChar(Datatype d)
{
return switchType<detail::IsSameChar<T_Char>>(d);
}
namespace detail
{
struct IsSigned
{
template <typename T>
static constexpr bool call()
{
if constexpr (auxiliary::IsVector_v<T> || auxiliary::IsArray_v<T>)
{
return call<typename T::value_type>();
}
else if constexpr (std::is_same_v<T, std::string>)
{
return call<char>();
}
else
{
return std::is_signed_v<T>;
}
}
static constexpr char const *errorMsg = "IsSigned";
};
} // namespace detail
constexpr inline bool isSigned(Datatype d)
{
return switchType<detail::IsSigned>(d);
}
constexpr inline bool isSameChar(Datatype d, Datatype e)
{
return isChar(d) && isChar(e) && isSigned(d) == isSigned(e);
}
constexpr bool isSame(openPMD::Datatype const d, openPMD::Datatype const e)
{
return
// exact same type
static_cast<int>(d) == static_cast<int>(e)
// same int
|| isSameInteger(d, e)
// same float
|| isSameFloatingPoint(d, e)
// same complex floating point
|| isSameComplexFloatingPoint(d, e)
// same char
|| isSameChar(d, e);
}
} // namespace openPMD
#include "openPMD/UndefDatatypeMacros.hpp"