diff -r 6d95c1a41e6e -r 63125c36de73 src/typeconversions.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/typeconversions.h Tue Jun 14 19:50:31 2022 +0300 @@ -0,0 +1,88 @@ +#ifndef TYPECONVERSIONS_H +#define TYPECONVERSIONS_H +#include +#include + +template +struct transfer_cvref +{ + using type = std::remove_reference_t; +}; + +template +struct transfer_cvref +{ + using type = std::remove_reference_t&; +}; + +template +struct transfer_cvref +{ + using type = const std::remove_reference_t&; +}; + +template +struct transfer_cvref +{ + using type = std::remove_reference_t&&; +}; + +//! \brief transfer l-value reference, r-value reference and const l-value +//! reference from T onto R +template +using transfer_cvref_t = typename transfer_cvref::type; +static_assert(std::is_same_v, char>); +static_assert(std::is_same_v, char&>); +static_assert(std::is_same_v, char&&>); + +//! \brief casts @c x to a suitable unsigned integer +template +constexpr auto unsigned_cast(T x) + -> std::enable_if_t, std::make_unsigned_t> +{ + return static_cast>(x); +} + +//! \brief casts @c x to a suitable signed integer +template +constexpr auto signed_cast(T x) + -> std::enable_if_t, std::make_signed_t> +{ + return static_cast>(x); +} + +//! \brief casts floating point values to float +template +constexpr auto float_cast(T x) + -> std::enable_if_t, float> +{ + return static_cast(x); +} + +//! \brief casts floating point values to double +template +constexpr auto double_cast(T x) + -> std::enable_if_t, double> +{ + return static_cast(x); +} + +//! \brief casts floating point values to qreal +template +constexpr auto qreal_cast(T x) + -> std::enable_if_t, qreal> +{ + return static_cast(x); +} + +//! \brief convert an enum value to its corresponding integer value (including +//! references) +template +constexpr auto&& enum_value_cast(T&& enu) +{ + using valuetype = std::underlying_type_t>; + using reftype = transfer_cvref_t; + return reinterpret_cast(enu); +} + +#endif // TYPECONVERSIONS_H