23#include "openPMD/auxiliary/TypeTraits.hpp"
24#include "openPMD/auxiliary/UniquePtr.hpp"
28#include "openPMD/DatatypeMacros.hpp"
111template <
typename T,
typename U>
114 typename std::remove_pointer<typename std::remove_cv<
115 typename std::decay<typename std::remove_all_extents<T>::type>::
117 typename std::remove_pointer<typename std::remove_cv<
118 typename std::decay<typename std::remove_all_extents<U>::type>::
119 type>::type>::type>::type
122template <
typename T,
typename U>
126inline constexpr Datatype determineDatatype()
133 else if (decay_equiv<T, unsigned char>::value)
171 return DT::ULONGLONG;
183 return DT::LONG_DOUBLE;
193 else if (
decay_equiv<
T, std::complex<long double>>::value)
195 return DT::CLONG_DOUBLE;
207 return DT::VEC_SHORT;
219 return DT::VEC_LONGLONG;
221 else if (
decay_equiv<
T, std::vector<unsigned char>>::value)
223 return DT::VEC_UCHAR;
225 else if (
decay_equiv<
T, std::vector<signed char>>::value)
227 return DT::VEC_SCHAR;
229 else if (
decay_equiv<
T, std::vector<unsigned short>>::value)
231 return DT::VEC_USHORT;
233 else if (
decay_equiv<
T, std::vector<unsigned int>>::value)
237 else if (
decay_equiv<
T, std::vector<unsigned long>>::value)
239 return DT::VEC_ULONG;
241 else if (
decay_equiv<
T, std::vector<unsigned long long>>::value)
243 return DT::VEC_ULONGLONG;
247 return DT::VEC_FLOAT;
251 return DT::VEC_DOUBLE;
253 else if (
decay_equiv<
T, std::vector<long double>>::value)
255 return DT::VEC_LONG_DOUBLE;
257 else if (
decay_equiv<
T, std::vector<std::complex<float>>>::value)
259 return DT::VEC_CFLOAT;
261 else if (
decay_equiv<
T, std::vector<std::complex<double>>>::value)
263 return DT::VEC_CDOUBLE;
265 else if (
decay_equiv<
T, std::vector<std::complex<long double>>>::value)
267 return DT::VEC_CLONG_DOUBLE;
269 else if (
decay_equiv<
T, std::vector<std::string>>::value)
271 return DT::VEC_STRING;
275 return DT::ARR_DBL_7;
282 return Datatype::UNDEFINED;
297 using T_stripped = std::remove_cv_t<std::remove_reference_t<T>>;
298 if constexpr (auxiliary::IsPointer_v<T_stripped>)
300 return determineDatatype<auxiliary::IsPointer_t<T_stripped>>();
302 else if constexpr (auxiliary::IsContiguousContainer_v<T_stripped>)
304 static_assert(auxiliary::dependent_false_v<T_stripped>, R
"(
305Error: Passed a contiguous container type to determineDatatype<>().
306These types are not directly supported due to colliding semantics.
307Assuming a vector object `std::vector<float> vec;`,
308use one of the following alternatives:
3101) If what you want is a direct openPMD::Datatype equivalent
311 of the container type, use:
312 `determineDatatype<decltype(vec)>()`
314 `determineDatatype<std::vector<float>>()`.
315 The result will be `Datatype::VECTOR_FLOAT`.
3162) If what you want is the openPMD::Datatype equivalent of the *contained type*,
317 use the raw pointer overload by:
318 `determineDatatype(vec.data())`
319 The result will be `Datatype::FLOAT`.
320 This is the variant that you likely wish to use if intending to write data
321 from the vector via `storeChunk()`, e.g. `storeChunk(vec, {0}, {10})`.
326 static_assert(auxiliary::dependent_false_v<T_stripped>, R
"(
327Error: Unknown datatype passed to determineDatatype<>().
328For a direct translation from C++ type to the openPMD::Datatype enum, use:
329`auto determineDatatype<T>() -> Datatype`.
331For detecting the contained datatpye of a pointer type (shared or raw pointer),
332use this following template (i.e. `auto determineDatatype<T>(T &&) -> Datatype`)
333which accepts pointer-type parameters (raw, shared or unique).
337 return Datatype::UNDEFINED;
357 return sizeof(
unsigned char);
360 return sizeof(
signed char);
363 return sizeof(short);
371 case DT::VEC_LONGLONG:
372 return sizeof(
long long);
375 return sizeof(
unsigned short);
378 return sizeof(
unsigned int);
381 return sizeof(
unsigned long);
383 case DT::VEC_ULONGLONG:
384 return sizeof(
unsigned long long);
387 return sizeof(float);
391 return sizeof(double);
392 case DT::LONG_DOUBLE:
393 case DT::VEC_LONG_DOUBLE:
394 return sizeof(
long double);
397 return sizeof(float) * 2;
399 case DT::VEC_CDOUBLE:
400 return sizeof(double) * 2;
401 case DT::CLONG_DOUBLE:
402 case DT::VEC_CLONG_DOUBLE:
403 return sizeof(
long double) * 2;
408 throw std::runtime_error(
"toBytes: Invalid datatype!");
437 case DT::VEC_LONGLONG:
442 case DT::VEC_ULONGLONG:
445 case DT::VEC_LONG_DOUBLE:
447 case DT::VEC_CDOUBLE:
448 case DT::VEC_CLONG_DOUBLE:
473 case DT::LONG_DOUBLE:
474 case DT::VEC_LONG_DOUBLE:
498 case DT::VEC_CDOUBLE:
499 case DT::CLONG_DOUBLE:
500 case DT::VEC_CLONG_DOUBLE:
517 Datatype dtype = determineDatatype<T>();
532 Datatype dtype = determineDatatype<T>();
558 case DT::VEC_LONGLONG:
559 return std::make_tuple(
true,
true);
567 case DT::VEC_ULONGLONG:
568 return std::make_tuple(
true,
false);
570 return std::make_tuple(
false,
false);
585 Datatype dtype = determineDatatype<T>();
596template <
typename T_FP>
605 if (tt_is_fp && dt_is_fp &&
toBits(d) ==
toBits(determineDatatype<T_FP>()))
618template <
typename T_CFP>
627 if (tt_is_cfp && dt_is_cfp &&
641template <
typename T_Int>
645 bool tt_is_int, tt_is_sig;
649 bool dt_is_int, dt_is_sig;
650 std::tie(dt_is_int, dt_is_sig) =
isInteger(d);
652 if (tt_is_int && dt_is_int && tt_is_sig == dt_is_sig &&
671 case Datatype::SCHAR:
672 case Datatype::UCHAR:
691template <
typename T_Char>
703 if (
static_cast<int>(d) ==
static_cast<int>(e))
710 bool d_is_int, d_is_sig;
711 std::tie(d_is_int, d_is_sig) =
isInteger(d);
712 bool e_is_int, e_is_sig;
713 std::tie(e_is_int, e_is_sig) =
isInteger(e);
714 if (d_is_int && e_is_int && d_is_vec == e_is_vec && d_is_sig == e_is_sig &&
722 if (d_is_fp && e_is_fp && d_is_vec == e_is_vec &&
toBits(d) ==
toBits(e))
729 if (d_is_cfp && e_is_cfp && d_is_vec == e_is_vec &&
toBits(d) ==
toBits(e))
745std::string datatypeToString(
Datatype dt);
747Datatype stringToDatatype(
const std::string &s);
749void warnWrongDtype(std::string
const &key,
Datatype store,
Datatype request);
754constexpr auto datatypeIndex() ->
size_t
756 return static_cast<size_t>(
static_cast<int>(determineDatatype<T>()));
773template <
typename Action,
typename... Args>
775 ->
decltype(Action::template call<char>(std::forward<Args>(args)...));
792template <
typename Action,
typename... Args>
794 ->
decltype(Action::template call<char>(std::forward<Args>(args)...));
811template <
typename Action,
typename... Args>
813 ->
decltype(Action::template call<char>(std::forward<Args>(args)...));
817#if !defined(_MSC_VER)
843#include "openPMD/Datatype.tpp"
844#include "openPMD/UndefDatatypeMacros.hpp"
Public definitions of openPMD-api.
Definition Date.cpp:29
std::tuple< bool, bool > isInteger()
Compare if a type is an integer type.
Definition Datatype.hpp:583
constexpr bool isSameChar(Datatype d)
Determines if d and T_Char are char types of same representation.
bool isSameFloatingPoint(Datatype d)
Compare if a Datatype is equivalent to a floating point type.
Definition Datatype.hpp:597
bool isSameComplexFloatingPoint(Datatype d)
Compare if a Datatype is equivalent to a complex floating point type.
Definition Datatype.hpp:619
size_t toBits(Datatype d)
Return number of bits representing a Datatype.
Definition Datatype.hpp:417
@ T
time
Definition UnitDimension.hpp:41
bool isFloatingPoint()
Compare if a type is a floating point type.
Definition Datatype.hpp:515
constexpr bool isChar(Datatype d)
Determines if d represents a char type.
Definition Datatype.hpp:666
std::vector< Datatype > openPMD_Datatypes()
All openPMD datatypes defined in Datatype, listed in order in a vector.
Definition Datatype.cpp:227
constexpr bool isComplexFloatingPoint()
Compare if a type is a complex floating point type.
Definition Datatype.hpp:530
bool isSame(openPMD::Datatype const d, openPMD::Datatype const e)
Comparison for two Datatypes.
Definition Datatype.hpp:700
size_t toBytes(Datatype d)
Return number of bytes representing a Datatype.
Definition Datatype.hpp:345
constexpr bool isVector(Datatype d)
Compare if a Datatype is a vector type.
Definition Datatype.hpp:427
bool isSameInteger(Datatype d)
Compare if a Datatype is equivalent to an integer type.
Definition Datatype.hpp:642
Datatype
Concrete datatype of an object available at runtime.
Definition Datatype.hpp:51
constexpr auto switchType(Datatype dt, Args &&...args) -> decltype(Action::template call< char >(std::forward< Args >(args)...))
Generalizes switching over an openPMD datatype.
constexpr auto switchNonVectorType(Datatype dt, Args &&...args) -> decltype(Action::template call< char >(std::forward< Args >(args)...))
Generalizes switching over an openPMD datatype.
Datatype basicDatatype(Datatype dt)
basicDatatype Strip openPMD Datatype of std::vector, std::array et.
Definition Datatype.cpp:289
constexpr auto switchDatasetType(Datatype dt, Args &&...args) -> decltype(Action::template call< char >(std::forward< Args >(args)...))
Generalizes switching over an openPMD datatype.
Fundamental equivalence check for two given types T and U.
Definition Datatype.hpp:120